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
Java chapter 5
PPTX
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
Java presentation
PPTX
Unit No 3 Inheritance annd Polymorphism.pptx
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
13 inheritance
Java chapter 5
‏‏‏‏‏‏oop lecture objectives will come.pdf
Java presentation
Unit No 3 Inheritance annd Polymorphism.pptx
Inheritance in Java is a mechanism in which one object acquires all the prope...

Similar to Inheritance & Polymorphism (20)

PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PPTX
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
PPTX
some basic knowledge about Java programming
PPT
PPTX
Inheritance and Polymorphism
PPTX
Inheritance and Polymorphism Java
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPTX
Java Lecture5.pptx
PPTX
Object Oriented Programming.pptx
PPTX
Unit3 inheritance
PPTX
java_inheritance_oop_20250730110153.pptx
PPTX
Unit3 part2-inheritance
PPT
Java Programming - Polymorphism
PPT
Lecture d-inheritance
PDF
Polymorphism in Java by Animesh Sarkar
PPTX
Core java oop
PPT
Chapter 8 Polymorphism
PPTX
Pi j3.1 inheritance
PDF
Java Polymorphism: Types And Examples (Geekster)
OOPS_Unit2.inheritance and interface objected oriented programming
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
some basic knowledge about Java programming
Inheritance and Polymorphism
Inheritance and Polymorphism Java
Java programming -Object-Oriented Thinking- Inheritance
Java Lecture5.pptx
Object Oriented Programming.pptx
Unit3 inheritance
java_inheritance_oop_20250730110153.pptx
Unit3 part2-inheritance
Java Programming - Polymorphism
Lecture d-inheritance
Polymorphism in Java by Animesh Sarkar
Core java oop
Chapter 8 Polymorphism
Pi j3.1 inheritance
Java Polymorphism: Types And Examples (Geekster)
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
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
IP : I ; Unit I : Preformulation Studies
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Journal of Dental Science - UDMY (2021).pdf
PPTX
Module on health assessment of CHN. pptx
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
Farming Based Livelihood Systems English Notes
PDF
Climate and Adaptation MCQs class 7 from chatgpt
PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PDF
M.Tech in Aerospace Engineering | BIT Mesra
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
PPTX
INSTRUMENT AND INSTRUMENTATION PRESENTATION
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
Literature_Review_methods_ BRACU_MKT426 course material
Environmental Education MCQ BD2EE - Share Source.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
What’s under the hood: Parsing standardized learning content for AI
IP : I ; Unit I : Preformulation Studies
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Journal of Dental Science - UDMY (2021).pdf
Module on health assessment of CHN. pptx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Farming Based Livelihood Systems English Notes
Climate and Adaptation MCQs class 7 from chatgpt
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
CRP102_SAGALASSOS_Final_Projects_2025.pdf
M.Tech in Aerospace Engineering | BIT Mesra
Cambridge-Practice-Tests-for-IELTS-12.docx
Everyday Spelling and Grammar by Kathi Wyldeck
INSTRUMENT AND INSTRUMENTATION PRESENTATION
Journal of Dental Science - UDMY (2022).pdf
International_Financial_Reporting_Standa.pdf
Literature_Review_methods_ BRACU_MKT426 course material

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(); } }