ICS4U Grade 12 – Computer Science – Inheritance and Polymorphism

Grade 12 – Computer Science

 

Polymorphism and Inheritance – A Guide to Programming in Java Ch 9

 

Intro

  • Inheritance extends on an existing class
  • Polymorphism is something you can do to classes that are being inherited
  • You might want to extend a class by adding more specific items into the object
    • A car class might be extended to “BMW” class where it has BMW specific methods and variables
    • Or a Circle class might be extended to “Disk” and accepts the input of height.

 

Inheritance

  • Inheritance: when you extend classes from a “super class” to make it do more specific things
    • “allows a class to define a specialized type of an already existing class”
    • Is-a relationship: an extended class “is a” type of another class
      • BMW “is a” type of a car class. Disk “is a” type of circle class.

 

  • Implementation:
    • extends: a keyword you must use to declare a “sub class”
      • this keyword will make it refer to the super class
      • public class <name> extends <superclass_name> { … }
    • base class: or “super class: is the class which this sub class is based on
    • derived class: or “sub class” often contains overridden methods or new data from the base class
    • super: is a keyword used to access methods in the base class
      • Ex. super(r) from the BMW class will call the constructor of the super class (car) , and pass variable r into it.
    • Members that are private cannot be accessed by derived classes
      • Hence, accessor methods are used to get values (ie getWeight(), getRadius())
    • Inherited Methods can be called directly.
      • If M3 is the object of BMW class, and you call carStart() from client code. If BMW class doesn’t have it’s own carStart() method, but Car class does, since BMW inherited the Car class, the client code doesn’t care and still executes carStart().

 

Polymorphism

  • Polymorphism: an OOP property where objects have the ability to assume different types.
    • Polymorphism is based on inheritance
    • Since a subclass is derived from a super class, a super class object can reference an object of the subclass

 

ie Car honda;

BMW 320i = new BMW(<arguments>);

honda = 320i  //honda now references 320i

        • Since BMW is inherited from Car class, any object made from Car can be “morphed” into another object made by a subclass of Car, in this case, a BMW 320i object.

 

    • Overridden methods from the super class can be called from the sub class.
      • From the earlier example, the method toString() originally in Car was overridden in BMW. Even though honda was made by Car class, then later morphed into a BMW object, I can still call the toString() method overridden by BMW.

 

Abstract Classes

  • Abstract Classes: are used to model abstract concepts.
    • For example, a “vehicle” is an abstract object, you cannot have a “vehicle” vehicle. You can have cars, trucks, BMWs, Hondas which are in the form of vehicles.
    • Abstract classes are instantiated but do not represent the objects
    • They only have general details and actions of a type of object
      • Example: a Vehicle abstract class may only have number of wheels, or number of seats. Later, another class, like Car, can inherit the Vehicle class and add more details like colour, make, brand, etc.
    • abstract: Abstract classes are declared with abstract keyword
      • All abstract classes are intended to be inherited
      • Only public members of abstract classes are visible to derived objects
        • abstract class <class name> { .. }
    • Abstract Methods: methods declared with abstract keyword but contains no body
      • They are meant to be overridden in derived classes
        • abstract <return type>  <method name>;

 

Interfaces

  • Interface: a class with method declarations but no implementations
    • Interfaces cannot be inherited, they must be imported
    • Interfaces always has abstract methods
    • You can only implement an interface class, but it is not part of the hierarchy
      • <access level> interface <name> { .. methods .. }
    • Comparable Interface: one example of an interface in the java.lang package
      • CompareTo(Object obj) : returns 0 when obj is same as the object, a negative integer if obj is less than the object, and positive integer if obj is greater than the object.
      • A class must implement each method defined in the interface
    • Multiple Interfaces: A class can have multiple interfaces
      • When more than 1 interface is implemented, the interface names are separated by commas in the class declaration.