SlideShare a Scribd company logo
Inheritance
Types of Inheritance
Super Keyword
Method Overriding
Inheritance
• Inheritance is a mechanism wherein a new class is derived from an
existing class. In Java, classes may inherit or acquire the properties and
methods of other classes.
• A class derived from another class is called a subclass
• Where as the class from which a subclass is derived is called a super class
• A subclass can have only one superclass
• whereas a superclass may have one or more subclasses.
Use of Inheritance
• For Method Overriding (so runtime polymorphism can be achieved).
• For Code Reusability.
Types of inheritance supported in java
Single level inheritance
Multi level inheritance
Hierarchal inheritance
Single level inheritance
• When a class extends another one class only then we call it a single
inheritance.
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extendsClass_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
public class inheritance
{ public static void main(String[] y)
{ // with object of b class a class methods can be accessed
Class_B b=new Class_B();
b.method_B();
b.method_A();
}
}
Multilevel inheritance
• Multilevel inheritance refers to a mechanism in OO technology where one
class can inherit from a derived class, thereby making this derived class the
base class for the new class
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extends Class_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
class Class_C extends Class_B
{ void method_C()
{ System.out.println("the Class_C method_C a called"); }
}
public class inheritance
{ public static void main(String[] y)
{
/*
* with object of b class a class methods can be accessed
*/
Class_C C=new Class_C();
C.method_C();
C.method_B();
C.method_A();
}
}
Hierarchical Inheritance
• In such kind of inheritance one class is inherited by many sub classes.
class Class_A
{ void method_A()
{ System.out.println("the Class_A method_A a called"); }
}
class Class_B extends Class_A
{ void method_B()
{ System.out.println("the Class_b method_B a called"); }
}
class Class_C extends Class_A
{ void method_C()
{ System.out.println("the Class_C method_C a called"); }
}
public class inheritance
{ public static void main(String[] y)
{
/*
* with object of b class a class methods can be accessed
*/
Class_C C=new Class_C();
C.method_A();
C.method_C();
Class_B B=new Class_B();
B.method_A();
B.method_B();
}
}
Super Keyword
• super is used to serve following two objectives:
1) Accessing super class members (Methods and Variables).
2) Calling super class Constructor.
/** accessing super class methods and variables */
class Class_A
{ int int_a=100;
void demo()
{ System.out.println("hello world"); }
}
class Class_B extends Class_A
{ int int_a=200;
void demo_1()
{ System.out.println("the value of int_a is"+super.int_a);
super.demo();
}
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B();
b.demo_1();
}
}
/** accessing super class Constructor */
class Class_A
{ int int_a;
Class_A()
{ int_a=100;
System.out.println("the int_a value is "+int_a);
}
}
class Class_B extends Class_A
{ Class_B()
{ super(); }//calls super class constructor
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B(); }
}
Method Overriding
• Child class has the same method as of base class. In such cases child class
overrides the parent class method without even touching the source code
of the base class. This feature is known as method overriding.
/* * Method overriding */
class Class_A
{ void demo()
{ System.out.println("the class A method called"); }
}
class Class_B extends Class_A
{ void demo()
{ System.out.println("the class B method called"); }
}
public class inheritance
{ public static void main(String[] y)
{ Class_B b=new Class_B();
b.demo();
}
}
Inheritance

More Related Content

What's hot

Seminar on java
Seminar on javaSeminar on java
Seminar on java
shathika
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
kamal kotecha
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
VINOTH R
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Tamanna Akter
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Darpan Chelani
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Arnab Bhaumik
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
javainheritance
javainheritancejavainheritance
javainheritance
Arjun Shanka
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
yash jain
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Elizabeth alexander
 
Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 

What's hot (20)

Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
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
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
javainheritance
javainheritancejavainheritance
javainheritance
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 

Similar to Inheritance

Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
teach4uin
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
HarshithaAllu
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
Ahsan Raja
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
GaneshKumarKanthiah
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
Java
JavaJava
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
sotlsoc
 
Oops
OopsOops
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
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
Sunil Kumar Gunasekaran
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
29csharp
29csharp29csharp
29csharp
Sireesh K
 

Similar to Inheritance (20)

Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Inheritance
InheritanceInheritance
Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Java
JavaJava
Java
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
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
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
29csharp
29csharp29csharp
29csharp
 

