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

PDF
itft-Inheritance in java
PPTX
Access Modifier.pptx
PPTX
Super keyword in java
PPTX
Inheritance In Java
PPT
Inheritance in java
PPTX
Inheritance in java
PPT
Inheritance and Polymorphism
PPTX
Lecture_7-Encapsulation in Java.pptx
itft-Inheritance in java
Access Modifier.pptx
Super keyword in java
Inheritance In Java
Inheritance in java
Inheritance in java
Inheritance and Polymorphism
Lecture_7-Encapsulation in Java.pptx

What's hot (20)

PPT
Abstract class
PPTX
This keyword in java
PPTX
Polymorphism in java
PPT
Abstract class in java
PPTX
INHERITANCE IN JAVA.pptx
PPTX
Java Method, Static Block
PPTX
Inheritance in Java
PPTX
Collections framework in java
PPT
Polymorphism in java
PPT
Java oops and fundamentals
PPTX
Multiple inheritance possible in Java
PPT
Final keyword in java
PPTX
Java constructors
PPTX
Structure of java program diff c- cpp and java
PPTX
Java program structure
PPT
Java Programming - Polymorphism
PPTX
encapsulation, inheritance, overriding, overloading
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
Abstract Class & Abstract Method in Core Java
Abstract class
This keyword in java
Polymorphism in java
Abstract class in java
INHERITANCE IN JAVA.pptx
Java Method, Static Block
Inheritance in Java
Collections framework in java
Polymorphism in java
Java oops and fundamentals
Multiple inheritance possible in Java
Final keyword in java
Java constructors
Structure of java program diff c- cpp and java
Java program structure
Java Programming - Polymorphism
encapsulation, inheritance, overriding, overloading
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Abstract Class & Abstract Method in Core Java
Ad

Viewers also liked (20)

PDF
Java Inheritance
PPT
Java inheritance
PPTX
Inheritance
PPTX
Advanced Object-Oriented/SOLID Principles
PPTX
Object Oriented Design SOLID Principles
PPTX
S.O.L.I.D. Principles for Software Architects
PPT
SOLID Design Principles
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
PDF
Lesson 1 • Elements & Principles of Design and Art
PDF
Iot data aggrigation
PPTX
Inheritance in java
PPT
Java: Inheritance
PDF
Constructors and Destructors
PPSX
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
Inheritance in JAVA PPT
KEY
"SOLID" Object Oriented Design Principles
PPTX
Scanner
PPT
Hierarchical Object Oriented Design
PPT
Constructor & Destructor
Java Inheritance
Java inheritance
Inheritance
Advanced Object-Oriented/SOLID Principles
Object Oriented Design SOLID Principles
S.O.L.I.D. Principles for Software Architects
SOLID Design Principles
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Lesson 1 • Elements & Principles of Design and Art
Iot data aggrigation
Inheritance in java
Java: Inheritance
Constructors and Destructors
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in JAVA PPT
"SOLID" Object Oriented Design Principles
Scanner
Hierarchical Object Oriented Design
Constructor & Destructor
Ad

Similar to Java inheritance (20)

PPTX
Inheritance ppt
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
PPTX
Inheritance,single,multiple.access rulepptx
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Inheritance
PPTX
Java - Inheritance_multiple_inheritance.pptx
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PDF
Inheritance and interface
PPTX
Unit3 part2-inheritance
PPTX
Inheritance Slides
PPTX
Unit3 inheritance
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
PPTX
Chap-3 Inheritance.pptx
PPTX
Java(inheritance)
PPT
L7 inheritance
PPT
L7 inheritance
PPTX
Inheritance Interface and Packags in java programming.pptx
PPTX
inheritance and interface in oops with java .pptx
Inheritance ppt
‏‏‏‏‏‏oop lecture objectives will come.pdf
OBJECT ORIENTED PROGRAMMING_Unit2_NOTES.pptx
Inheritance,single,multiple.access rulepptx
Modules 333333333³3444444444444444444.pptx
Inheritance
Java - Inheritance_multiple_inheritance.pptx
OOPS_Unit2.inheritance and interface objected oriented programming
Inheritance and interface
Unit3 part2-inheritance
Inheritance Slides
Unit3 inheritance
Inheritance in Java is a mechanism in which one object acquires all the prope...
Chap-3 Inheritance.pptx
Java(inheritance)
L7 inheritance
L7 inheritance
Inheritance Interface and Packags in java programming.pptx
inheritance and interface in oops with java .pptx

More from BHUVIJAYAVELU (9)

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

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.