INHERITANCE
In Java
presented by:
Hannah Roseline .R
Sri Ramakrishna college of arts &science.
Inheritance:
In java inheritance is a mechanism which one object can get
all the properties and behaviours of a parent object. In
inheritance there is a relationship between two classes.
That’s why it is-a relationship.
For example: Bike, car, bus this
are vehicle. That’s why the
child of vehicle are bike,
car, bus and vehicle is the
parent of this objects.
Vehicle
Bike, car, bus
Java inheritance:
• The important of inheritance in java program is code
reusability. We can reuse the function of parent class at
child class.
• In java we use Extends keyword for inheritance.
• Subclass and Superclass in inheritance.
Inheritance Syntax:
class Parent
{
//code
}
class Child extends Parent
{
//code
}
Parent
Child
Simple program
class super {
Public void display()
{
System.out.println(“I am parent class”);
}
}
class sub extends super {
public static void main(String args[])
{
sub message = new sub()
message.display();
}
}
Super
I am from parent class
sub
Parent class
Child classs
Output: I am parent class
TYPES OF INHERITANCE:
 Single inheritance.
 Multiple inheritance.
 Multi-level inheritance.
 Hierarchical inheritance.
 Hybrid inheritance.
Note: In java Multiple inheritance is not supported.
SINGLE INHERITANCE:
Single inheritance is the most easy type to understand. In
single inheritance a class extends another only one class.
The child class inherit only parents class. The subclass
inherit the objects of superclass.
Example: A cat is a animal.
So a cat only can inherit the
objects of animal class.
Animal
(superclass)
Cat
(subclass)
SAMPLE PROGRAM OF SINGLE INHERITANCE :
class message_super {
Public void display()
{
System.out.println(“I am from superclass”);
}
}
class message_sub extends message_super {
public static void main(String args[])
{
Message_sub message = new message_sub()
Message.display();
}
}
Output: I am from superclass
message_super
I am from superclass
message_sub
MULTILEVEL INHERITANCE
When a class extends a class, and that class extends another
class and its object then it call multilevel inheritance.
Example: a class A is parent class.
Class B is inherit the
objects of class A. Another
one class c is child of class B.
class c can inherit the objects
of class C.
A
B
C
Base class
Intermediary
class
Child class
SAMPLE PROGRAM OF MULTILEVEL INHERITANCE :
class a {
int data= 15; }
class b extends a {
}
class c extends b {
public void display() {
System.out.println(“number is:”+data);
}
public static void main(String args[])
{
c num= new c()
num.display();
}
}
Output: number is: 15
a
Data=15
b
c
HIERARCHICAL INHERITANCE:
When one superclass can be inherited by more than one
subclass then it is called hierarchical inheritance.
Example: a class A is parent class.
Class B is inherit the
objects of class A. Another
one class c also can inherit the
objects of class A.
A
B C
Syntax of Hierarchical inheritance:
Class Food
{
//code
}
class Rice extends Food{
//code
}
class Fruit extends Food{
//code
}
Food
Rice Fruit
Inheritance Method overriding:
When a method is already in parent class but also declaring
in child class, then it is known as Method Overriding.
Note: But when it is ‘Final’ method it cannot overriding.
Example: A parent class of animal
has method ‘eat’. Also a child class
tiger has method ‘eat’.
Animal
eat()
Tiger
eat()
Sample program of method overriding:
class animal {
public void display() {
System.out.println(“I am animal”); }
}
class tiger extends animal {
public void display() {
System.out.println(“I am tiger”);
}
public static void main(String args[]) {
tiger t = new tiger();
t.display(); }
}
Output: I am tiger
Introduction:
Polymorphism
poly=many
morphism=forms
i.e one interface having many methods.
 Polymorphism simply means many forms.
It can be defined as one thing performing different
forms.
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.
 In other words, If subclass provides the specific implementation of the
method that has been provided by one of its parent class, it is known as
method overriding.
Usage of Java Method Overriding
 Method overriding is used to provide specific implementation of
a method that is already provided by its super class.
 Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
 method must have same name as in the parent class
 method must have same parameter as in the parent class.
SUPER KEYWORD IN JAVA
Super keyword is a reference variable that is used for refer parent class object.
Super Keyword is mainly used at three level in java
 At variable level
 At method level
 At constructor level
WHY USE SUPER KEYWORD IN JAVA?
Whenever inherit base class data into derived class it is chance to get
ambiguity(தெளிவின
்மை), because may be base class and derived class data
are same so to difference these data need to use super keyword
EXAMPLE OF SUPER KEYWORD
class Employee {
float salary=10000;
}
class HR extends Employee {
float salary=20000;
void display() {
System.out.println("Salary: "+super.salary);//print base class salary
}
}
class Supervarible {
public static void main(String[] args) {
HR obj=new HR();
obj.display();
}
}
Thank you

