The Best Way To Learn Java Inheritance and Aggregation in Just 5 Days

0
57
java training in chennai

Learn Java Inheritance and Aggregation

An object can inherit all the properties and behaviors of a parent object through the inheritance mechanism in Java. It is the main component of OOPs (Object Oriented Programming Systems).

  • The legacy concept in Java is that new classes can be created on top of old ones. When you inherit from an existing class, you can use the methods and properties of the parent class. Additional fields and techniques can be added to an existing class.
  • A parent-child relationship, also known as an IS-A relationship, is represented by inheritance.

Why is inheritance used in Java?

In Java, the most crucial use of inheritance is code reuse. Code in the parent class can be used directly by the child class. Runtime polymorphism is one more name for the technique Overriding. Consequently, polymorphism can be implemented in Java using inheritance.

  • Method Overriding
  • Code Reusability

Inheritance terminology

Class: A class is a gathering of items with comparable properties. It is a layout or plan from which things are made.

Subclass/Child Class: A subclass is a class that derives from another class. Another name is derived class, extended class, or kit class.

Super Class/Parent Class: A superclass is a class from which a subclass inherits its features. It is also referred to as base class or parent class.

Reusability: As the name suggests, reusability is a feature that allows the reuse of existing class fields and methods while creating a new class. You can reuse the fields and methods defined in the previous class.

Syntax of Inheritance

class Subclass-name extends Superclass-name  

{  

   //methods and fields  

}  

Types of inheritance in java

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

Single Inheritance

In a single Inheritance, a Single subclass stretches out from a solitary superclass.

Multilevel Inheritance

In staggering Inheritance, a subclass stretches out from a superclass and afterward, a similar subclass goes about as a superclass for another class.

Hierarchical Inheritance

In various leveled legacies, numerous subclasses stretch out from a solitary superclass.

Multiple Inheritance

In various Inheritance, a solitary subclass stretches out from numerous superclasses.

Hybrid Inheritance

Hybrid Inheritance is a mix of at least two sorts of Inheritance.

Example of Employee: 

 

class Employee{  

 float salary=40000;  

}  

class Programmer extends Employee{  

 int bonus=10000;  

 public static void main(String args[]){  

   Programmer p=new Programmer();  

   System.out.println(“Programmer salary is:”+p.salary);  

   System.out.println(“Bonus of Programmer is:”+p.bonus);  

}  

}  

Aggregation in Java

Aggregation in Java is a two-class relationship, best described as a “contains” and a “whole/part” relationship. This is a more specific variation of the association link. An aggregate class contains a reference to another class and is said to belong to that class. Each specified class is considered a subset of the total class.

  • Since a total link cannot have circular references, the right happens. 
  • If class A refers to class B and class B refers to class A, clear ownership cannot be determined and the relationship is simply association.

Why use Aggregation?

  • Aggregation occurs when object A contains a reference to another object B or when object A has a HAS-A relationship with B.
  • Integration facilitates code reuse. Application methods in object B can be called by multiple objects. Any class that contains object B can use its methods.

While utilizing Aggregation?

  • Code reuse is likewise best accomplished by collection when there is no is-a relationship.
  • Legacy ought to be utilized provided that the relationship is-an is kept up with all through the lifetime of the items in question; in any case, conglomeration is the most ideal decision.

 

Examples

 

 public class Subject {

private String name;

public void setName(String name)  {

this.name = name;

}

public String getName()

{

return name;

}

}

public class Student {

private Subject[] studyAreas = new Subject[10];

//the rest of the Student class

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here