SlideShare a Scribd company logo
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 1/5
Inheritance
Inheritance
Inheritance: Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are built upon existing
classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class.
Moreover, you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Advantages of inheritance:
For Method Overriding (so runtime polymorphism can be achieved).
For Code Reusability.
Syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
A class which is inherited is called a parent or superclass, and the new class is called child or subclass.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class,
extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also
called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields
and methods of the existing class when you create a new class. You can use the same fields and
methods already defined in the previous class.
Example:
Cat is the subclass and Animal is the superclass. The relationship between the two classes is Cat IS-A
Animal. It means that Cat is a type of Animal.
class Animal
{
}
class Cat extends Animal
{
}
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 2/5
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
typesofinheritance.jpg
In java programming, multiple and hybrid inheritance is supported through interface only.
multiple.jpg
Note: Multiple inheritance is not supported in Java through class.
When one class inherits multiple classes, it is known as multiple inheritance.
Single Inheritance Example
single.png
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Demo
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
Output:
barking...
eating...
Multilevel Inheritance Example
Multi level
inheritance.png
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 3/5
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}
}
class Demo
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
Output:
weeping...
barking...
eating...
Hierarchical Inheritance Example
heirarchical inheritance.png
class Animal
{
void eat()
{
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 4/5
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 Demo
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
Output:
meowing...
eating...
Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and
B classes have the same method and you call it from child class object, there will be ambiguity to call the
method of A or B class.
If you inherit 2 classes, there will be compile time error.
class A
{
void msg()
{
3/30/2019 Inheritance: Your Guided Course Template
https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 5/5
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B
{
public static void main(String args[])
{
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
Output:
Compile Time Error

More Related Content

What's hot (20)

PDF
java-06inheritance
Arjun Shanka
 
PPTX
Multiple inheritance possible in Java
Kurapati Vishwak
 
PPTX
Inheritance in Java
Elizabeth alexander
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Java Inheritance
VINOTH R
 
PDF
javainheritance
Arjun Shanka
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPTX
Inheritance and its types In Java
MD SALEEM QAISAR
 
PPTX
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
PPT
Inheritance polymorphism-in-java
Deepak Singh
 
PPTX
Java(inheritance)
Pooja Bhojwani
 
PPT
Inheritance
Aadhi Aadhithya
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PPTX
Java Inheritance | Java Course
RAKESH P
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
java-06inheritance
Arjun Shanka
 
Multiple inheritance possible in Java
Kurapati Vishwak
 
Inheritance in Java
Elizabeth alexander
 
Java inheritance
BHUVIJAYAVELU
 
itft-Inheritance in java
Atul Sehdev
 
Java inheritance
Arati Gadgil
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance in java
Tech_MX
 
Java Inheritance
VINOTH R
 
javainheritance
Arjun Shanka
 
Inheritance In Java
Darpan Chelani
 
Inheritance and its types In Java
MD SALEEM QAISAR
 
Interface in java ,multiple inheritance in java, interface implementation
HoneyChintal
 
Inheritance polymorphism-in-java
Deepak Singh
 
Java(inheritance)
Pooja Bhojwani
 
Inheritance
Aadhi Aadhithya
 
Inheritance C#
Raghuveer Guthikonda
 
Java Inheritance | Java Course
RAKESH P
 
Inheritance and Polymorphism Java
M. Raihan
 

Similar to Inheritance used in java (20)

PDF
Inheritance in Java.pdf
kumari36
 
PPTX
inheritance
Jay Prajapati
 
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
PPTX
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
PPTX
INHERITANCE.pptx
HARIPRIYA M P
 
PPTX
Ayan Das_25300121057.pptx
Ayan974999
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PDF
IRJET- Inheritance in Java
IRJET Journal
 
PPTX
Inheritance in java
Ariful Islam
 
PPTX
Understanding_of_Inheritance_in_Java_by_Quipoin.pptx
quipoin04
 
PPTX
ppt.pptx
FaleenFayaz
 
PPTX
Inheritance in oop
MuskanNazeer
 
PPTX
Inheritance in java
RahulAnanda1
 
PDF
Presentation Slide about Inharitance in Java Object Oriented Programming
Abdullah Al Noman
 
PPTX
Types of inheritance in java
chauhankapil
 
PDF
java_vyshali.pdf
Vyshali6
 
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
PDF
java inheritance that is used in oop cls
hassanshahzad1502
 
PPTX
How do i use inheritance in java?
kritikumar16
 
PPTX
How do i use inheritance in java?
kritikumar16
 
Inheritance in Java.pdf
kumari36
 
inheritance
Jay Prajapati
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Chapter5.pptxfghwryhYETHYETH67IOIKUTJJUILOUI
berihun18
 
INHERITANCE.pptx
HARIPRIYA M P
 
Ayan Das_25300121057.pptx
Ayan974999
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
IRJET- Inheritance in Java
IRJET Journal
 
Inheritance in java
Ariful Islam
 
Understanding_of_Inheritance_in_Java_by_Quipoin.pptx
quipoin04
 
ppt.pptx
FaleenFayaz
 
Inheritance in oop
MuskanNazeer
 
Inheritance in java
RahulAnanda1
 
Presentation Slide about Inharitance in Java Object Oriented Programming
Abdullah Al Noman
 
Types of inheritance in java
chauhankapil
 
java_vyshali.pdf
Vyshali6
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
java inheritance that is used in oop cls
hassanshahzad1502
 
How do i use inheritance in java?
kritikumar16
 
How do i use inheritance in java?
kritikumar16
 
Ad

More from TharuniDiddekunta (15)

PDF
String class
TharuniDiddekunta
 
PDF
Exception handling basic
TharuniDiddekunta
 
PDF
Creating your own exception
TharuniDiddekunta
 
PDF
Built in exceptions
TharuniDiddekunta
 
PDF
Packages access protection, importing packages
TharuniDiddekunta
 
PDF
Operators, control statements represented in java
TharuniDiddekunta
 
PDF
Arrays in java
TharuniDiddekunta
 
PPTX
Software Metrics (Testing)
TharuniDiddekunta
 
PPTX
unit 3 Design 1
TharuniDiddekunta
 
PPTX
Unit 4 testing
TharuniDiddekunta
 
PPTX
risk managment and quality
TharuniDiddekunta
 
PPTX
Design
TharuniDiddekunta
 
PPT
Network layer
TharuniDiddekunta
 
PPTX
Transport layer and Application layer
TharuniDiddekunta
 
PPT
Congection control and Internet working
TharuniDiddekunta
 
String class
TharuniDiddekunta
 
Exception handling basic
TharuniDiddekunta
 
Creating your own exception
TharuniDiddekunta
 
Built in exceptions
TharuniDiddekunta
 
Packages access protection, importing packages
TharuniDiddekunta
 
Operators, control statements represented in java
TharuniDiddekunta
 
Arrays in java
TharuniDiddekunta
 
Software Metrics (Testing)
TharuniDiddekunta
 
unit 3 Design 1
TharuniDiddekunta
 
Unit 4 testing
TharuniDiddekunta
 
risk managment and quality
TharuniDiddekunta
 
Network layer
TharuniDiddekunta
 
Transport layer and Application layer
TharuniDiddekunta
 
Congection control and Internet working
TharuniDiddekunta
 
Ad

Recently uploaded (20)

PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
Bachelor of information technology syll
SudarsanAssistantPro
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
Digital water marking system project report
Kamal Acharya
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Bachelor of information technology syll
SudarsanAssistantPro
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 

Inheritance used in java

  • 1. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 1/5 Inheritance Inheritance Inheritance: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. Advantages of inheritance: For Method Overriding (so runtime polymorphism can be achieved). For Code Reusability. Syntax: class Subclass-name extends Superclass-name { //methods and fields } A class which is inherited is called a parent or superclass, and the new class is called child or subclass. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class. Example: Cat is the subclass and Animal is the superclass. The relationship between the two classes is Cat IS-A Animal. It means that Cat is a type of Animal. class Animal { } class Cat extends Animal { }
  • 2. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 2/5 Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. typesofinheritance.jpg In java programming, multiple and hybrid inheritance is supported through interface only. multiple.jpg Note: Multiple inheritance is not supported in Java through class. When one class inherits multiple classes, it is known as multiple inheritance. Single Inheritance Example single.png class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Demo { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); } } Output: barking... eating... Multilevel Inheritance Example Multi level inheritance.png
  • 3. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 3/5 class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class BabyDog extends Dog { void weep() { System.out.println("weeping..."); } } class Demo { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); } } Output: weeping... barking... eating... Hierarchical Inheritance Example heirarchical inheritance.png class Animal { void eat() {
  • 4. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 4/5 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 Demo { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); //c.bark();//C.T.Error } } Output: meowing... eating... Why multiple inheritance is not supported in java? To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. If you inherit 2 classes, there will be compile time error. class A { void msg() {
  • 5. 3/30/2019 Inheritance: Your Guided Course Template https://canvas.instructure.com/courses/1480238/pages/inheritance?module_item_id=21012851 5/5 System.out.println("Hello"); } } class B { void msg() { System.out.println("Welcome"); } } class C extends A,B { public static void main(String args[]) { C obj=new C(); obj.msg();//Now which msg() method would be invoked? } } Output: Compile Time Error