SlideShare a Scribd company logo
Java Inheritance
There are two ways to reuse the existing classes
Composition
define a new class, which is composed of existing classes.
Composition exhibits a "has-a" relationship.
Inheritance
derive a new class based on an existing class
with modifications / extensions.
Inheritance exhibits a “is-a" relationship.
2
Inheritance: Definition
 inheritance: a parent-child relationship between classes
 Reuse of existing code or Method Overriding (Runtime Polymorphism).
 allows sharing of the behavior of the parent class into its child classes
 child class can add new behavior / override existing behavior from parent
 The more general class is called:
 superclass, base class, parent class
 The more specific class is called:
 subclass, derived class, child class
3
 Two kinds:
 implementation: the code that defines methods.
 Derived class inherits the implementations of all methods
from base class.
 interface: the method prototypes only.
 Accessing superclass methods from derived class.
 Can use super() to access all (non-private) superclass methods.
 Can use super() to call base class constructor.
Syntax of Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
keyword extends:
Indicates that you are making a new class that derives from an existing class.
Types of Inheritance:
On the basis of class, there can be three types of inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple & Hybrid - supported through interface only
Java inheritance
Java inheritance
Why multiple inheritance is not supported in java?
class A
{
void msg(){System.out.println("Hello");}
}
class B
{
void msg(){System.out.println("Welcome");}
}
class C extends A,B
{ //suppose if it were
Public Static void main(String args[]){
C obj=new C();
obj.msg(); //Now which msg() method would be invoked?
}
}
super keyword
The super is a reference variable that is used to refer immediate
parent class object.
Usage of super Keyword
super is used to refer immediate parent class instance variable.
super() is used to invoke immediate parent class constructor.
super is used to invoke immediate parent class method.
Example:
class Vehicle
{
int speed=50;
}
class Bike extends Vehicle
{
int speed=100;
void display()
{
System.out.println(speed);
//will print speed of Bike
}
public static void main(String args[])
{
Bike b=new Bike();
b.display();
}
}
class Vehicle
{
int speed=50;
}
class Bike extends Vehicle
{
int speed=100;
void display()
{
System.out.println(super.speed);
//will print speed of Vehicle now
}
public static void main(String args[])
{
Bike b=new Bike();
b.display();
}
}
super is used to invoke parent class constructor.
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();
}}
super can be used to invoke parent class method.
class Person
{
void message(){System.out.println("welcome");}
}
class Student extends Person
{
void message() { System.out.println("welcome to java");
}
void display()
{
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[])
{
Student s=new Student();
s.display();}}
Method Overriding in Java
Having the same method in the subclass as declared in the parent
class is known as method overriding.
In other words, If subclass provides the specific implementation of
the method i.e. already provided by its parent class, it is known as
Method Overriding.
Method Overriding is used for Runtime Polymorphism
Rules for Method Overriding:
method must have same name as in the parent class
method must have same parameter as in the parent class.
must be inheritance (IS-A) relationship.
class Vehicle
{
void run()
{
System.out.println("Vehicle");
}
}
class Bike extends Vehicle
{
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
}
class Vehicle
{
void run()
{
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike is running");
}
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
Method Overloading Method Overriding
1) Method overloading is used to
increase the readability of the program.
Method overriding is used to provide
the specific implementation of the
method that is already provided by its
super class.
2) method overloading is performed
within a class.
Method overriding occurs in two
classes that have IS-A relationship.
3) In case of method overloading
parameter must be different.
In case of method overriding parameter
must be same.

More Related Content

PPTX
Inheritance in java
yash jain
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPTX
Interface in java
PhD Research Scholar
 
PDF
Polymorphism In Java
Spotle.ai
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Polymorphism
Arif Ansari
 
PPTX
Method Overloading in Java
Sonya Akter Rupa
 
Inheritance in java
yash jain
 
Inheritance In Java
Darpan Chelani
 
Interface in java
PhD Research Scholar
 
Polymorphism In Java
Spotle.ai
 
Inheritance in java
Lovely Professional University
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Polymorphism
Arif Ansari
 
Method Overloading in Java
Sonya Akter Rupa
 

What's hot (20)

PPTX
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
PPTX
Inheritance in java
Tech_MX
 
PPT
Abstract class
Tony Nguyen
 
