SlideShare a Scribd company logo
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.
Inheritance,single,multiple.access rulepptx
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
Inheritance,single,multiple.access rulepptx
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

More Related Content

PPTX
Inheritance in java
PPTX
Java - Inheritance_multiple_inheritance.pptx
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
Java inheritance
PPTX
Inheritance
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
PPTX
Inheritance Interface and Packags in java programming.pptx
PPTX
Unit3 inheritance
Inheritance in java
Java - Inheritance_multiple_inheritance.pptx
Inheritance in Java is a mechanism in which one object acquires all the prope...
Java inheritance
Inheritance
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Inheritance Interface and Packags in java programming.pptx
Unit3 inheritance

Similar to Inheritance,single,multiple.access rulepptx (20)

PDF
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
PDF
java inheritance that is used in oop cls
PPTX
Basics to java programming and concepts of java
PPTX
Unit3 part2-inheritance
PPTX
Inheritance in oop
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Chap-3 Inheritance.pptx
PPTX
Inheritance ppt
PPTX
PDF
Inheritance in Java.pdf
PPTX
Inheritance Slides
PPTX
Inheritance and Interfaces
PDF
Java programming -Object-Oriented Thinking- Inheritance
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
object oriented programming unit two ppt
PDF
Inheritance and interface
PPTX
Detailed_description_on_java_ppt_final.pptx
PPTX
java_inheritance_oop_20250730110153.pptx
PPTX
Inheritance in OOPs with java
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
java inheritance that is used in oop cls
Basics to java programming and concepts of java
Unit3 part2-inheritance
Inheritance in oop
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Modules 333333333³3444444444444444444.pptx
Chap-3 Inheritance.pptx
Inheritance ppt
Inheritance in Java.pdf
Inheritance Slides
Inheritance and Interfaces
Java programming -Object-Oriented Thinking- Inheritance
‏‏‏‏‏‏oop lecture objectives will come.pdf
object oriented programming unit two ppt
Inheritance and interface
Detailed_description_on_java_ppt_final.pptx
java_inheritance_oop_20250730110153.pptx
Inheritance in OOPs with java
Ad

More from ArunPatrick2 (20)

PPT
Introduction to HTML table,width,height.ppt
PPTX
introduction to java Multithreading presentation.pptx
PPT
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
PPTX
collectionsframework210616084411 (1).pptx
PPT
Interfaces implements,presentation in java.ppt
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPTX
InterfaceAbstractClass presentation.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
presentation-on-exception-handling-160611180456 (1).pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
java package java package in java packages
PPT
Interfaces in java.. introduction, classes, objects
PPTX
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
PPTX
Exception Handling,finally,catch,throw,throws,try.pptx
PPTX
Data Analytics overview,kDD process,mining Techniques.pptx
PPTX
Data warehouse-complete-1-100227093028-phpapp01.pptx
PPTX
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
PPTX
Difference between Abstract class and Interface.pptx
PPT
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
PPTX
InterfaceAbstractClass,interfaces,final keyword,.pptx
Introduction to HTML table,width,height.ppt
introduction to java Multithreading presentation.pptx
topic-6-Presentation e-commerce,B2B,B2C,C2C.ppt
collectionsframework210616084411 (1).pptx
Interfaces implements,presentation in java.ppt
multithreading,thread and processinjava-210302183809.pptx
InterfaceAbstractClass presentation.pptx
unit3 Exception Handling multithreadingppt.pptx
presentation-on-exception-handling-160611180456 (1).pptx
unit3multithreadingppt-copy-180122162204.pptx
java package java package in java packages
Interfaces in java.. introduction, classes, objects
javapackage,try,cthrow,finallytch,-160518085421 (1).pptx
Exception Handling,finally,catch,throw,throws,try.pptx
Data Analytics overview,kDD process,mining Techniques.pptx
Data warehouse-complete-1-100227093028-phpapp01.pptx
DataWarehouse Architecture,daat mining,data mart,etl process.pptx
Difference between Abstract class and Interface.pptx
finalkeywordinjava abstract,interface,implementation.-170702034453.ppt
InterfaceAbstractClass,interfaces,final keyword,.pptx
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Sports Quiz easy sports quiz sports quiz
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
Cell Types and Its function , kingdom of life
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Sports Quiz easy sports quiz sports quiz
STATICS OF THE RIGID BODIES Hibbelers.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Anesthesia in Laparoscopic Surgery in India
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Insiders guide to clinical Medicine.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india

Inheritance,single,multiple.access rulepptx

  • 1. INHERITANCE In Java presented by: Hannah Roseline .R Sri Ramakrishna college of arts &science.
  • 2. 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
  • 3. 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.
  • 4. Inheritance Syntax: class Parent { //code } class Child 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 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)
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. Syntax of Hierarchical inheritance: Class Food { //code } class Rice extends Food{ //code } class Fruit extends Food{ //code } Food Rice Fruit
  • 13. 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()
  • 14. 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
  • 15. 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.
  • 17. 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.
  • 18. 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
  • 20. 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
  • 21. 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(); } }