Inheritance in OOP
Inheritance in OOP
Dr. Kuppusamy .P
Associate Professor / SCOPE
Inheritance in OOP
• Inheritance is the mechanism in which one class inherits the attributes (variables)
and methods of another class i.e., inheritance defines relationship among classes.
• The class whose properties and methods are inherited is known as the parent class
or superclass or base class.
• The class derives (inherits) the properties and methods from the parent class is the
child class or derived class or extended class or sub class.
• The child class can add its own fields and methods in addition to the parent class
fields and methods.
• Keyword used for inheritance is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Dr. Kuppusamy P
Inheritance in OOP
• Association, Aggregation, Composition are the terms used to signify the
relationship between classes.
• Inheritance provides code re-usability i.e., instead of writing the same code again
and again, user can simply inherit the properties of one class into the other.
University
class
Dr. Kuppusamy P
Association
• Association is a relationship between two objects.
• Association is an ‘using’ relationship specifies how objects know each other and
how they are using each other’s functionality.
Symbol:
• The association between objects could be
• one-to-one E.g. A Student can have only one student_id card
Student Student_id
• one-to-many E.g. A Student can register many courses
Course 1
Student
Course 2
• many-to-one E.g. Many students can register single course
Student 1
Course
Student 2
Student 1 Course 1
• many-to-many
• E.g. Many students can register many courses Student 2 Course 2
Dr. Kuppusamy P
Association Association
Types of Association
• Aggregation Aggregation
• Composition
Aggregation Composition
• Aggregation is a special case of association
• A unidirectional association between objects
• When an object ‘has-a’ another object, then user have got an aggregation between
them. Aggregation is also called a “Has-a” relationship (one way relationship).
• Symbol:
Example:
• Library object has a Bookcase Object. Bookcase Object has a Book
• If java book object is destroyed, bookcase object does not destroyed. Because it can have another
object. So, it has Weak association.
Dr. Kuppusamy P
Composition
• Composition is a special case of aggregation
• Also called restricted aggregation since it has part-of relationship.
• If the parent object is destroyed, then the child objects also be terminated. So, it
has Strong association.
• Symbol:
Example:
• Book contains many pages. If the Book is demolished, then all of the pages are
also destroyed.
public class Book
{
private Page page;
public Book()
{
page = new Page();
}
} Dr. Kuppusamy P
Types of inheritance
Dr. Kuppusamy P
Types of inheritance
• Based on the class, three types of inheritance in java: single, multilevel and
hierarchical.
• Based on interface, multiple and hybrid inheritance is supported by java.
Single Inheritance
• A class inherits another class.
• E.g., Lion class inherits the Animal class.
class Animal{
void eat(){System.out.println("eating...");}
}
class Lion extends Animal{
void roar(){System.out.println(“Roaring...");}
}
class InheritEx{
public static void main(String args[]){
Lion d=new Lion();
d.roar();
d.eat();
}}
Dr. Kuppusamy P
Types of inheritance
Multilevel Inheritance
• A class can be derived from more than one base class.
• E.g., Lioncub class inherits the Lion class which again inherits the Animal class.
class Animal{
void eat(){System.out.println("eating...");}
}
class Lion extends Animal{
void roar(){System.out.println(“Roaring...");}
}
class Lioncub extends Lion{
void weep(){System.out.println("weeping...");}
}
class InheritEx{
public static void main(String args[]){
Lioncub d=new Lioncub();
d.roar();
d.eat();
d.weep();
}}
Dr. Kuppusamy P
Types of inheritance
Hierarchical Inheritance
• Two or more classes inherits a single class.
• E.g., Lion and Tiger classes inherits the Animal class.
class Animal{
void eat(){System.out.println("eating...");}
}
class Lion extends Animal{
void roar(){System.out.println(“Roaring...");}
}
class Tiger extends Animal{
void run(){System.out.println(“Running...");}
}
class InheritEx{
public static void main(String args[]){
Tiger d=new Tiger();
d.run();
d.eat();
d.roar(); // Compile time error
Lion L = new Lion();
L.roar();
L.eat(); }} Dr. Kuppusamy P
Create an Array of objects
• Array is a collection of the same data type that dynamically creates objects and contains
elements of primitive data types (int, String, float, long, Char, double, etc).
Syntax
ClassName obj[]=new ClassName[array_length];
Example:
• Class Rackets have five rackets records.
Rackets obj[] = new Rackets[5]
Initializing an array of objects
• Should initialize each element of array i.e. each object/object reference needs to be
initialized.
Two approaches to initialize the array of objects:
• Using the constructors
• Using a separate member method
Dr. Kuppusamy P
Create an Array of objects and Initialize using the
constructors
class ShopKeeper1 {
public static void main(String args[]) {
// Declaring an array of Rackets
Rackets[] rack = new Rackets[4];
// Creating actual Racket objects
rack[0] = new Rackets("voltric","America",5000, "Red");
rack[1] = new Rackets("nanoray", "India",4000, "Greeen");
rack[2] = new Rackets("astrox","Srilanka",3000, "Black");
rack[3] = new Rackets("carbonex","Italy",5500, "Yellow");
System.out.println("Racket data in rack[0]: ");
rack[0].show();
System.out.println("Racket data in rack[1]: ");
rack[1].show();
System.out.println("Racket data in rack[2]: ");
rack[2].show();
System.out.println("Racket data in rack[3]: ");
rack[3].show();
} } Dr. Kuppusamy P
Create an Array of objects and Initialize using the
constructors
// Creating a Racket class with origin, price, color
class Rackets {
private String racktype;
private String origin;
private String color;
private int price;
// Constructor
Rackets(String racktype, String origin, int price,String color)
{
this.racktype = racktype;
this.origin = origin;
this.price = price;
this.color = color;
}
Dr. Kuppusamy P
Create an Array of objects and Initialize using the
constructors
public void show()
{
System.out.println("Racket Type:"+racktype+", "+"Origin: " + origin + ", "+ "Price: "+
price+", "+"Color: "+color);
System.out.println();
}
}
Dr. Kuppusamy P
Create an Array of objects and Initialize using a
separate member method
class ShopKeeper {
public static void main(String args[])
{
// Declaring an array of Rackets
Rackets[] rack = new Rackets[4];
// Creating actual racket objects
rack[0] = new Rackets();
rack[1] = new Rackets();
rack[2] = new Rackets();
rack[3] = new Rackets();
// Assigning data to Rackets objects
rack[0].setData("voltric","America",5000, "Red");
rack[1].setData("nanoray", "India",4000, "Greeen");
rack[2].setData("astrox","Srilanka",3000, "Black");
rack[3].setData("carbonex","Italy",5500, "Yellow");
// Displaying the Rackets data
System.out.println("Racket data in rack[0]: ");
rack[0].show(); Dr. Kuppusamy P
Create an Array of objects and Initialize using a separate
member method
System.out.println("Racket data in rack[1]: ");
rack[1].show();
System.out.println("Racket data in rack[2]: ");
rack[2].show();
System.out.println("Racket data in rack[3]: ");
rack[3].show();
}
}
// Creating a Racket class with origin, price, color
class Rackets {
private String racktype;
private String origin;
private String color;
private int price;
Dr. Kuppusamy P
Create an Array of objects and Initialize using a separate member
method
Dr. Kuppusamy P
Super Keyword
• super keyword is a reference variable used to refer immediate parent class object.
• Whenever user create the instance of subclass, an instance of parent class is
created implicitly that is referred by super reference variable.
• It happens when a derived class and base class has same data members.
Usage of super keyword:
• To refer immediate parent class instance variable.
• To invoke immediate parent class method.
• To invoke immediate parent class constructor.
• The constructors of the superclass are never inherited by the subclass.
• This is the only exception to the rule that a subclass inherits all the properties of its
superclass
Dr. Kuppusamy P
Super Keyword for variables
class Vehicle
{
int maxSpeed = 120;
}
class Car extends Vehicle
{
int maxSpeed = 180;
void display()
{
System.out.println("Maximum Speed: " + super.maxSpeed);
}}
class Test
{
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}
Dr. Kuppusamy P
Super Keyword for methods
• class Person
• {
• void message()
• {
• System.out.println("This is person class");
• }}
• class Student extends Person
• {
• void message()
• {
• System.out.println("This is student class");
• }
• void display()
• {
• message();
• super.message(); //parent class method
• }}
• class Test
• {
• public static void main(String args[])
• {
• Student s = new Student();
• s.display();
• }}
Dr. Kuppusamy P
Invoke parent class constructor from child class constructor.
Not from another method.
class A
{
public A(int x)
{
System.out.println("Parent class constructor X value "+x);
}
}
class Sub extends A
{
public Sub(int x)
{
super(x);
}
}
Dr. Kuppusamy P
Superclass constructors are never inherited by subclass
class Parent {
public Parent()
{
}
}
Dr. Kuppusamy P