OOPs Defination
OOPs Defination
Encapsulation is the concept of wrapping data (fields) and methods in a single unit, or class,
and restricting access to some of the class's components. Encapsulation promotes data hiding
by making fields private and providing public getter and setter methods for access.
Example:
public class Person {
private String name; // private field
Inheritance:
Inheritance allows a class (subclass) to inherit fields and methods from another class
(superclass), promoting code reuse and establishing a relationship between classes.
Example:
class Animal {
void eat() { System.out.println("Eating..."); }
}
Abstraction:
Abstraction is the concept of hiding complex implementation details and showing only essential
features. It is achieved through abstract classes and interfaces.
Example:
abstract class Shape {
abstract void draw(); // abstract method
}
Classes:
A class is a blueprint for creating objects, containing fields (attributes) and methods (behavior).
Example:
public class Car {
String color;
void drive() { System.out.println("Car is driving"); }
}
Methods:
Methods are functions defined in a class that define behavior for objects of that class.
Example:
public void greet() { System.out.println("Hello!"); }
Objects:
Objects are instances of a class created with the new keyword.
Example:
Car car1 = new Car(); // car1 is an object of the Car class
Creation of Objects:
Objects are created by calling a class’s constructor with the new keyword.
Example:
Person person = new Person(); // calls the default constructor of Person
Constructor:
A special method used to initialize objects. It has the same name as the class and no return
type.
Example:
public class Car {
Car() { System.out.println("Car is created"); }
}
Parameterized Constructor:
A constructor that accepts parameters to initialize fields.
Example:
public class Car {
String color;
Car(String color) { this.color = color; }
}
Copy Constructor:
A constructor that creates a new object as a copy of an existing object.
Example:
public class Car {
String color;
Car(Car car) { this.color = car.color; }
}
Overloaded Constructor:
A class having multiple constructors with different parameters to allow different ways of object
initialization.
Example:
public class Car {
Car() { } // default constructor
Car(String color) { this.color = color; } // parameterized constructor
}
Object as Reference:
In Java, objects are accessed through references, which store the memory address of the object.
Example:
Car car1 = new Car();
Car car2 = car1; // car2 is a reference to car1
Finalizer() Method:
This method is called just before an object is garbage collected. Java uses protected void
finalize() for cleanup.
Garbage Collection:
Java’s memory management process that automatically removes objects no longer in use.
Example:
car1 = null; – If no other reference exists, the object will be eligible for garbage collection.
This Keyword:
Refers to the current object within a class.
Example:
public class Car {
String color;
Car(String color) { this.color = color; }
}
Super Keyword:
Refers to the superclass and is used to access superclass methods and constructors.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
Immutable Classes:
Classes whose objects cannot be modified after creation. String is an example of an immutable
class.
Example:
String s = "hello";
s = "world"; // a new String is created; "hello" remains unchanged
Mutable Classes:
Classes where objects can be changed after creation, like StringBuilder.
Example:
StringBuilder sb = new StringBuilder("hello");
sb.append(" world"); // modifies sb to "hello world"
String as an Immutable Class: The String class in Java is immutable, meaning its value cannot be
changed once assigned.
Example:
String str = "hello";
str = "world"; // creates a new String object
Final Keyword:
Used to declare constants (variables that cannot be changed), prevent inheritance (final class),
or prevent method overriding (final method).
Example:
final int MAX = 100; // constant value
Overloading:
Multiple methods with the same name but different parameters in the same class.
Example:
void add(int a, int b) { }
void add(double a, double b) { }
Overriding:
Redefining a superclass method in a subclass to provide specific behavior.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
Access Modifiers:
Access modifiers are keywords in Java that control the accessibility or visibility of classes,
methods, and variables. Java has four main access modifiers:
1. public: Allows access from any other class.
2. protected: Allows access within the same package and from subclasses in other
packages.
3. default (package-private): Allows access only within the same package (if no modifier is
specified).
4. private: Allows access only within the same class.
These modifiers primarily control the visibility of class members and are used to enforce
encapsulation in Java.
Example of Access Modifiers:
public class Car { // public class accessible from any package
private String model; // private field accessible only within this class
protected int year; // protected field accessible within the package and subclasses
String color; // default (package-private) field accessible only within the package
}
Access Specifiers:
Access specifiers is a broader term that includes all keywords that determine access levels,
including access modifiers as well as other specifiers that modify behavior. For instance:
• Access Modifiers like public, protected, default, and private.
• Non-access Modifiers that change the behavior of a class or member, like static, final,
abstract, synchronized, and native.
So, access modifiers are a subset of access specifiers in Java.
Aggregation
• Definition: Aggregation is a type of association that represents a weak "has-a"
relationship between two classes. In aggregation, one class can contain or use another
class, but the contained class can still exist independently of the container.
• Example: A Library and Book relationship. A Library can contain Books, but if the Library
is closed or deleted, the Books may still exist independently.
• Key Point: Aggregation implies a relationship where the contained objects can exist
separately from the container.
Composition
• Definition: Composition is a stronger form of association that represents a strong "has-
a" relationship. In composition, one class is entirely dependent on another; if the
container object is destroyed, the contained objects are also destroyed.
• Example: A House and Room relationship. A House is composed of multiple Rooms, and
if the House is destroyed, the Rooms do not exist separately—they are destroyed along
with the house.
• Key Point: Composition implies ownership, meaning that the contained objects do not
exist independently of the container.
Is-A Relationship
• Definition: The "is-a" relationship represents inheritance in OOP, where one class (the
subclass) inherits from another class (the superclass). It shows that the subclass is a type
of the superclass.
• Example: A Car is a Vehicle. Here, Car inherits from Vehicle, meaning it has the
properties and behaviors of a Vehicle but can also have additional features specific to
Car.
• Key Point: The "is-a" relationship is useful for creating a hierarchy and supports
polymorphism, where subclasses can be treated as instances of their superclass.
Has-A Relationship
• Definition: The "has-a" relationship represents aggregation or composition. It means
that a class contains or uses objects of another class as part of its functionality.
• Example:
o Aggregation (Weak Has-A): A School has Students. If the School is removed, the
Students can still exist independently.
o Composition (Strong Has-A): A Car has an Engine. The Engine cannot exist
separately from the Car; if the Car is destroyed, so is the Engine.