Object & Class
Object-Oriented Programming in Java
Class Definition:
A class is a blueprint for creating objects. It defines the attributes (fields or data members) and methods (functions or behavior) that the objects of the class will have. Here's a basic example of a class:
public class Car {
String make;
String model;
int year;
public void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
Object Creation:
An object is an instance of a class. Once a class is defined, you can create multiple objects of that class. Here's how you can create an object of the Car
class:
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Camry";
myCar.year = 2022;
myCar.displayInfo();
}
}
Constructors:
A constructor is a special method that is called when an object is created. It initializes the object's state. If you don't explicitly define a constructor, Java provides a default constructor. Here's an example of a constructor:
public class Car {
String make;
String model;
int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
public void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
Encapsulation:
Encapsulation is the bundling of data and methods that operate on the data within a single unit (class). It hides the internal details of an object and exposes only what is necessary. In Java, you often use private access modifiers to achieve encapsulation.
Access Modifiers:
Access modifiers control the visibility of fields and methods. The main access modifiers in Java are:
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible within the same package or by subclasses.
- default (no modifier): Accessible within the same package.
Inheritance:
Inheritance is a mechanism where a class (subclass/derived class) inherits the properties and behaviors of another class (superclass/base class). It promotes code reuse and establishes a relationship between classes.
Example with Inheritance:
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
Polymorphism:
Polymorphism allows objects of different types to be treated as objects of a common type. It can be achieved through method overloading and method overriding.
Example with Polymorphism:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
class Square extends Shape {
@Override
void draw() {
System.out.println("Drawing a square");
}
}
This is a high-level overview of object-oriented concepts in Java. OOP principles, including encapsulation, inheritance, and polymorphism, are fundamental to Java programming and contribute to creating modular, maintainable, and extensible code.