SlideShare a Scribd company logo
Inheritance
Types of Inheritance
Super Keyword
Method Overriding
Inheritance
• Inheritance is a mechanism wherein a new class is derived from an
existing class. In Java, classes may inherit or acquire the properties and
methods of other classes.
• A class derived from another class is called a subclass
• Where as the class from which a subclass is derived is called a super class
• A subclass can have only one superclass
• whereas a superclass may have one or more subclasses.
Use of Inheritance
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
Types of inheritance supported in java
Single level inheritance
Multi level inheritance
Hierarchal inheritance
Single level inheritance
• When a class extends another one class only then we call it a single
inheritance.
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extendsClass_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
public class inheritance
{ public static void main(String[] y)
{ // with object of b class a class methods can be accessed
Class_B b=new Class_B();
b.method_B();
b.method_A();
}
}
Multilevel inheritance
• Multilevel inheritance refers to a mechanism in OO technology where one
class can inherit from a derived class, thereby making this derived class the
base class for the new class
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extends Class_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
class Class_C extends Class_B
{ void method_C()
{ System.out.println("the Class_C method_C a called"); }
}
public class inheritance
{ public static void main(String[] y)
{
/*
* with object of b class a class methods can be accessed
*/
Class_C C=new Class_C();
C.method_C();
C.method_B();
C.method_A();
}
}
Hierarchical Inheritance
• In such kind of inheritance one class is inherited by many sub classes.
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extends Class_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
class Class_C extends Class_A
{ void method_C()
{ System.out.println("the Class_C method_C a called"); }
}
public class inheritance
{ public static void main(String[] y)
{
/*
* with object of b class a class methods can be accessed
*/
Class_C C=new Class_C();
C.method_A();
C.method_C();
Class_B B=new Class_B();
B.method_A();
B.method_B();
}
}
Super Keyword
• super is used to serve following two objectives:
1) Accessing super class members (Methods and Variables).
2) Calling super class Constructor.
/** accessing super class methods and variables */
class Class_A
{ int int_a=100;
void demo()
{ System.out.println("hello world"); }
}
class Class_B extends Class_A
{ int int_a=200;
void demo_1()
{ System.out.println("the value of int_a is"+super.int_a);
super.demo();
}
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B();
b.demo_1();
}
}
/** accessing super class Constructor */
class Class_A
{ int int_a;
Class_A()
{ int_a=100;
System.out.println("the int_a value is "+int_a);
}
}
class Class_B extends Class_A
{ Class_B()
{ super(); }//calls super class constructor
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B(); }
}
Method Overriding
• Child class has the same method as of base class. In such cases child class
overrides the parent class method without even touching the source code
of the base class. This feature is known as method overriding.
/* * Method overriding */
class Class_A
{ void demo()
{ System.out.println("the class A method called"); }
}
class Class_B extends Class_A
{ void demo()
{ System.out.println("the class B method called"); }
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B();
b.demo();
}
}
Inheritance

More Related Content

PPTX
Inheritance
PPT
Inheritance polymorphism-in-java
PDF
itft-Inheritance in java
PPTX
Java inheritance
PPT
Csharp4 inheritance
PPTX
Dynamic method dispatch
PPTX
Ppt on this and super keyword
PPT
Java Programming - Inheritance
Inheritance
Inheritance polymorphism-in-java
itft-Inheritance in java
Java inheritance
Csharp4 inheritance
Dynamic method dispatch
Ppt on this and super keyword
Java Programming - Inheritance

What's hot (20)

PPSX
Seminar on java
PPS
Inheritance chepter 7
PPTX
Java Inheritance
PPTX
Inheritance in Java
PPTX
Inheritance and Polymorphism Java
PPTX
Inheritance ppt
PPTX
Inheritance In Java
PPT
Java: Inheritance
PPT
Inheritance in java
PPTX
Inheritance in java
PPTX
Multiple inheritance possible in Java
PDF
Inheritance In Java
PPT
L7 inheritance
PDF
javainheritance
PDF
java-06inheritance
PPTX
Inheritance in java
PPTX
Inheritance in Java
PPTX
Inheritance
PPT
Java inheritance
Seminar on java
Inheritance chepter 7
Java Inheritance
Inheritance in Java
Inheritance and Polymorphism Java
Inheritance ppt
Inheritance In Java
Java: Inheritance
Inheritance in java
Inheritance in java
Multiple inheritance possible in Java
Inheritance In Java
L7 inheritance
javainheritance
java-06inheritance
Inheritance in java
Inheritance in Java
Inheritance
Java inheritance
Ad

Similar to Inheritance (20)

PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Inheritance & interface ppt Inheritance
PPTX
Inheritance Slides
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
Java - Inheritance_multiple_inheritance.pptx
PPTX
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PPTX
Unit3 part2-inheritance
PPTX
Unit3 inheritance
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Inheritance in oop
PPTX
Inheritance in JAVA PPT
PDF
IRJET- Inheritance in Java
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
PPTX
Inheritance in java
PPTX
Java Inheritance
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
Inheritance and Interfaces
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Inheritance,single,multiple.access rulepptx
Inheritance & interface ppt Inheritance
Inheritance Slides
Modules 333333333³3444444444444444444.pptx
Inheritance in Java is a mechanism in which one object acquires all the prope...
Java - Inheritance_multiple_inheritance.pptx
OOPS_Unit2.inheritance and interface objected oriented programming
Unit3 part2-inheritance
Unit3 inheritance
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in oop
Inheritance in JAVA PPT
IRJET- Inheritance in Java
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Inheritance in java
Java Inheritance
‏‏‏‏‏‏oop lecture objectives will come.pdf
Inheritance and Interfaces
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Ad

More from yugandhar vadlamudi (14)

ODP
Toolbarexample
ODP
Singleton pattern
PPT
Object Relational model for SQLIite in android
DOCX
JButton in Java Swing example
PPTX
Collections framework in java
PPTX
Packaes & interfaces
PPTX
Exception handling in java
DOCX
JMenu Creation in Java Swing
DOCX
Adding a action listener to button
PPTX
Operators in java
PPTX
Control flow statements in java
PPTX
Closer look at classes
PPTX
java Applet Introduction
PPTX
Class introduction in java
Toolbarexample
Singleton pattern
Object Relational model for SQLIite in android
JButton in Java Swing example
Collections framework in java
Packaes & interfaces
Exception handling in java
JMenu Creation in Java Swing
Adding a action listener to button
Operators in java
Control flow statements in java
Closer look at classes
java Applet Introduction
Class introduction in java

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Basic Mud Logging Guide for educational purpose
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
01-Introduction-to-Information-Management.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Pharma ospi slides which help in ospi learning
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Renaissance Architecture: A Journey from Faith to Humanism
PPH.pptx obstetrics and gynecology in nursing
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Insiders guide to clinical Medicine.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
Basic Mud Logging Guide for educational purpose
102 student loan defaulters named and shamed – Is someone you know on the list?
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
01-Introduction-to-Information-Management.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Final Presentation General Medicine 03-08-2024.pptx
Sports Quiz easy sports quiz sports quiz
Pharma ospi slides which help in ospi learning
Microbial disease of the cardiovascular and lymphatic systems
STATICS OF THE RIGID BODIES Hibbelers.pdf

Inheritance

  • 1. Inheritance Types of Inheritance Super Keyword Method Overriding
  • 2. Inheritance • Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. • A class derived from another class is called a subclass • Where as the class from which a subclass is derived is called a super class • A subclass can have only one superclass • whereas a superclass may have one or more subclasses. Use of Inheritance • For Method Overriding (so runtime polymorphism can be achieved). • For Code Reusability.
  • 3. Types of inheritance supported in java Single level inheritance Multi level inheritance Hierarchal inheritance
  • 4. Single level inheritance • When a class extends another one class only then we call it a single inheritance. class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extendsClass_A { void method_B() { System.out.println("the Class_b method_B a called"); } } public class inheritance { public static void main(String[] y) { // with object of b class a class methods can be accessed Class_B b=new Class_B(); b.method_B(); b.method_A(); } }
  • 5. Multilevel inheritance • Multilevel inheritance refers to a mechanism in OO technology where one class can inherit from a derived class, thereby making this derived class the base class for the new class class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extends Class_A { void method_B() { System.out.println("the Class_b method_B a called"); } } class Class_C extends Class_B { void method_C() { System.out.println("the Class_C method_C a called"); } } public class inheritance { public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_C(); C.method_B(); C.method_A(); } }
  • 6. Hierarchical Inheritance • In such kind of inheritance one class is inherited by many sub classes. class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extends Class_A { void method_B() { System.out.println("the Class_b method_B a called"); } } class Class_C extends Class_A { void method_C() { System.out.println("the Class_C method_C a called"); } } public class inheritance { public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_A(); C.method_C(); Class_B B=new Class_B(); B.method_A(); B.method_B(); } }
  • 7. Super Keyword • super is used to serve following two objectives: 1) Accessing super class members (Methods and Variables). 2) Calling super class Constructor. /** accessing super class methods and variables */ class Class_A { int int_a=100; void demo() { System.out.println("hello world"); } } class Class_B extends Class_A { int int_a=200; void demo_1() { System.out.println("the value of int_a is"+super.int_a); super.demo(); } } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); b.demo_1(); } }
  • 8. /** accessing super class Constructor */ class Class_A { int int_a; Class_A() { int_a=100; System.out.println("the int_a value is "+int_a); } } class Class_B extends Class_A { Class_B() { super(); }//calls super class constructor } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); } }
  • 9. Method Overriding • Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding. /* * Method overriding */ class Class_A { void demo() { System.out.println("the class A method called"); } } class Class_B extends Class_A { void demo() { System.out.println("the class B method called"); } } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); b.demo(); } }