SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Inheritance can be defined as the process where one class acquires the properties of another. With the use of
inheritance the information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass derivedclass, childclass and the class
whose properties are inherited is known as superclass baseclass, parentclass.
extends Keyword
∙ extends is the keywordused to inherit the properties of a class. Below given is the
syntax of extends keyword.
∙ class Super
{ .....
..... }
class Sub extends Super
{ .....
..... }
Sample Code
Below given is an example demonstrating Java inheritance. In this example you can observe two classes namely Calculationand My_Calculation.
Using extends keyword the My_Calculationinheritsthe methodsadditionand Subtractionof Calculationclass.
class Calculation{
int z;
public void addition(int x, int y)
{
z=x+y;
System.out.println("The sum of the given numbers:"+z);
}
public void Substraction(int x,int y)
{
z=x-y;
System.out.println("The difference betweenthe given numbers:"+z);
}
}
public class My_Calculationextends Calculation
{
public void multiplication(intx, int y)
{
z=x*y;
System.out.println("Theproduct of the given numbers:"+z);
}
public static void main(String args[])
{ int a=20, b=10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Substraction(a, b);
demo.multiplication(a, b);
}
}
• Compile and execute the above code as shown
below
javac My_Calculation.java
java My_Calculation
After executing the program it will produce the following result.
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
∙ In the given program when an object to
My_Calculation class is created, a copy
of the contents of the super class is
made with in it. That is why, using the
object of the subclass you can access the
members of a super class.
∙ The Superclass reference variable can
hold the subclass object, but using that
variable you can access only the
members of the superclass, so to access
the members of both classes it is
recommended to always create reference
variable to the subclass.
∙ If you consider the above program, you can instantiate the class as given below. But
using the superclass reference variable ( cal in this case) you cannot call the
method multiplication(), which belongs to the subclass My_Calculation.
∙ Calculation demo = new My_Calculation();
∙ demo.addition(a, b);
∙ demo.Subtraction(a, b);
∙ Note − A subclass inherits all the members (fields, methods, and nested classes) from
its superclass. Constructors are not members, so they are not inherited by subclasses,
but the constructor of the superclass can be invoked from the subclass.
∙ super.variable
∙ super.method();
∙
∙ Sample Code
∙ This section provides you a program that demonstrates the usage of the super keyword.
∙ In the given program, you have two classes namely Sub_class and Super_class, both have a
method named display() with different implementations, and a variable named num with
different values. We are invoking display() method of both classes and printing the value of
the variable num of both classes. Here you can observe that we have used super keyword to
differentiate the members of superclass from subclass.
∙ The super keyword
∙ The super keyword is similar to this keyword. Following are the scenarios where the super
keyword is used.
∙ It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
∙ It is used to invoke the superclass constructor from subclass.
∙ Differentiating the Members
∙ If a class is inheriting the properties of another class. And if the members of the superclass
have the names same as the sub class, to differentiate these variables we use super keyword
as shown below.
∙ Example
∙ class Super_class {
∙ int num = 20;
∙
∙ // display method of superclass
∙ public void display() {
∙ System.out.println("This is the display method of superclass");
∙ }
∙ }
∙
∙ public class Sub_class extends Super_class {
∙ int num = 10;
∙
∙ // display method of sub class
∙ public void display() {
∙ System.out.println("This is the display method of subclass");
∙ }
∙
∙ // Invoking the display() method of superclass
∙ super.display();
∙
∙ // printing the value of variable num of subclass
∙ System.out.println("value of the variable named num in sub class:"+ sub.num);
∙
∙ // printing the value of variable num of superclass
∙ System.out.println("value of the variable named num in super class:"+ super.num);
∙ }
∙
∙ public static void main(String args[]) {
∙ Sub_class obj = new Sub_class();
∙ obj.my_method();
∙ }
∙ }
∙ Compile and execute the above code using the following syntax.
∙ javac Super_Demo
∙ java Super
∙
∙ On executing the program, you will get the following result −
∙ Output
∙ This is the display method of subclass
∙ This is the display method of superclass
∙ value of the variable named num in sub class:10
∙ value of the variable named num in super class:20
∙ IS-A Relationship
∙ IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.
∙ public class Animal {
∙ }
∙
∙ public class Mammal extends Animal {
∙ }
∙
∙ public class Reptile extends Animal {
∙ }
∙
∙ public class Dog extends Mammal {
∙ }
∙ With the use of the extends keyword, the subclasses will be able to inherit all the propertiesof the superclassexcept for the
private propertiesof the superclass.
∙ Example
∙ class Animal {
∙ }
∙
∙ class Mammal extends Animal {
∙ }
∙
∙ class Reptile extends Animal {
∙ }
∙
∙ public class Dog extends Mammal {
∙
∙ public static void main(String args[]) {
∙ Animal a = new Animal();
∙ Mammal m = new Mammal();
∙ Dog d = new Dog();
∙
∙ public static void main(String args[]) {
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
∙ This will produce the following result −
∙ Output
∙ true
∙ true
∙ true
∙ Circulate Sprint
∙ Planning
∙ Summary
∙ Verify Day 1 Burn
∙ Down

Contenu connexe

Similaire à Presentation 3.pdf

Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
sotlsoc
 

Similaire à Presentation 3.pdf (20)

Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Core java oop
Core java oopCore java oop
Core java oop
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 
Sdtl assignment 03
Sdtl assignment 03Sdtl assignment 03
Sdtl assignment 03
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
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
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
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
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading.NET F# Inheritance and operator overloading
.NET F# Inheritance and operator overloading
 

Dernier

一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
F
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Monica Sydney
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
ayvbos
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
ayvbos
 

Dernier (20)

20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理一比一原版田纳西大学毕业证如何办理
一比一原版田纳西大学毕业证如何办理
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
call girls in Anand Vihar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime NagercoilNagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
Nagercoil Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nagercoil
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
一比一原版(Curtin毕业证书)科廷大学毕业证原件一模一样
 
Best SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency DallasBest SEO Services Company in Dallas | Best SEO Agency Dallas
Best SEO Services Company in Dallas | Best SEO Agency Dallas
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 

Presentation 3.pdf

  • 1. Inheritance can be defined as the process where one class acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass derivedclass, childclass and the class whose properties are inherited is known as superclass baseclass, parentclass.
  • 2. extends Keyword ∙ extends is the keywordused to inherit the properties of a class. Below given is the syntax of extends keyword. ∙ class Super { ..... ..... } class Sub extends Super { ..... ..... }
  • 3. Sample Code Below given is an example demonstrating Java inheritance. In this example you can observe two classes namely Calculationand My_Calculation. Using extends keyword the My_Calculationinheritsthe methodsadditionand Subtractionof Calculationclass. class Calculation{ int z; public void addition(int x, int y) { z=x+y; System.out.println("The sum of the given numbers:"+z); } public void Substraction(int x,int y) { z=x-y; System.out.println("The difference betweenthe given numbers:"+z); } }
  • 4. public class My_Calculationextends Calculation { public void multiplication(intx, int y) { z=x*y; System.out.println("Theproduct of the given numbers:"+z); } public static void main(String args[]) { int a=20, b=10; My_Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Substraction(a, b); demo.multiplication(a, b); } }
  • 5. • Compile and execute the above code as shown below javac My_Calculation.java java My_Calculation After executing the program it will produce the following result. The sum of the given numbers:30 The difference between the given numbers:10 The product of the given numbers:200
  • 6. ∙ In the given program when an object to My_Calculation class is created, a copy of the contents of the super class is made with in it. That is why, using the object of the subclass you can access the members of a super class. ∙ The Superclass reference variable can hold the subclass object, but using that variable you can access only the members of the superclass, so to access the members of both classes it is recommended to always create reference variable to the subclass.
  • 7. ∙ If you consider the above program, you can instantiate the class as given below. But using the superclass reference variable ( cal in this case) you cannot call the method multiplication(), which belongs to the subclass My_Calculation. ∙ Calculation demo = new My_Calculation(); ∙ demo.addition(a, b); ∙ demo.Subtraction(a, b); ∙ Note − A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
  • 8. ∙ super.variable ∙ super.method(); ∙ ∙ Sample Code ∙ This section provides you a program that demonstrates the usage of the super keyword. ∙ In the given program, you have two classes namely Sub_class and Super_class, both have a method named display() with different implementations, and a variable named num with different values. We are invoking display() method of both classes and printing the value of the variable num of both classes. Here you can observe that we have used super keyword to differentiate the members of superclass from subclass.
  • 9. ∙ The super keyword ∙ The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used. ∙ It is used to differentiate the members of superclass from the members of subclass, if they have same names. ∙ It is used to invoke the superclass constructor from subclass. ∙ Differentiating the Members ∙ If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below.
  • 10. ∙ Example ∙ class Super_class { ∙ int num = 20; ∙ ∙ // display method of superclass ∙ public void display() { ∙ System.out.println("This is the display method of superclass"); ∙ } ∙ } ∙ ∙ public class Sub_class extends Super_class { ∙ int num = 10; ∙ ∙ // display method of sub class ∙ public void display() { ∙ System.out.println("This is the display method of subclass"); ∙ } ∙
  • 11. ∙ // Invoking the display() method of superclass ∙ super.display(); ∙ ∙ // printing the value of variable num of subclass ∙ System.out.println("value of the variable named num in sub class:"+ sub.num); ∙ ∙ // printing the value of variable num of superclass ∙ System.out.println("value of the variable named num in super class:"+ super.num); ∙ } ∙ ∙ public static void main(String args[]) { ∙ Sub_class obj = new Sub_class(); ∙ obj.my_method(); ∙ } ∙ }
  • 12. ∙ Compile and execute the above code using the following syntax. ∙ javac Super_Demo ∙ java Super ∙ ∙ On executing the program, you will get the following result − ∙ Output ∙ This is the display method of subclass ∙ This is the display method of superclass ∙ value of the variable named num in sub class:10 ∙ value of the variable named num in super class:20
  • 13. ∙ IS-A Relationship ∙ IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. ∙ public class Animal { ∙ } ∙ ∙ public class Mammal extends Animal { ∙ } ∙ ∙ public class Reptile extends Animal { ∙ } ∙ ∙ public class Dog extends Mammal { ∙ }
  • 14. ∙ With the use of the extends keyword, the subclasses will be able to inherit all the propertiesof the superclassexcept for the private propertiesof the superclass. ∙ Example ∙ class Animal { ∙ } ∙ ∙ class Mammal extends Animal { ∙ } ∙ ∙ class Reptile extends Animal { ∙ } ∙ ∙ public class Dog extends Mammal { ∙ ∙ public static void main(String args[]) { ∙ Animal a = new Animal(); ∙ Mammal m = new Mammal(); ∙ Dog d = new Dog(); ∙
  • 15. ∙ public static void main(String args[]) { Animal a = new Animal(); Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } }
  • 16. ∙ This will produce the following result − ∙ Output ∙ true ∙ true ∙ true
  • 17. ∙ Circulate Sprint ∙ Planning ∙ Summary ∙ Verify Day 1 Burn ∙ Down