Inheritance,single,multiple.access rulepptx

  • 1.
    INHERITANCE In Java presented by: HannahRoseline .R Sri Ramakrishna college of arts &science.
  • 2.
    Inheritance: In java inheritanceis a mechanism which one object can get all the properties and behaviours of a parent object. In inheritance there is a relationship between two classes. That’s why it is-a relationship. For example: Bike, car, bus this are vehicle. That’s why the child of vehicle are bike, car, bus and vehicle is the parent of this objects. Vehicle Bike, car, bus
  • 3.
    Java inheritance: • Theimportant of inheritance in java program is code reusability. We can reuse the function of parent class at child class. • In java we use Extends keyword for inheritance. • Subclass and Superclass in inheritance.
  • 4.
    Inheritance Syntax: class Parent { //code } classChild extends Parent { //code } Parent Child
  • 5.
    Simple program class super{ Public void display() { System.out.println(“I am parent class”); } } class sub extends super { public static void main(String args[]) { sub message = new sub() message.display(); } } Super I am from parent class sub Parent class Child classs Output: I am parent class
  • 6.
    TYPES OF INHERITANCE: Single inheritance.  Multiple inheritance.  Multi-level inheritance.  Hierarchical inheritance.  Hybrid inheritance. Note: In java Multiple inheritance is not supported.
  • 7.
    SINGLE INHERITANCE: Single inheritanceis the most easy type to understand. In single inheritance a class extends another only one class. The child class inherit only parents class. The subclass inherit the objects of superclass. Example: A cat is a animal. So a cat only can inherit the objects of animal class. Animal (superclass) Cat (subclass)
  • 8.
    SAMPLE PROGRAM OFSINGLE INHERITANCE : class message_super { Public void display() { System.out.println(“I am from superclass”); } } class message_sub extends message_super { public static void main(String args[]) { Message_sub message = new message_sub() Message.display(); } } Output: I am from superclass message_super I am from superclass message_sub
  • 9.
    MULTILEVEL INHERITANCE When aclass extends a class, and that class extends another class and its object then it call multilevel inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c is child of class B. class c can inherit the objects of class C. A B C Base class Intermediary class Child class
  • 10.
    SAMPLE PROGRAM OFMULTILEVEL INHERITANCE : class a { int data= 15; } class b extends a { } class c extends b { public void display() { System.out.println(“number is:”+data); } public static void main(String args[]) { c num= new c() num.display(); } } Output: number is: 15 a Data=15 b c
  • 11.
    HIERARCHICAL INHERITANCE: When onesuperclass can be inherited by more than one subclass then it is called hierarchical inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c also can inherit the objects of class A. A B C
  • 12.
    Syntax of Hierarchicalinheritance: Class Food { //code } class Rice extends Food{ //code } class Fruit extends Food{ //code } Food Rice Fruit
  • 13.
    Inheritance Method overriding: Whena method is already in parent class but also declaring in child class, then it is known as Method Overriding. Note: But when it is ‘Final’ method it cannot overriding. Example: A parent class of animal has method ‘eat’. Also a child class tiger has method ‘eat’. Animal eat() Tiger eat()
  • 14.
    Sample program ofmethod overriding: class animal { public void display() { System.out.println(“I am animal”); } } class tiger extends animal { public void display() { System.out.println(“I am tiger”); } public static void main(String args[]) { tiger t = new tiger(); t.display(); } } Output: I am tiger
  • 15.
    Introduction: Polymorphism poly=many morphism=forms i.e one interfacehaving many methods.  Polymorphism simply means many forms. It can be defined as one thing performing different forms.
  • 17.
    Method Overriding inJava  If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.  In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding. Usage of Java Method Overriding  Method overriding is used to provide specific implementation of a method that is already provided by its super class.  Method overriding is used for runtime polymorphism Rules for Java Method Overriding  method must have same name as in the parent class  method must have same parameter as in the parent class.
  • 18.
    SUPER KEYWORD INJAVA Super keyword is a reference variable that is used for refer parent class object. Super Keyword is mainly used at three level in java  At variable level  At method level  At constructor level
  • 20.
    WHY USE SUPERKEYWORD IN JAVA? Whenever inherit base class data into derived class it is chance to get ambiguity(தெளிவின ்மை), because may be base class and derived class data are same so to difference these data need to use super keyword
  • 21.
    EXAMPLE OF SUPERKEYWORD class Employee { float salary=10000; } class HR extends Employee { float salary=20000; void display() { System.out.println("Salary: "+super.salary);//print base class salary } } class Supervarible { public static void main(String[] args) { HR obj=new HR(); obj.display(); } }
  • 22.