SlideShare a Scribd company logo
1 of 20
 The process by which one class acquires the
properties(data members) and functionalities(methods)
of another class is called inheritance.
 The aim of inheritance is to provide the reusability of
code so that a class has to write only the unique
features and rest of the common properties and
functionalities can be extended from the another class.
 Child Class:
The class that extends the features of another class is
known as child class, sub class or derived class.
 Parent Class:
The class whose properties and functionalities are
used(inherited) by another class is known as parent
class, super class or Base class.
 To inherit a class we use extends keyword. Here
class XYZ is child class and class ABC is parent
class.
 The class XYZ is inheriting the properties and
methods of ABC class.
class XYZ extends ABC
{
}
class Teacher {
String designation = "Teacher";
String collegeName =
"Beginnersbook";
void does()
{ System.out.println("Teaching");
}
}
public class PhysicsTeacher
extends Teacher
{
String mainSubject = "Physics";
public static void main(String
args[])
{
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Beginnersbook
Teacher
Physics
Teaching
 Constructors are not inherited, even though they
have public visibility
 Yet we often want to use the parent's constructor to
set up the "parent's part" of the object
 The super reference can be used to refer to the
parent class, and often is used to invoke the
parent's constructor
 A child’s constructor is responsible for calling the parent’s
constructor
 The first line of a child’s constructor should use the super
reference to call the parent’s constructor
 The super reference can also be used to reference other
variables and methods defined in the parent’s class
 Java supports single inheritance, meaning that a
derived class can have only one parent class
 Multiple inheritance allows a class to be derived
from two or more classes, inheriting the members of
all parents
 Collisions, such as the same variable name in two
parents, have to be resolved
 Java does not support multiple inheritance
 In most cases, the use of interfaces gives us aspects
of multiple inheritance without the overhead
A class inherits properties
from a class which again has
inherits properties.
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 =newCube();
cube.display();
cube.area();
cube.volume();
}
}
Output
Inside display
Inside area
Inside volume
 A child class can override the definition of an
inherited method in favor of its own
 The new method must have the same signature as
the parent's method, but can have a different body
 The type of the object executing the method
determines which version of the method is invoked
 A parent method can be invoked explicitly using the super
reference
 If a method is declared with the final modifier, it cannot be
overridden
 The concept of overriding can be applied to data and is
called shadowing variables
 Shadowing variables should be avoided because it tends to
cause unnecessarily confusing code
 A child class can override the definition of an
inherited method in favor of its own
 The new method must have the same
signature as the parent's method, but can
have a different body
 The type of the object executing the method
determines which version of the method is
invoked
 A parent method can be invoked explicitly using
the super reference
 If a method is declared with the final modifier, it
cannot be overridden
 The concept of overriding can be applied to data
and is called shadowing variables
 Shadowing variables should be avoided because it
tends to cause unnecessarily confusing code
Class A
{
int i,j;
A(int a,int b)
{
i=a;
j=b;
}
Void show()
{
System.out.println(“i and j:”+i+”
“+j);
}
}
Class B extends A {
B(int a,int b,int c) {
Super(a,b);
K=c;
}
Void show() {
System.out.println(“K:”+k);
}
}
Class override {
Public static void main(strng args[]) {
B subob=new B(1,2,3);
Subob.show();
}
}
OUTPUT:
 An abstract class is a placeholder in a class
hierarchy that represents a generic concept
 An abstract class cannot be instantiated
 We use the modifier abstract on the class header to
declare a class as abstract:
public abstract class Whatever
{
// contents
}
 Single inheritance is damn easy to
understand. 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.
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();
method obj.methodB();
}
}
 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.
