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