KEMBAR78
Ch5Inheritance.pptx Java program Java file | PPTX
Object Oriented Programming Using Java
INHERITANCE
School of Computer Engineering
KIIT Deemed to be University
Dr.Sunil Kumar Gouda
Asst. Professor
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Inheriatnce
 Inheritance is the process by which one class can acquire the properties
of another class.
 The class, from which properties are inherited to another class, is known
as super class.
 The class, to which properties are inherited from an existing class, is
known as sub class.
 A sub class inherits all non private variables and methods defined by the
super class and add its own, unique elements.
 To derive a sub class from super class, we use extends keyword.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Inheritance Cont..
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Advantages of Inheritance
Inheritance allows the creation of hierarchical classifications of classes.
It allows code reusability feature.
The sub class extends the properties of super classes to create more
dominant objects.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Syntax of Inheritance
The syntax of a class declaration that inherits a super class is :
class subclass-name extends superclass-name
{
// body of the class
}
Point to Remember:
A sub class can access all the public and protected members of a super class,
not the private members. (By default, the variable or functions of a class are
public in nature)
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Types of Inheritance
Java supports following types of inheritance:
Single Inheritance
Hierarchical Inheritance
Multiple Inheritance (not supported using class, but supported through
interface)
Multilevel Inheritance
Hybrid Inheritance ( only which not contain Multiple Inheritance)
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Single Inheritance
In single inheritance, we have only one super class and one sub class.
The subclass inherits properties from the only super class.
 Syntax of single inheritance:
class superClass
{
// Body of super class
}
class subClass extends superClass
{
// Body of sub class
}
Super
Class
Sub
Class
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Single Inheritance (Example)
class Rectangle
{
int l, b;
void disp_lb()
{
System.out.println("l= " +l+", b="+b);
}
}
class Cuboid extends Rectangle
{
int h;
void disp_h()
{
System.out.println("h= " +h);
}
void find_volume()
{
System.out.println ("Volume= "+l*b*h);
}
}
class SingleInheritance
{
public static void main(String args[])
{
Cuboid c=new Cuboid();
c.l=9;
c.b=7;
c.h=3;
c.disp_lb();
c.disp_h();
c.find_volume();
}
}
Output:
l=9, b=7
h=3
Volume=189
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Hierachical Inheritance
In hierarchical inheritance, two or more subclass inherits properties from
a single super class.
 Syntax of hierarchical inheritance:
class superClass
{
// Body of super class
}
class subclass1 extends superClass
{
// body of sub class 1
}
class subclass2 extends superClass
{
// body of sub class 2
}
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Hierachical Inheritance (Example)
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class HierarchicalInheritance
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}
}
Output:
meowing...
eating...
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Multiple Inheritance
 In multiple inheritance, one subclass inherits properties from two or more
super class. Multiple inheritance is not supported in java.
 Syntax of multiple inheritance:
class super1
{
// Body of super class 1
}
class super2
{
// Body of super class 2
}
// Illegal
class subclass extends super1, super2
{
// Body of sub class
}
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Multiple Inheritance Cont..
 In Java, multiple inheritance is not supported using classes due to
ambiguity problem. Ambiguity is a situation where compiler cannot
predicate to inherit which field out of identical fields present in multiple
super classes.
 But same can be achieved using the concept of interface.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Multilevel Inheritance
 In multi level inheritance, number of classes are represented at different
levels.
 Syntax of multilevel inheritance:
class superClass1
{
// Body of super class 1
}
class superClass2 extends superClass1
{
// body of super class 2
}
class subClass extends superClass2
{
// body of sub class
}
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Multilevel Inheritance (Example)
class Shape
{
public void display()
{
System.out.println("Inside display");
}
}
class Rectangle extends Shape
{
public void area()
{
System.out.println("Inside area");
}
}
class Cube extends Rectangle
{
public void volume()
{
System.out.println("Inside volume");
}
}
public class Tester
{
public static void main(String[] arguments)
{
Cube cube = new Cube();
cube.display();
cube.area();
cube.volume();
}
}
Output:
Inside display
Inside area
Inside volume
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Hybrid Inheritance
 It is the combination of all the above inheritance.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
How constructor of a super class gets called ?
 A subclass constructor can call or invoke the constructor of its super
class in two ways:
 A subclass constructor invokes the default or non parameterized
constructor of its super class implicitly or automatically.
 A subclass constructor can invoke the parameterized constructor of its
super class explicitly by using the “super” statement.
 Note: In a class hierarchy, constructors are called in order of derivation,
from super class to subclass.
 super() must be the first statement to be executed in a subclass
constructor to call the parameterized constructor of super class.
 If super() is not used, then the default constructor of super class will be
called.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Invoking default constructor of the super class
class A
{
A()
{
System.out.println("Inside A's
constructor.");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's
constructor.");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's constructor.");
}
}
class CallingCons
{
public static void main(String args[])
{
C c = new C();
}
}
Output:
Inside A’s constructor.
Inside B’s constructor.
Inside C’s constructor.
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
Invoking parameterized constructor of the super class
class Rect
{
int length, breadth;
Rect (int l, int b)
{
length=l;
breadth=b;
}
}
class Cuboid extends Rect
{
double height;
Cuboid (int l, int b, int h)
{
super(l, b);
height=h;
}
void volume()
{
System.out.println (length*breadth*height);
}
}
class Vol
{
public static void main(String args[])
{
Cuboid v=new Cuboid(1, 2, 3);
v.volume();
}
}
Output:
6.0
01/25/2025 Java Programming, Dr.Sunil Kumar Gouda
END OF THE CHAPTER