Class X
{
public
void methodX()
{
System.out.println("Class X method");
} }
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
public
void methodZ()
{
System.out.println("class Z
method");
}
public static void main(String
args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}
 Multiple inheritance is a feature of
some object-oriented computer programming
languages in which an object
or class can inherit characteristics and
features from more than one parent object
or parent class.
Class X
{
public void methodX()
{
System.out.println("Class X
method");
}
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y
method");
}
}
Class Z extends Y
{
public void methodZ()
{
System.out.println("class Z method");
}
public static void main(String args[])
{
Z obj = new Z();
obj.methodX();
obj.methodY();
obj.methodZ();
}
}

More Related Content

What's hot

Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course RAKESH P
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - InheritanceOum Saokosal
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Dr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java InheritanceDr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java Inheritancejalinder123
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 

What's hot (20)

Java Inheritance | Java Course
Java Inheritance | Java Course Java Inheritance | Java Course
Java Inheritance | Java Course
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Inheritance
InheritanceInheritance
Inheritance
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Dr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java InheritanceDr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java Inheritance
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 

Similar to Inheritance in Java Explained in 40 Characters

Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance SlidesAhsan Raja
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3Narayana Swamy
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING ManpreetSingh1387
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javachauhankapil
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.pptRassjb
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 

Similar to Inheritance in Java Explained in 40 Characters (20)

Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Inheritance
InheritanceInheritance
Inheritance
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Chap11
Chap11Chap11
Chap11
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Core java oop
Core java oopCore java oop
Core java oop
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

More from Haripritha

Mapreduce script
Mapreduce scriptMapreduce script
Mapreduce scriptHaripritha
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networksHaripritha
 
Operating system
Operating systemOperating system
Operating systemHaripritha
 
Presentation 2
Presentation 2Presentation 2
Presentation 2Haripritha
 
Computer Organization
Computer OrganizationComputer Organization
Computer OrganizationHaripritha
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingHaripritha
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++Haripritha
 

More from Haripritha (9)

Mapreduce script
Mapreduce scriptMapreduce script
Mapreduce script
 
Wireless sensor networks
Wireless sensor networksWireless sensor networks
Wireless sensor networks
 
Datamining
DataminingDatamining
Datamining
 
Operating system
Operating systemOperating system
Operating system
 
Presentation 2
Presentation 2Presentation 2
Presentation 2
 
Computer Organization
Computer OrganizationComputer Organization
Computer Organization
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
programming language in c&c++
programming language in c&c++programming language in c&c++
programming language in c&c++
 
encoding
encodingencoding
encoding
 

Recently uploaded

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

Inheritance in Java Explained in 40 Characters

  • 1.
  • 2.  The process by which one class acquires the properties(data members) and functionalities(methods) of another class is called inheritance.  The aim of inheritance is to provide the reusability of code so that a class has to write only the unique features and rest of the common properties and functionalities can be extended from the another class.  Child Class: The class that extends the features of another class is known as child class, sub class or derived class.  Parent Class: The class whose properties and functionalities are used(inherited) by another class is known as parent class, super class or Base class.
  • 3.  To inherit a class we use extends keyword. Here class XYZ is child class and class ABC is parent class.  The class XYZ is inheriting the properties and methods of ABC class. class XYZ extends ABC { }
  • 4. class Teacher { String designation = "Teacher"; String collegeName = "Beginnersbook"; void does() { System.out.println("Teaching"); } } public class PhysicsTeacher extends Teacher { String mainSubject = "Physics"; public static void main(String args[]) { System.out.println(obj.collegeName); System.out.println(obj.designation); System.out.println(obj.mainSubject); obj.does(); } } Output: Beginnersbook Teacher Physics Teaching
  • 5.  Constructors are not inherited, even though they have public visibility  Yet we often want to use the parent's constructor to set up the "parent's part" of the object  The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
  • 6.  A child’s constructor is responsible for calling the parent’s constructor  The first line of a child’s constructor should use the super reference to call the parent’s constructor  The super reference can also be used to reference other variables and methods defined in the parent’s class
  • 7.  Java supports single inheritance, meaning that a derived class can have only one parent class  Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents  Collisions, such as the same variable name in two parents, have to be resolved  Java does not support multiple inheritance  In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
  • 8. A class inherits properties from a class which again has inherits properties. 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 =newCube(); cube.display(); cube.area(); cube.volume(); } } Output Inside display Inside area Inside volume
  • 9.  A child class can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 10.  A parent method can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 11.  A child class can override the definition of an inherited method in favor of its own  The new method must have the same signature as the parent's method, but can have a different body  The type of the object executing the method determines which version of the method is invoked
  • 12.  A parent method can be invoked explicitly using the super reference  If a method is declared with the final modifier, it cannot be overridden  The concept of overriding can be applied to data and is called shadowing variables  Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
  • 13. Class A { int i,j; A(int a,int b) { i=a; j=b; } Void show() { System.out.println(“i and j:”+i+” “+j); } } Class B extends A { B(int a,int b,int c) { Super(a,b); K=c; } Void show() { System.out.println(“K:”+k); } } Class override { Public static void main(strng args[]) { B subob=new B(1,2,3); Subob.show(); } } OUTPUT:
  • 14.  An abstract class is a placeholder in a class hierarchy that represents a generic concept  An abstract class cannot be instantiated  We use the modifier abstract on the class header to declare a class as abstract: public abstract class Whatever { // contents }
  • 15.  Single inheritance is damn easy to understand. 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.
  • 16. 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(); method obj.methodB(); } }
  • 17.  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.
  • 18. Class X { public void methodX() { System.out.println("Class X method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } } Class Z extends Y { public void methodZ() { System.out.println("class Z method"); } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); obj.methodY(); obj.methodZ(); } }
  • 19.  Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class.
  • 20. Class X { public void methodX() { System.out.println("Class X method"); } } Class Y extends X { public void methodY() { System.out.println("class Y method"); } } Class Z extends Y { public void methodZ() { System.out.println("class Z method"); } public static void main(String args[]) { Z obj = new Z(); obj.methodX(); obj.methodY(); obj.methodZ(); } }