Serious Polymorphism — Chapter 8

Upulie Handalage
5 min readApr 27, 2022

--

An interface and abstract classes

It is a 100% abstract class. It’s a class that can’t be instantiated (are not specific enough)(to stop saying new on that object)(However, you can still use it to create arrays). An interface can be used as a reference type, a polymorphic argument/return type or to make a polymorphic array. An abstract class has virtually no use unless its extended. The only exception is when an abstract class has static members (which is when you could use them).

A lot of classes from the API are abstract classes, especially the GUI library.

abstract class Canine extends Animal{

//override the methods from Animal class here

}

Abstract methods

Abstract methods can be created, in order to signify that the method must be overridden in the inherited classes. Abstract method has no body ({}).

public abstract void eat();

If you declare a method as abstract, then the class holding it should be abstract (you can’t have an abstract method in a non-abstract class). However, an abstract class can have a mixture of abstract and non-abstract methods.

When implementing, a body, with the same method signature (names and arguments) and a return type of the abstract method.

Object class

It is a non-abstract class which has method implementions,

  • some could be overridden — hashCode(),equals(),toString()
  • some are marked final — getClass()

Objects of the Object class could be created to used in thread synchronization.

Common methods

  • Find out if one object equal to another — a.equals(c) — returns boolean
  • Find out the actual class type of the object — c.getClass() — returns Class
  • Have a hashcode for the object (to be used in hashtables)—c.hashCode() returns int
  • To print a string message for the object — c.toString() — returns String

When objects are referred to by an Object reference type, JVM automatically assumes it is an instance type. So, only methods allowed to call on that object are the 4 above. Therefore, the below line will give compiler error (Java is a strongly-typed language).

Object o = new Ferrari();

o.drive();

Using Object as the reference type

Getting elements out of an ArrayList<Object>􀁍will always return a reference type of object. So,

Example 1

ArrayList<Object>􀀀dog= new ArrayList<Dog>();

Dog d = dog.get(0); //will give a compile error (So do a cast)

Object d = dog.get(0); //works (because to the JVM what’s inside the ArrayList are Objects, and has lost its type and all its attributes and methods)

Example 2

public void go(){

Dog aDog = new Dog();

Dog sameDog= getObject(aDog); //Compiler error. Change Dog->Object or cast to Dog

}

public Object getObject(Object o){

return o;

}

Casting

If you are sure that a higher class object can be turned into a more specific object, do a casting.

Snowboard s1 =new Snowboard(); //s can do all a snowboard can

Object o=s1; //o only has Object capabilities. Snowboard capabilities are lost Snowboard s2 = (Snowboard) o; //Do a cast to retrieve Snowboard capabilities

If not sure, use instanceof operator to check. If wrong a ClassCastException will occur at runtime.

if(o instanceof Dog){

Dog d = (Dog) o;

}

Interfaces

Used to add a single behaviour in few of the sub classes. Guarantees that all the sub classes have all the methods defined.

Java doesn’t have multiple inheritance as it leads to the deadly diamond of death(DDD).

All the methods in an interface are abstract (So even if both a class and an interface is inherited and implemented to a class, the JVM knows exactly which method to give precedence to)(As all the methods in an instance should be overridden).

public interface Pet{

//all the abstract methods marked with the modifier

}

public class Dog extends Canine implements Pet{

// always follow the order of precedence in extends A implements B

}

With the usage of interfaces, a class doesn’t have to come from just one inheritance tree. (Apply interfaces according to the role an object plays, not by its class)(a class defines — who you are, an interface defines — the roles you play)

Advantages of Interfaces

  • When using interfaces, the objects can be from anywhere in the inheritance tree. However, the class has to implement the interface.
  • Commonly used in Java API, Eg:
  1. To save a state to a file — implement Serializable
  2. Need objects to run their methods in a separate thread of execution — implement Runnable
  3. A class can inherit multiple interfaces (unlike classes)

public class Dog extends Animal implements Pet, Saveable, Paintable{}

super()

The super keyword is really a reference to the super class portion of an object,. Using super.runReport(); on the first line, first runs all the code from the parent before continuing with the next lines of code on the child.

Note

  • Any class that doesn’t explicitly extend from some class, always implicitly extends the Object class.
  • The compiler decides whether you can call a method based on the reference type, not the actual object type.
  • public methods in a class are expose a class’s capabilities to the outside world.
  • Every time the dot operator is used on an object (a.doStuff()), the compiler looks at its reference(of the reference variable)(not the class of the actual object) to see if the used method is available.
  • Syntax for defining an interface is “public interface Pet{}”
  • Interface methods are implicitly public and abstract(so mentioning public and abstract is not needed)
  • Abstract methods don’t have a body so public abstract void beFriendly();
  • Use an abstract class when you want to define a template for a group of subclasses, and you have at least some implementation code that all subclasses could use. When you want to guarantee that nobody can make objects of that type.
  • Use an interface when you want to define a role that other classes can play, regardless of their position in the inheritance tree.

Vocabulary

type-safety — One of the Java’s greatest protection mechanisms for the code.

concrete class — The opposite of abstract. The classes that are specific enough to be instantiated. These are the classes used to create objects with. The very first concrete sub-class should implement all of the abstract methods from all of its abstract/interface parents.

deadly diamond of death — a problem which occurs due to having two parents that inherit from a single grand parent. The problem comes when the child is in question which parent to choose the inherited method from.

Thank you for reading! Until next time 👋🏽

--

--

Upulie Handalage

Everything in my point of view. Here for you to read on....