PPTX
JDBC ppt
Rohit Jain
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPTX
Exception handling
PhD Research Scholar
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PDF
input/ output in java
sharma230399
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PPTX
java interface and packages
VINOTH R
 
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
PPTX
Friend function
zindadili
 
PPT
C# Method overloading
Prem Kumar Badri
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PPT
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
PPTX
Polymorphism in java
Elizabeth alexander
 
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
Inheritance in java
Tech_MX
 
Abstract class
Tony Nguyen
 
JDBC ppt
Rohit Jain
 
Inheritance in OOPS
Ronak Chhajed
 
Exception handling
PhD Research Scholar
 
07. Virtual Functions
Haresh Jaiswal
 
Java Thread Synchronization
Benj Del Mundo
 
input/ output in java
sharma230399
 
Java exception handling
BHUVIJAYAVELU
 
Inheritance
Sapna Sharma
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
java interface and packages
VINOTH R
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
cprogrammings
 
Friend function
zindadili
 
C# Method overloading
Prem Kumar Badri
 
MULTI THREADING IN JAVA
VINOTH R
 
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Polymorphism in java
Elizabeth alexander
 
Ad

Viewers also liked (18)

PDF
Java Inheritance
Rosie Jane Enomar
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Advanced Object-Oriented/SOLID Principles
Jon Kruger
 
PPTX
Object Oriented Design SOLID Principles
rainynovember12
 
PPTX
S.O.L.I.D. Principles for Software Architects
Ricardo Wilkins
 
PPT
SOLID Design Principles
Andreas Enbohm
 
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Isuru Perera
 
PDF
Lesson 1 • Elements & Principles of Design and Art
Marcio Sargento
 
PDF
Iot data aggrigation
dokechin
 
PPT
Java: Inheritance
Tareq Hasan
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
KEY
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
PPTX
Scanner
at1211
 
PPT
Hierarchical Object Oriented Design
sahibsahib
 
PPT
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PPTX
Constructor ppt
Vinod Kumar
 
PPT
C++ Inheritance
Jussi Pohjolainen
 
Java Inheritance
Rosie Jane Enomar
 
Java inheritance
Arati Gadgil
 
Advanced Object-Oriented/SOLID Principles
Jon Kruger
 
Object Oriented Design SOLID Principles
rainynovember12
 
S.O.L.I.D. Principles for Software Architects
Ricardo Wilkins
 
SOLID Design Principles
Andreas Enbohm
 
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Isuru Perera
 
Lesson 1 • Elements & Principles of Design and Art
Marcio Sargento
 
Iot data aggrigation
dokechin
 
Java: Inheritance
Tareq Hasan
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
Scanner
at1211
 
Hierarchical Object Oriented Design
sahibsahib
 
Constructor & Destructor
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Constructor ppt
Vinod Kumar
 
C++ Inheritance
Jussi Pohjolainen
 
Ad

Similar to Java inheritance (20)

PPTX
Inheritance ppt
Nivegeetha
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PPTX
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
Inheritance
yugandhar vadlamudi
 
PPTX
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PDF
Inheritance and interface
Shubham Sharma
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PPTX
Inheritance Slides
Ahsan Raja
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
PPTX
Java
Haripritha
 
PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PPTX
Java(inheritance)
Pooja Bhojwani
 
PPT
L7 inheritance
teach4uin
 
PPT
L7 inheritance
teach4uin
 
PPTX
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
PPTX
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
Inheritance ppt
Nivegeetha
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Inheritance
yugandhar vadlamudi
 
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Inheritance and interface
Shubham Sharma
 
Unit3 part2-inheritance
DevaKumari Vijay
 
Inheritance Slides
Ahsan Raja
 
Unit3 inheritance
Kalai Selvi
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Java(inheritance)
Pooja Bhojwani
 
L7 inheritance
teach4uin
 
L7 inheritance
teach4uin
 
Inheritance Interface and Packags in java programming.pptx
Prashant416351
 
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
Inheritance & interface ppt Inheritance
narikamalliy
 

More from BHUVIJAYAVELU (8)

PPT
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
BHUVIJAYAVELU
 
PPT
Lecture no1
BHUVIJAYAVELU
 
PPTX
Java arrays
BHUVIJAYAVELU
 
PPTX
Hybrid m-a-t
BHUVIJAYAVELU
 
PPTX
Java interface
BHUVIJAYAVELU
 