More from yugandhar vadlamudi

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
yugandhar vadlamudi
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
yugandhar vadlamudi
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
yugandhar vadlamudi
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
yugandhar vadlamudi
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
yugandhar vadlamudi
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
yugandhar vadlamudi
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
yugandhar vadlamudi
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
yugandhar vadlamudi
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
yugandhar vadlamudi
 
Operators in java
Operators in javaOperators in java
Operators in java
yugandhar vadlamudi
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
yugandhar vadlamudi
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
yugandhar vadlamudi
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
yugandhar vadlamudi
 

More from yugandhar vadlamudi (14)

Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
 
Object Relational model for SQLIite in android
Object Relational model for SQLIite  in android Object Relational model for SQLIite  in android
Object Relational model for SQLIite in android
 
JButton in Java Swing example
JButton in Java Swing example JButton in Java Swing example
JButton in Java Swing example
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Packaes & interfaces
Packaes & interfacesPackaes & interfaces
Packaes & interfaces
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
JMenu Creation in Java Swing
JMenu Creation in Java SwingJMenu Creation in Java Swing
JMenu Creation in Java Swing
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Closer look at classes
Closer look at classesCloser look at classes
Closer look at classes
 
java Applet Introduction
java Applet Introductionjava Applet Introduction
java Applet Introduction
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 

Recently uploaded

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 

Recently uploaded (20)

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 

Inheritance

  • 1. Inheritance Types of Inheritance Super Keyword Method Overriding
  • 2. Inheritance • Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. • A class derived from another class is called a subclass • Where as the class from which a subclass is derived is called a super class • A subclass can have only one superclass • whereas a superclass may have one or more subclasses. Use of Inheritance • For Method Overriding (so runtime polymorphism can be achieved). • For Code Reusability.
  • 3. Types of inheritance supported in java Single level inheritance Multi level inheritance Hierarchal inheritance
  • 4. Single level inheritance • When a class extends another one class only then we call it a single inheritance. class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extendsClass_A { void method_B() { System.out.println("the Class_b method_B a called"); } } public class inheritance { public static void main(String[] y) { // with object of b class a class methods can be accessed Class_B b=new Class_B(); b.method_B(); b.method_A(); } }
  • 5. Multilevel inheritance • Multilevel inheritance refers to a mechanism in OO technology where one class can inherit from a derived class, thereby making this derived class the base class for the new class class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extends Class_A { void method_B() { System.out.println("the Class_b method_B a called"); } } class Class_C extends Class_B { void method_C() { System.out.println("the Class_C method_C a called"); } } public class inheritance { public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_C(); C.method_B(); C.method_A(); } }
  • 6. Hierarchical Inheritance • In such kind of inheritance one class is inherited by many sub classes. class Class_A { void method_A() { System.out.println("the Class_A method_A a called"); } } class Class_B extends Class_A { void method_B() { System.out.println("the Class_b method_B a called"); } } class Class_C extends Class_A { void method_C() { System.out.println("the Class_C method_C a called"); } } public class inheritance { public static void main(String[] y) { /* * with object of b class a class methods can be accessed */ Class_C C=new Class_C(); C.method_A(); C.method_C(); Class_B B=new Class_B(); B.method_A(); B.method_B(); } }
  • 7. Super Keyword • super is used to serve following two objectives: 1) Accessing super class members (Methods and Variables). 2) Calling super class Constructor. /** accessing super class methods and variables */ class Class_A { int int_a=100; void demo() { System.out.println("hello world"); } } class Class_B extends Class_A { int int_a=200; void demo_1() { System.out.println("the value of int_a is"+super.int_a); super.demo(); } } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); b.demo_1(); } }
  • 8. /** accessing super class Constructor */ class Class_A { int int_a; Class_A() { int_a=100; System.out.println("the int_a value is "+int_a); } } class Class_B extends Class_A { Class_B() { super(); }//calls super class constructor } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); } }
  • 9. Method Overriding • Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding. /* * Method overriding */ class Class_A { void demo() { System.out.println("the class A method called"); } } class Class_B extends Class_A { void demo() { System.out.println("the class B method called"); } } public class inheritance { public static void main(String[] y) { Class_B b=new Class_B(); b.demo(); } }