0% found this document useful (0 votes)
8 views

Aggregation and Inheritance

The document discusses the concepts of aggregation and inheritance in object oriented programming. Aggregation refers to a HAS-A relationship where one class uses another class as a property, while inheritance refers to a IS-A relationship where a subclass inherits properties and behaviors from its parent superclass.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Aggregation and Inheritance

The document discusses the concepts of aggregation and inheritance in object oriented programming. Aggregation refers to a HAS-A relationship where one class uses another class as a property, while inheritance refers to a IS-A relationship where a subclass inherits properties and behaviors from its parent superclass.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming

Code Re-use

Aggregation

&

Inheritance
Object Oriented Programming

The concepts of Aggregation and Inheritance are characterized by the relationship of


the new code you write to the code you re-use.

If the code you write Has-A instance of the class you re-use, then it's Aggregation

If the new code you write Is-A derivation of the code you re-use, then it's Inheritance.
Inheritance
Object Oriented Programming

The idea behind Aggregation is the simpler idea.

When you re-use code written before by declaring a previously defined class in your
code, then you are using aggregation.
aggregation

aggregation represents the HAS-A relationship.

Say you have a class called Address:

public class Address {


String city,state,country;

public Address(String city, String state, String country) {


this.city = city;
this.state = state;
this.country = country;
}
}
Object Oriented Programming
Now if you want to use address in another class Emp, this is aggregation.
aggregation

public class Emp {


int id;
String name;
Address address;

public Emp(int id, String name,Address address) {


this.id = id;
this.name = name;
this.address=address;
}

public static void main(String[] args) {


Address address1=new Address("street1","MA","usa");
Address address2=new Address("stree2","NY","usa");

Emp e=new Emp(101,"ravi",address1);


Emp e2=new Emp(102,"aruna",address2);

e.display();
e2.display();
}
}
Object Oriented Programming

The idea behind inheritance in java is that you can create new classes that are built
upon existing classes.

When you inherit from an existing class, you can reuse methods and fields of the parent
class, and you can also add new methods and fields.

Inheritance represents the IS-A relationship, also known as parent-child relationship.


Object Oriented Programming

Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.

properties == fields
Behavior == methods
Object Oriented Programming
Object Oriented Programming
SuperClass is the parent
SubClass is the child
Object Oriented Programming

h
Object Oriented Programming

Syntax of Java Inheritance:

class Subclass-name extends Superclass-name


{
//methods and fields
}

The extends keyword indicates that you are making a new class that derives
from an existing class.

In the terminology of Java, a class that is inherited is called a super class.

The new class is called a subclass.
Object Oriented Programming
Understanding the simple example of inheritance

As displayed in the above figure:



Programmer is the subclass

Employee is the superclass.
superclass
Relationship between two classes is: Programmer IS-A Employee.
It means that Programmer is a type of Employee.
Object Oriented Programming

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);
}
}
Object Oriented Programming

Different types of inheritance


Object Oriented Programming

Different types of inheritance


Object Oriented Programming

On the basis of class, there are types of inheritance in java:



Single

Multi-level

hierarchical.

In java programming, multiple and hybrid inheritance is supported through


interface only. We will learn about interfaces later.
Object Oriented Programming

To reduce the complexity and simplify the language, multiple inheritance is not
supported in java through class.

It's better to get a Compile time error than to get a runtime error in your program.

class A{
void msg(){System.out.println("Class A");}
}

class B{
void msg(){System.out.println("Class B");}
}

class C extends A,B{ //suppose this was allowed.

Public Static void main(String args[]){


C obj=new C();
obj.msg(); //Now which msg() method would be invoked?
}
}
Object Oriented Programming

You might also like