Ch5Inheritance.pptx Java program Java file

  • 1.
    Object Oriented ProgrammingUsing Java INHERITANCE School of Computer Engineering KIIT Deemed to be University Dr.Sunil Kumar Gouda Asst. Professor
  • 2.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Inheriatnce  Inheritance is the process by which one class can acquire the properties of another class.  The class, from which properties are inherited to another class, is known as super class.  The class, to which properties are inherited from an existing class, is known as sub class.  A sub class inherits all non private variables and methods defined by the super class and add its own, unique elements.  To derive a sub class from super class, we use extends keyword.
  • 3.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Inheritance Cont..
  • 4.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Advantages of Inheritance Inheritance allows the creation of hierarchical classifications of classes. It allows code reusability feature. The sub class extends the properties of super classes to create more dominant objects.
  • 5.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Syntax of Inheritance The syntax of a class declaration that inherits a super class is : class subclass-name extends superclass-name { // body of the class } Point to Remember: A sub class can access all the public and protected members of a super class, not the private members. (By default, the variable or functions of a class are public in nature)
  • 6.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Types of Inheritance Java supports following types of inheritance: Single Inheritance Hierarchical Inheritance Multiple Inheritance (not supported using class, but supported through interface) Multilevel Inheritance Hybrid Inheritance ( only which not contain Multiple Inheritance)
  • 7.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Single Inheritance In single inheritance, we have only one super class and one sub class. The subclass inherits properties from the only super class.  Syntax of single inheritance: class superClass { // Body of super class } class subClass extends superClass { // Body of sub class } Super Class Sub Class
  • 8.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Single Inheritance (Example) class Rectangle { int l, b; void disp_lb() { System.out.println("l= " +l+", b="+b); } } class Cuboid extends Rectangle { int h; void disp_h() { System.out.println("h= " +h); } void find_volume() { System.out.println ("Volume= "+l*b*h); } } class SingleInheritance { public static void main(String args[]) { Cuboid c=new Cuboid(); c.l=9; c.b=7; c.h=3; c.disp_lb(); c.disp_h(); c.find_volume(); } } Output: l=9, b=7 h=3 Volume=189
  • 9.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Hierachical Inheritance In hierarchical inheritance, two or more subclass inherits properties from a single super class.  Syntax of hierarchical inheritance: class superClass { // Body of super class } class subclass1 extends superClass { // body of sub class 1 } class subclass2 extends superClass { // body of sub class 2 }
  • 10.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Hierachical Inheritance (Example) class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class HierarchicalInheritance { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); } } Output: meowing... eating...
  • 11.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Multiple Inheritance  In multiple inheritance, one subclass inherits properties from two or more super class. Multiple inheritance is not supported in java.  Syntax of multiple inheritance: class super1 { // Body of super class 1 } class super2 { // Body of super class 2 } // Illegal class subclass extends super1, super2 { // Body of sub class }
  • 12.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Multiple Inheritance Cont..  In Java, multiple inheritance is not supported using classes due to ambiguity problem. Ambiguity is a situation where compiler cannot predicate to inherit which field out of identical fields present in multiple super classes.  But same can be achieved using the concept of interface.
  • 13.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Multilevel Inheritance  In multi level inheritance, number of classes are represented at different levels.  Syntax of multilevel inheritance: class superClass1 { // Body of super class 1 } class superClass2 extends superClass1 { // body of super class 2 } class subClass extends superClass2 { // body of sub class }
  • 14.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Multilevel Inheritance (Example) class Shape { public void display() { System.out.println("Inside display"); } } class Rectangle extends Shape { public void area() { System.out.println("Inside area"); } } class Cube extends Rectangle { public void volume() { System.out.println("Inside volume"); } } public class Tester { public static void main(String[] arguments) { Cube cube = new Cube(); cube.display(); cube.area(); cube.volume(); } } Output: Inside display Inside area Inside volume
  • 15.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Hybrid Inheritance  It is the combination of all the above inheritance.
  • 16.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda How constructor of a super class gets called ?  A subclass constructor can call or invoke the constructor of its super class in two ways:  A subclass constructor invokes the default or non parameterized constructor of its super class implicitly or automatically.  A subclass constructor can invoke the parameterized constructor of its super class explicitly by using the “super” statement.  Note: In a class hierarchy, constructors are called in order of derivation, from super class to subclass.  super() must be the first statement to be executed in a subclass constructor to call the parameterized constructor of super class.  If super() is not used, then the default constructor of super class will be called.
  • 17.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Invoking default constructor of the super class class A { A() { System.out.println("Inside A's constructor."); } } class B extends A { B() { System.out.println("Inside B's constructor."); } } class C extends B { C() { System.out.println("Inside C's constructor."); } } class CallingCons { public static void main(String args[]) { C c = new C(); } } Output: Inside A’s constructor. Inside B’s constructor. Inside C’s constructor.
  • 18.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda Invoking parameterized constructor of the super class class Rect { int length, breadth; Rect (int l, int b) { length=l; breadth=b; } } class Cuboid extends Rect { double height; Cuboid (int l, int b, int h) { super(l, b); height=h; } void volume() { System.out.println (length*breadth*height); } } class Vol { public static void main(String args[]) { Cuboid v=new Cuboid(1, 2, 3); v.volume(); } } Output: 6.0
  • 19.
    01/25/2025 Java Programming,Dr.Sunil Kumar Gouda END OF THE CHAPTER