0% found this document useful (0 votes)
2 views9 pages

Java Inheritance and Aggregation Explained

Inheritance in Java allows one object to acquire properties and behaviors from a parent object, representing an IS-A relationship. It supports method overriding for runtime polymorphism and code reusability, while aggregation represents a HAS-A relationship. The document also covers types of inheritance, method overriding rules, and the differences between method overloading and overriding.

Uploaded by

Utkarsh raut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Java Inheritance and Aggregation Explained

Inheritance in Java allows one object to acquire properties and behaviors from a parent object, representing an IS-A relationship. It supports method overriding for runtime polymorphism and code reusability, while aggregation represents a HAS-A relationship. The document also covers types of inheritance, method overriding rules, and the differences between method overloading and overriding.

Uploaded by

Utkarsh raut
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Inheritance in Java

Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of parent object.
Inheritance represents the IS-A relationship, also known as parent-child relationship.

Why use inheritance in java


 For Method Overriding (so runtime polymorphism can be achieved).
 For Code Reusability.

Syntax of Java Inheritance


1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4. }

1. class Employee
2.
3. {
4. float salary=40000;
5. }
6. class Programmer extends Employee{
7. int bonus=10000;
8. public static void main(String args[]){
9. Programmer p=new Programmer();
10. [Link]("Programmer salary is:"+[Link]);
11. [Link]("Bonus of Programmer is:"+[Link]);
12. }
13. }

Types of inheritance in java


On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance is supported through interface only.
Q) Why multiple inheritance is not supported in java?
1. class A{
2. void msg(){[Link]("Hello");}
3. }
4. class B{
5. void msg(){[Link]("Welcome");}
6. }
7. class C extends A,B{//suppose if it were
8.
9. Public Static void main(String args[]){
10. C obj=new C();
11. [Link]();//Now which msg() method would be invoked?
12. }
13. }

Compile Time Error

Aggregation in Java
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-
A relationship.

Why use Aggregation?


 For Code Reusability.
Simple Example of Aggregation

In this example, we have created the reference of Operation class in the Circle class.
1. class Operation{
2. int square(int n){
3. return n*n;
4. }
5. }
6.
7. class Circle{
8. Operation op;//aggregation
9. double pi=3.14;
10.
11. double area(int radius){
12. op=new Operation();
13. int rsquare=[Link](radius);//code reusability (i.e. delegates the method call).
14. return pi*rsquare;
15. }
16.
17. public static void main(String args[]){
18. Circle c=new Circle();
19. double result=[Link](5);
20. [Link](result);
21. }
22. }
Test it Now

Output:78.5

When use Aggregation?


 Code reuse is also best achieved by aggregation when there is no is-a relationship.
 Inheritance should be used only if the relationship is-a is maintained throughout the
lifetime of the objects involved; otherwise, aggregation is the best choice.
understanding meaningful example of Aggregation

[Link]
1. public class Address {
2. String city,state,country;
3.
4. public Address(String city, String state, String country) {
5. [Link] = city;
6. [Link] = state;
7. [Link] = country;
8. }
9.
10. }

[Link]
1. public class Emp {
2. int id;
3. String name;
4. Address address;
5.
6. public Emp(int id, String name,Address address) {
7. [Link] = id;
8. [Link] = name;
9. [Link]=address;
10. }
11.
12. void display(){
13. [Link](id+" "+name);
14.
15. [Link]([Link]+" "+[Link]+" "+[Link]);
16. }
17.
18. public static void main(String[] args) {
19. Address address1=new Address("gzb","UP","india");
20. Address address2=new Address("gno","UP","india");
21.
22. Emp e=new Emp(111,"varun",address1);
23. Emp e2=new Emp(112,"arun",address2);
24.
25. [Link]();
26. [Link]();
27.
28. }
29. }
Output:111 varun
gzb UP india
112 arun
gno UP india
Method Overriding in Java
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in java

Usage of Java Method Overriding


o Method overriding is used to provide specific implementation of a method that is
already provided by its super class.
o Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).

Understanding the problem without method overriding


Let's understand the problem that we may face in the program if we don't use method
overriding.
1. class Vehicle{
2. void run(){[Link]("Vehicle is running");}
3. }
4. class Bike extends Vehicle{
5.
6. public static void main(String args[]){
7. Bike obj = new Bike();
8. [Link]();
9. }
10. }
Output:Vehicle is running
example of method overriding
In this example, we have defined the run method in the subclass as defined in the parent
class but it has some specific implementation. The name and parameter of the method is
same and there is IS-A relationship between the classes, so there is method overriding.
1. class Vehicle{
2. void run(){[Link]("Vehicle is running");}
3. }
4. class Bike2 extends Vehicle{
5. void run(){[Link]("Bike is running safely");}
6.
7. public static void main(String args[]){
8. Bike2 obj = new Bike2();
9. [Link]();
10. }
Test it Now

Output:Bike is running safely

Real example of Java Method Overriding


Consider a scenario, Bank is a class that provides functionality to get rate of interest. But,
rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could
provide 8%, 7% and 9% rate of interest.

1. class Bank{
2. int getRateOfInterest(){return 0;}
3. }
4.
5. class SBI extends Bank{
6. int getRateOfInterest(){return 8;}
7. }
8.
9. class ICICI extends Bank{
10. int getRateOfInterest(){return 7;}
11. }
12. class AXIS extends Bank{
13. int getRateOfInterest(){return 9;}
14. }
15.
16. class Test2{
17. public static void main(String args[]){
18. SBI s=new SBI();
19. ICICI i=new ICICI();
20. AXIS a=new AXIS();
21. [Link]("SBI Rate of Interest: "+[Link]());
22. [Link]("ICICI Rate of Interest: "+[Link]());
23. [Link]("AXIS Rate of Interest:
24.
25. ());
26. }
27. }
Test it Now

Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

Can we override static method?


No, static method cannot be overridden. It can be proved by runtime polymorphism

Why we cannot override static method?


because static method is bound with class whereas instance method is bound with object.
Static belongs to class area and instance belongs to heap area.

Can we override java main method?


No, because main is a static method.
Difference between method overloading and
method overriding in java
There are many differences between method overloading and method overriding in java. A
list of differences between method overloading and method overriding are given below:

No. Method Overloading Method Overriding

1) Method overloading is used to increase the Method overriding is used to provide


readability of the program. the specific implementation of the
method that is already provided by
its super class.

2) Method overloading is performed within class. Method overriding occurs in two


classes that have IS-A (inheritance)
relationship.

3) In case of method overloading, parameter must be In case of method overriding, parameter


different. must be same.

4) Method overloading is the example of compile time Method overriding is the example of run
polymorphism. time polymorphism.

5) In java, method overloading can't be performed by Return type must be same or covariant in
changing return type of the method only. Return type method overriding.
can be same or different in method overloading. But
you must have to change the parameter.

Java Method Overloading example


1. class OverloadingExample{
2. static int add(int a,int b){return a+b;}
3. static int add(int a,int b,int c){return a+b+c;}
4. }

Java Method Overriding example


1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void eat(){[Link]("eating bread...");}
6. }

You might also like