0% found this document useful (0 votes)
27 views20 pages

Unit 3.1-Inheritance

Java

Uploaded by

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

Unit 3.1-Inheritance

Java

Uploaded by

Aryan Gireesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Inheritance

Inheritance:

Inheritance is a mechanism in which new class is derived from


existing class.

Syntax of 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.
The meaning of "extends" is to increase the functionality.

Class which is inherited is called parent or super class


Class which is derived from parent class is called child or subclass.
Examples:
Why use inheritance in java?

❖For Method Overriding (so runtime polymorphism can be achieved).

❖For code reusability.


Examples:

class Vehicle.
{ ......

}
class Car extends Vehicle
{
....... //extends the property of vehicle class.

Vehicle is Super class and Car is subclass.


Types of inheritance in java
Single Inheritance : In single inheritance, subclasses inherit the features
of one superclass. class A serves as a base class for the derived class B.
Multilevel Inheritance :
In Multilevel Inheritance, a derived class will be inheriting a base class
and as well as the derived class also act as the base class to other class.
the class A serves as a base class for the derived class B, which in turn
serves as a base class for the derived class C. In Java, a class cannot
directly access the grandparent’s members.
Hierarchical Inheritance :
In Hierarchical Inheritance, one class serves as a superclass (base class)
for more than one sub class.In below image, the class A serves as a base
class for the derived class B,C and D.
Multiple Inheritance (Through Interfaces) :
In Multiple inheritance ,one class can have more than one superclass and inherit
features from all parent classes. Please note that Java does not support multiple
inheritance with classes. In java, we can achieve multiple inheritance only
through Interfaces. In image below, Class C is derived from interface A and B.
Hybrid Inheritance(Through Interfaces) :
It is a mix of two or more of the above types of inheritance. Since java doesn’t
support multiple inheritance with classes, the hybrid inheritance is also not
possible with classes. In java, we can achieve hybrid inheritance only through
Interfaces.
super keyword in java
The super keyword in java is a reference variable which is used to refer
immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is


created implicitly which is referred by super reference variable.
Usage of java super Keyword

super can be used to refer immediate parent class instance variable.

super can be used to invoke immediate parent class method.

super() can be used to invoke immediate parent class constructor.


1) super is used to refer immediate parent class instance variable.

We can use super keyword to access the data member or field of parent class. It
is used if parent class and child class have same fields.
class Animal
{
String type="mammal";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
System.out.println("Dog color:"+color);
System.out.println("dog type: "+super.type);
}
}
class testsuper1
{
public static void main(String args[])
{
Dog d=new Dog();
d.printColor();
}
}
2.super can be used to invoke parent class
method
The super keyword can also be
used to invoke parent class method.
It should be used if subclass
contains the same method as parent
class. In other words, it is used if
method is overridden.
Examples:

class Animal
{
void eat()
{
System.out.println("Animal is eating...");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog is eating bread...");
}
void bark()
{
System.out.println("barking...");
}
void work()
{

super.eat();// Animal class method eat() implemented.


eat();
bark();

}
}
class testsuper2
{
public static void main(String args[])
{
Dog d=new Dog();
d.work();
}
}
3)super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent


class constructor.

Example:
class applicant
{
String name,city;
applicant(String n,String c)
{
name=n;
city=c;
}
void dispapp()
{
System.out.println("Name: "+name+"\nCity: "+city);
}
}
class employee extends applicant
{
int empid,deptid;
employee(String n,String c,int e,int d)
{
super(n,c);
empid=e;
deptid=d;
}
void dispemp()
{
System.out.println("Emp Id: "+empid);
System.out.println("Dept Id: "+deptid+"\n");
}

}
class testsuper3
{
public static void main(String args[])
{
employee e1=new employee("Rahul","Nashik",101,111);
e1.dispapp();
e1.dispemp();
employee e2=new employee("Rohan","Pune",102,112);
e2.dispapp();
e2.dispemp();
}
}

You might also like