Class: A blueprint for creating objects, defining attributes (fields) and behaviors (methods) that objects of that type will have.
Example :
public class Car { // Class definition String color; int year; // Constructor public Car(String color, int year) { this.color = color; this.year = year; } // Method public void drive() { System.out.println("The car is driving."); } }
Object: An instance of a class. It's a concrete realization of the class blueprint, with its own set of attributes and behaviors.
Example :
Car myCar = new Car("Red", 2022); // Creating an object of the Car class
Method: A block of code that performs a specific task. Methods are defined within a class and can be called to execute their functionality.
Example :
public void drive() { System.out.println("The car is driving."); }
Constructor: A special type of method that is called when an object is instantiated. It is used to initialize the object's state.
Example :
public Car(String color, int year) { this.color = color; this.year = year; }
Instance Variables (Fields): Variables declared within a class but outside of any method. They represent the state of an object.
Example :
String color; int year;
Instance Method: A method that operates on an instance of a class. It can access and modify the instance variables of the class.
Example :
public void drive() { System.out.println("The car is driving."); }
Inheritance: A mechanism in which a new class inherits properties and behaviors from an existing class (superclass). It promotes code reuse and allows for the creation of hierarchical relationships between classes.
Example :
public class SportsCar extends Car { // Additional fields and methods specific to SportsCar }
Polymorphism: The ability of objects of different classes to be treated as objects of a common superclass. It allows methods to be called on objects without knowing their specific type at compile time.
Example :
Car myCar = new SportsCar(); // Polymorphism
Encapsulation: The bundling of data (instance variables) and methods that operate on the data into a single unit (class). It hides the internal state of an object and only exposes the necessary functionalities.
Example :
private String model; // Encapsulated field public void setModel(String model) { // Encapsulated method this.model = model; }
Abstraction: The process of hiding the implementation details of a class and only showing its essential features. It allows for the creation of user-defined data types with specific behaviors.
Example :
public abstract class Vehicle { public abstract void drive(); // Abstract method }
Interface: A reference type in Java that defines a set of abstract methods. Classes can implement interfaces, thereby agreeing to provide concrete implementations for those methods.
Example :
public interface Drivable { void drive(); }
Package: A namespace that organizes a set of related classes and interfaces. It helps in avoiding naming conflicts and provides better modularization of code.
Example :
package com.example.myproject;
Access Modifiers: Keywords such as
public
,private
,protected
, anddefault
that control the accessibility of classes, variables, and methods in Java.Example :
public class Car { // Public access modifier private String color; // Private access modifier protected int year; // Protected access modifier int price; // Default (package-private) access modifier }
Static Keyword: A keyword used to declare members (variables and methods) that belong to the class rather than to any specific instance. Static members can be accessed using the class name itself.
Example :
public static double PI = 3.14; // Static variable public static void printMessage() { // Static method System.out.println("Hello!"); }
Exception Handling: The process of handling runtime errors or exceptional conditions in a program. Java provides keywords such as
try
,catch
,finally
, andthrow
for exception handling.Example :
try { // Code that may throw an exception } catch (Exception e) { // Handling the exception } finally { // Code that runs regardless of whether an exception occurred or not }
No comments:
Post a Comment