SlideShare a Scribd company logo
Inheritance
&
Polymorphism
Inheritance & OOP
• In java inheritance means deriving a class from existing class.
• This allows us to reuse code.
• We can use fields & methods from the existing class, because it is reusable.
• For example, the child inherits from his/her parents.
• The existing class called super class & the deriving class called subclass.
o Subclass (child): the class that inherits from another class
o Super class (parent): the class being inherited from
The Object Class
• As we will see, we can derive one class from another. For example, suppose we write a class called Product
for a sports shop.
• Basketball is a Product but is more specific than simply a Product.
• So, we can say following:
o Product is a class and has functionality that any products have.
o Basketball is a class and has all the functionality of a Product but perhaps more.
• So, the product is a super/base/parent class and the basketball is a child/sub class.
Example of Inheritance
• Polygon is the base/super/parent class. Learn these 3
terms!!
o Triangle “is a” Polygon and Rectangle “is a” Polygon.
o Square “is a” special Rectangle.
• We programming types use these terms:
o base, super or parent class
o child, sub, or derived class
o is a
Polymorphism
• Polymorphism is the ability of an object to take on many forms.
• It allows us to call superclass method from a derived class because of the “is a” relationship.
• We showed that inheritance lets us inherit attributes and methods from another class. Polymorphism uses
those methods to perform different tasks. This allows us to perform a single action in different ways.
• For example:
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
• Now, the Deer class is polymorphic since this has multiple inheritance. Following are true for the above
examples:
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object
Example of Polymorphism
• Superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats,
Dogs; And they also have their own implementation of an animal sound (the pig oinks, cat meows, etc.):
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
• Now we can create Pig and Dog objects and call the animalSound() method on both of them:
• For Example:
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
Continued to next page . . .
class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create an Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}

More Related Content

PPTX
13 inheritance
PPTX
Pi j3.1 inheritance
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPSX
Learn java objects inheritance-overriding-polymorphism
PDF
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
13 inheritance
Pi j3.1 inheritance
Inheritance in Java is a mechanism in which one object acquires all the prope...
‏‏‏‏‏‏oop lecture objectives will come.pdf
Learn java objects inheritance-overriding-polymorphism
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf

Similar to Inheritance & Polymorphism (20)

PPT
Inheritance in java
PPT
PPTX
Ayan Das_25300121057.pptx
PPTX
Java - Inheritance_multiple_inheritance.pptx
PPTX
INHERITANCE.pptx
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
Inheritance_in_OOP_using Python Programming
PPTX
Unit3 inheritance
PPTX
encapsulation, inheritance, overriding, overloading
PPTX
Understanding_of_Inheritance_in_Java_by_Quipoin.pptx
PPTX
Object Oriented Programming -Single Inheritance.pptx
PPT
Eo gaddis java_chapter_09_5e
PPT
Eo gaddis java_chapter_09_5e
PPTX
Unit3 part2-inheritance
PPTX
88 Inheritance - Part 1.pptx
PDF
Inheritance in Java.pdf
PDF
03-inheretance java code programming .pdf
PPTX
Object Oriented Programming.pptx
PPTX
Inheritance in c++
PDF
inheritance-16031525566nbhij56604452.pdf
Inheritance in java
Ayan Das_25300121057.pptx
Java - Inheritance_multiple_inheritance.pptx
INHERITANCE.pptx
Unit 3-Classes ,Objects and Inheritance.pdf
Inheritance_in_OOP_using Python Programming
Unit3 inheritance
encapsulation, inheritance, overriding, overloading
Understanding_of_Inheritance_in_Java_by_Quipoin.pptx
Object Oriented Programming -Single Inheritance.pptx
Eo gaddis java_chapter_09_5e
Eo gaddis java_chapter_09_5e
Unit3 part2-inheritance
88 Inheritance - Part 1.pptx
Inheritance in Java.pdf
03-inheretance java code programming .pdf
Object Oriented Programming.pptx
Inheritance in c++
inheritance-16031525566nbhij56604452.pdf
Ad

More from SAGARDAVE29 (12)

PPTX
Graphical User Interface (GUI)
PPTX
ArrayList in JAVA
PPTX
Exception Handling
PPTX
JAVA Multithreading
PPTX
Stack & Queue
PPTX
ArrayList in JAVA
PPTX
Threads in JAVA
PPTX
Exception handling
PPTX
More oop in java
PPTX
Coding Style & Tips for JAVA
PPTX
Some Important Methods in JAVA
PPTX
Recursion
Graphical User Interface (GUI)
ArrayList in JAVA
Exception Handling
JAVA Multithreading
Stack & Queue
ArrayList in JAVA
Threads in JAVA
Exception handling
More oop in java
Coding Style & Tips for JAVA
Some Important Methods in JAVA
Recursion
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
Complications of Minimal Access Surgery at WLH
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Trump Administration's workforce development strategy
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Yogi Goddess Pres Conference Studio Updates
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Classroom Observation Tools for Teachers
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Complications of Minimal Access Surgery at WLH
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
Trump Administration's workforce development strategy

Inheritance & Polymorphism

  • 2. Inheritance & OOP • In java inheritance means deriving a class from existing class. • This allows us to reuse code. • We can use fields & methods from the existing class, because it is reusable. • For example, the child inherits from his/her parents. • The existing class called super class & the deriving class called subclass. o Subclass (child): the class that inherits from another class o Super class (parent): the class being inherited from
  • 3. The Object Class • As we will see, we can derive one class from another. For example, suppose we write a class called Product for a sports shop. • Basketball is a Product but is more specific than simply a Product. • So, we can say following: o Product is a class and has functionality that any products have. o Basketball is a class and has all the functionality of a Product but perhaps more. • So, the product is a super/base/parent class and the basketball is a child/sub class.
  • 4. Example of Inheritance • Polygon is the base/super/parent class. Learn these 3 terms!! o Triangle “is a” Polygon and Rectangle “is a” Polygon. o Square “is a” special Rectangle. • We programming types use these terms: o base, super or parent class o child, sub, or derived class o is a
  • 5. Polymorphism • Polymorphism is the ability of an object to take on many forms. • It allows us to call superclass method from a derived class because of the “is a” relationship. • We showed that inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. • For example: public interface Vegetarian{} public class Animal{} public class Deer extends Animal implements Vegetarian{} • Now, the Deer class is polymorphic since this has multiple inheritance. Following are true for the above examples: A Deer IS-A Animal A Deer IS-A Vegetarian A Deer IS-A Deer A Deer IS-A Object
  • 6. Example of Polymorphism • Superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs; And they also have their own implementation of an animal sound (the pig oinks, cat meows, etc.): class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } } class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } }
  • 7. • Now we can create Pig and Dog objects and call the animalSound() method on both of them: • For Example: class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } } class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } } Continued to next page . . .
  • 8. class MyMainClass { public static void main(String[] args) { Animal myAnimal = new Animal(); // Create an Animal object Animal myPig = new Pig(); // Create a Pig object Animal myDog = new Dog(); // Create a Dog object myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); } }