Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Inheritance in Java

Inheritance in Java allows classes to inherit features from other classes. There are several types of inheritance including single, multilevel, multiple, and hierarchical. Single inheritance involves a child class extending one parent class. Multilevel inheritance extends the concept to allow a class to inherit from another derived class, forming a chain. Multiple inheritance is not supported in Java as it can cause conflicts, but multilevel and hierarchical inheritance are both supported.

Uploaded by

Prince Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Inheritance in Java

Inheritance in Java allows classes to inherit features from other classes. There are several types of inheritance including single, multilevel, multiple, and hierarchical. Single inheritance involves a child class extending one parent class. Multilevel inheritance extends the concept to allow a class to inherit from another derived class, forming a chain. Multiple inheritance is not supported in Java as it can cause conflicts, but multilevel and hierarchical inheritance are both supported.

Uploaded by

Prince Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

INHERITANCE IN JAVA

Inheritance in Java

• Mechanism of deriving new class from old class.


The old class is known as-
Base Class / Super class / Parent Class
• The new class is known as-
Derived class/ Sub Class / Child class
Types:
 Single Inheritance
 Multilevel Inheritance
 Multiple inheritance
 Hierarchical Inheritance
Inheritance In Java
  Single inheritance -
 When a class extends another one class only then we
call it a single inheritance.
 The below flow diagram shows that class B extends
only one class which is A.
 Here A is a parent class of B and B would be  a child
class of A.
Single Inheritance example program in Java
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
Sub Class Constructor
• A subclass constructor is used to construct the instance
variables of both the subclass and the superclass.
• The subclass constructor uses the keyword super to
invoke the constructor method of superclass.
• The super Keyword is used with following Conditions
super may only be used within a subclass constructor.
The call to super must appear as first statement.
Parameters in the super call must match with
declaration in superclass.
class Vehicle
{    
Vehicle()
{
System.out.println("Vehicle is created");
}
}
  class Bike extends Vehicle{
Bike()
{
super(); //will invoke parent class constructor     
System.out.println("Bike is created");    
}  
public static void main(String args[]){     
Bike b=new Bike();
}

“Multiple Inheritance”
• “Multiple Inheritance” refers to the concept
of one class extending (Or inherits) more than
one base class.
• The problem with “multiple inheritance” is
that the derived class will have to manage the
dependency on two base classes.
Multilevel Inheritance
• Multilevel inheritance refers to a mechanism
in OO technology where one can inherit from
a derived class, thereby making this derived
class the base class for the new class.
• As you can see in below flow diagram C is
subclass or child class of B and B is a child class
of A. A

You might also like