PPTX
Java packages
BHUVIJAYAVELU
 
PPT
Flow control and error control
BHUVIJAYAVELU
 
PPT
3 2--power-aware-cloud
BHUVIJAYAVELU
 
Eprojectprojectfinalreportgsmmonitoringcontrollingofdevicesusinggsm 090811012...
BHUVIJAYAVELU
 
Lecture no1
BHUVIJAYAVELU
 
Java arrays
BHUVIJAYAVELU
 
Hybrid m-a-t
BHUVIJAYAVELU
 
Java interface
BHUVIJAYAVELU
 
Java packages
BHUVIJAYAVELU
 
Flow control and error control
BHUVIJAYAVELU
 
3 2--power-aware-cloud
BHUVIJAYAVELU
 

Java inheritance

  • 1. Java Inheritance There are two ways to reuse the existing classes Composition define a new class, which is composed of existing classes. Composition exhibits a "has-a" relationship. Inheritance derive a new class based on an existing class with modifications / extensions. Inheritance exhibits a “is-a" relationship.
  • 2. 2 Inheritance: Definition  inheritance: a parent-child relationship between classes  Reuse of existing code or Method Overriding (Runtime Polymorphism).  allows sharing of the behavior of the parent class into its child classes  child class can add new behavior / override existing behavior from parent  The more general class is called:  superclass, base class, parent class  The more specific class is called:  subclass, derived class, child class
  • 3. 3  Two kinds:  implementation: the code that defines methods.  Derived class inherits the implementations of all methods from base class.  interface: the method prototypes only.  Accessing superclass methods from derived class.  Can use super() to access all (non-private) superclass methods.  Can use super() to call base class constructor.
  • 4. Syntax of Inheritance: class Subclass-name extends Superclass-name { //methods and fields } keyword extends: Indicates that you are making a new class that derives from an existing class.
  • 5. Types of Inheritance: On the basis of class, there can be three types of inheritance: Single Inheritance Multilevel Inheritance Hierarchical Inheritance Multiple & Hybrid - supported through interface only
  • 8. Why multiple inheritance is not supported in java? class A { void msg(){System.out.println("Hello");} } class B { void msg(){System.out.println("Welcome");} } class C extends A,B { //suppose if it were Public Static void main(String args[]){ C obj=new C(); obj.msg(); //Now which msg() method would be invoked? } }
  • 9. super keyword The super is a reference variable that is used to refer immediate parent class object. Usage of super Keyword super is used to refer immediate parent class instance variable. super() is used to invoke immediate parent class constructor. super is used to invoke immediate parent class method.
  • 10. Example: class Vehicle { int speed=50; } class Bike extends Vehicle { int speed=100; void display() { System.out.println(speed); //will print speed of Bike } public static void main(String args[]) { Bike b=new Bike(); b.display(); } } class Vehicle { int speed=50; } class Bike extends Vehicle { int speed=100; void display() { System.out.println(super.speed); //will print speed of Vehicle now } public static void main(String args[]) { Bike b=new Bike(); b.display(); } }
  • 11. super is used to invoke parent class constructor. 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(); }}
  • 12. super can be used to invoke parent class method. class Person { void message(){System.out.println("welcome");} } class Student extends Person { void message() { System.out.println("welcome to java"); } void display() { message();//will invoke current class message() method super.message();//will invoke parent class message() method } public static void main(String args[]) { Student s=new Student(); s.display();}}
  • 13. Method Overriding in Java Having the same method in the subclass as declared in the parent class is known as method overriding. In other words, If subclass provides the specific implementation of the method i.e. already provided by its parent class, it is known as Method Overriding. Method Overriding is used for Runtime Polymorphism Rules for Method Overriding: method must have same name as in the parent class method must have same parameter as in the parent class. must be inheritance (IS-A) relationship.
  • 14. class Vehicle { void run() { System.out.println("Vehicle"); } } class Bike extends Vehicle { public static void main(String args[]) { Bike obj = new Bike(); obj.run(); } } class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Bike extends Vehicle { void run() { System.out.println("Bike is running"); } public static void main(String args[]) { Bike obj = new Bike(); obj.run(); }
  • 15. Method Overloading Method Overriding 1) Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class. 2) method overloading is performed within a class. Method overriding occurs in two classes that have IS-A relationship. 3) In case of method overloading parameter must be different. In case of method overriding parameter must be same.