SlideShare a Scribd company logo
1 of 9
INHERITANCE
Inheritance in real World
TYPES OF INHERITANCE
class one
{ public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
class three extends two
{
public void print_geek()
{
System.out.println("Geeks");
}
} // Drived class
public class Main
{ public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek(); G
}
}
OUTPUT Geeks for Geeks
Example Multilevel inheritance
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}}
class three extends one
{ /*............*/
}
// Drived class
public class Main
{ public static void main(String[] args)
{
three g = new three();
g.print_geek();
two t = new two();
t.print_for();
g.print_geek();
} }
OUTPUT GeeGeeks for Geeks
concept of Hierarchical inheritance
Which statements are true?
Select the two correct answers.
(a) A class can only be extended by one class.
(b) Every Java object has a public method named equals.
(c) Every Java object has a public method named length.
(d) A class can extend any number of classes.
(e) A non-final class can be extended by any number of classes.
Which statements are true?
(a) A subclass must define all the methods from the superclass.
(b) It is possible for a subclass to define a method with the same name and
parameters as a method defined by the superclass.
(c) It is possible for a subclass to define a field with the same name as a field
defined by the superclass.
(d) It is possible for two classes to be the superclass of each other.
MCQ
Java this keyword
this keyword automatically holds the reference to current instance of a
class. It is very useful in scenarios where we are inheriting a method from
parent class into child class, and want to invoke method from child class
specifically. We can use this keyword to access static fields in the class as
well, but recommended approach to access static fields using class
reference e.g. MyClass.STATIC_FIELD.
Java super keyword
Similar to this keyword, super also is a reserved keyword in Java. It always
hold the reference to parent class of any given class. Using super keyword,
we can access the fields and methods of parent class in any child class.
**Can we have this () and super () together?
Both this() and super() are constructor calls. Constructor call must always
be the first statement. So we can not have two statements as first
statement, hence either we can call super() or we can call this() from the
constructor, but not both.
Use of this and Super
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}}
Output black white
Predict the output??
Which statement is true?
Select the one correct answer.
(a) A super() or this() call must always be provided explicitly as the first
statement in the body of a constructor.
(b) If both a subclass and its superclass do not have any declared
constructors,the implicit default constructor of the subclass will call
super() when run.
(c) If neither super() nor this() is declared as the first statement in the body of
a constructor, this() will implicitly be inserted as the first statement.
(d) If super() is the first statement in the body of a constructor, this() can be
declared as the second statement.
(e) Calling super() as the first statement in the body of a constructor of a
subclass will always work, since all superclasses have a default constructor.
MCQ

More Related Content

Similar to Day_2.1.pptx

Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)farhan amjad
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7kamal kotecha
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritanceIntro C# Book
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interfaceShubham Sharma
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.pptRassjb
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxNITHISG1
 

Similar to Day_2.1.pptx (20)

11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Inheritance chepter 7
Inheritance chepter 7Inheritance chepter 7
Inheritance chepter 7
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java unit2
Java unit2Java unit2
Java unit2
 
inheritance
inheritanceinheritance
inheritance
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Java2
Java2Java2
Java2
 
Inheritance and interface
Inheritance and interfaceInheritance and interface
Inheritance and interface
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 

More from ishasharma835109

More from ishasharma835109 (6)

hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
DAY_1.4.pptx
DAY_1.4.pptxDAY_1.4.pptx
DAY_1.4.pptx
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
 
DAY_1.2.pptx
DAY_1.2.pptxDAY_1.2.pptx
DAY_1.2.pptx
 
DAY_1.1.pptx
DAY_1.1.pptxDAY_1.1.pptx
DAY_1.1.pptx
 

Recently uploaded

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfRagavanV2
 

Recently uploaded (20)

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 

Day_2.1.pptx

  • 4. class one { public void print_geek() { System.out.println("Geeks"); } } class two extends one { public void print_for() { System.out.println("for"); } } class three extends two { public void print_geek() { System.out.println("Geeks"); } } // Drived class public class Main { public static void main(String[] args) { three g = new three(); g.print_geek(); g.print_for(); g.print_geek(); G } } OUTPUT Geeks for Geeks Example Multilevel inheritance
  • 5. class one { public void print_geek() { System.out.println("Geeks"); } } class two extends one { public void print_for() { System.out.println("for"); }} class three extends one { /*............*/ } // Drived class public class Main { public static void main(String[] args) { three g = new three(); g.print_geek(); two t = new two(); t.print_for(); g.print_geek(); } } OUTPUT GeeGeeks for Geeks concept of Hierarchical inheritance
  • 6. Which statements are true? Select the two correct answers. (a) A class can only be extended by one class. (b) Every Java object has a public method named equals. (c) Every Java object has a public method named length. (d) A class can extend any number of classes. (e) A non-final class can be extended by any number of classes. Which statements are true? (a) A subclass must define all the methods from the superclass. (b) It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass. (c) It is possible for a subclass to define a field with the same name as a field defined by the superclass. (d) It is possible for two classes to be the superclass of each other. MCQ
  • 7. Java this keyword this keyword automatically holds the reference to current instance of a class. It is very useful in scenarios where we are inheriting a method from parent class into child class, and want to invoke method from child class specifically. We can use this keyword to access static fields in the class as well, but recommended approach to access static fields using class reference e.g. MyClass.STATIC_FIELD. Java super keyword Similar to this keyword, super also is a reserved keyword in Java. It always hold the reference to parent class of any given class. Using super keyword, we can access the fields and methods of parent class in any child class. **Can we have this () and super () together? Both this() and super() are constructor calls. Constructor call must always be the first statement. So we can not have two statements as first statement, hence either we can call super() or we can call this() from the constructor, but not both. Use of this and Super
  • 8. class Animal{ String color="white"; } class Dog extends Animal{ String color="black"; void printColor(){ System.out.println(color);//prints color of Dog class System.out.println(super.color);//prints color of Animal class } } class TestSuper1{ public static void main(String args[]){ Dog d=new Dog(); d.printColor(); }} Output black white Predict the output??
  • 9. Which statement is true? Select the one correct answer. (a) A super() or this() call must always be provided explicitly as the first statement in the body of a constructor. (b) If both a subclass and its superclass do not have any declared constructors,the implicit default constructor of the subclass will call super() when run. (c) If neither super() nor this() is declared as the first statement in the body of a constructor, this() will implicitly be inserted as the first statement. (d) If super() is the first statement in the body of a constructor, this() can be declared as the second statement. (e) Calling super() as the first statement in the body of a constructor of a subclass will always work, since all superclasses have a default constructor. MCQ