[!INDEX]
- def - is a - subclass inherit...
- def - has a - one class contains or is composed ...
- def - composition ( strong form, life cycle, owned)
- def - aggregation ( week form, not - life cycle, owned)
- is a code dog subclass of animal ( dog is a animal) code
- code : composition = car, engine
- code : aggregation = car, engine
1. Def. - is a - subclass inherit...
[!NOTE]
- where one class (subclass or child class) inherits the properties and behaviors of another class (superclass or parent class).
- In Java, the concepts of "is-a" relationship and "has-a" relationship are fundamental to understanding object-oriented programming principles like inheritance and composition.
2. def. - has a - one class contains or is composed ...
[!NOTE]
- Both composition and aggregation represent "has-a" relationships where one class contains or is composed of one or more instances of other classes.
- one class contains a reference to another class. This relationship is established using instance variables. It indicates that an object of one class has an object of another class.
3. def. - composition ( strong form, life cycle, owned)
[!NOTE]
- strong form of association where the contained object (part) is owned by the container object (whole). If the container object is destroyed, the contained object is also destroyed.
- Strong Ownership: The container class owns the contained class. The contained object cannot exist independently of the container.
- Lifecycle Dependency: The lifecycle of the contained object is tightly bound to the lifecycle of the container object.
4. def. - aggregation ( week form, not - life cycle, owned)
[!NOTE]
- weaker form of association where the contained object can exist independently of the container object. The container and the contained objects have their own lifecycles.
- Key Characteristics:
- Weak Ownership: The container class references the contained class but does not own it. The contained object can exist independently of the container.
- Independent Lifecycle: The lifecycle of the contained object is independent of the container object.
5. is a code dog subclass of animal ( dog is a animal) code
// Superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method from Animal
myDog.bark(); // Method of Dog
}
}
[!NOTE]
- In this example,
Dogis a subclass ofAnimal. Therefore, aDogis anAnimal, establishing an "is-a" relationship.
6. code : composition = car, engine
class Engine {
void start() {
System.out.println("Engine starts.");
}
}
class Car {
private Engine engine;
Car() {
this.engine = new Engine(); // Creating and owning the Engine instance
}
void startCar() {
engine.start();
System.out.println("Car starts.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.startCar();
}
}
[!NOTE]
- In this example,
Carhas a strong ownership ofEngine. TheEngineinstance is created within theCarconstructor and cannot exist without theCar.- the car is container , coz it contains /own the engine object.
- if car object destroy , cant access the engine object, means only car object can access the engine members
- owns का मतलब contained object की life cycle को container का object manage करेगा | मतलब creation , existence और destruction ये तीनो container object पर निर्भर करता है |
7. code : aggregation = car, engine
class Engine {
void start() {
System.out.println("Engine starts.");
}
}
class Car {
private Engine engine;
Car(Engine engine) {
this.engine = engine; // Using an existing Engine instance
}
void startCar() {
engine.start();
System.out.println("Car starts.");
}
}
public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Car car = new Car(engine); // Passing an existing Engine instance
car.startCar();
}
}
[!NOTE]
- In this example,
CaraggregatesEngine. TheEngineinstance is created outside theCarand passed to theCarconstructor. TheEnginecan exist independently of theCar.- its mean that another class can easily access the engine fields and members.
- Composition/Aggregation - दोनों का मतलब एक class contain करती है दुसरी क्लास का reference बिना extends के method को कैसे access करे
[!approach]
- create engine { with start }and car class { with startCar() }
- in main class , turn on car using startCar method(). -- but car doesn't has engine
- two way to achieve
COMPOSITION
- create engine instance variable in car and initialized it with new instance of Engine class.
- this instance can now call Engine's class start method.
AGGREGATION
- create new instance in main method and pass it to car parameterized constructor
- this instance can now call Engine's class start method.