Interface

  • implicitly abstract
  • A class can implement multiple interfaces
  • A class can only extend single class hence multiple inheritance is not possible
  • An interface can extend any number of interfaces.

Methods

  • Interface methods by default public and abstract so you can omit them.
  • Abstract methods cannot have a body.

Fields

  • Interface fields are by default public static and final and you can omit them.

@Override

  • Optional and compiler will check if the method is really being overridden.
  • it helps dev know the method is overriding

Polymorphism

types

  • Compile time aka static or early binding.
    • e.g. method overloading
  • Runtime aka dynamic or late binding.
    • e.g. method overriding - which method needs to be executed is decided at runtime
    • the process of resolving which method to execute is known as dynamic dispatch.
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        Animal animal = new Animal();
        if(args.length > 0) {
            if("cat".equals(args[0])){
                animal = new Cat();
            }
        }
        animal.walk();
    }
}

Abstract

  • An abstract class cannot be instantiated.
  • interface cannot be instantiated either as they are implicitly abstract.
  • If a class has an abstract method then the class must be marked abstract vice-versa is not true.
  • An abstract class can contain abstract and non-abstract methods.
  • If a class is extending an abstract class then it must implement all the abstract methods else the class must itself be abstract.
  • The constructor can be called by the constructor of the non abstract(concrete) child class.
  • All child class constructor must call parent constructor using super in the first line.

Abstract class vs Interface

  • Interfaces have implicitly public abstract methods while Abstract class does not.
  • With Abstract class you can only extend one class while we can implement multiple interfaces.
  • Abstract class can have constructor while an interface cannot.

Constructor

  • By default if you don’t define a constructor then java automatically creates a no args constructor for you.
  • If you define a constructor then java will not create any implicit constructor for you and you have to call your constructor to create objects.
  • You have to call parents constructor in the first line using super keyword if you don’t specify it then java will insert a call to parent’s no args constructor automatically i.e. super().
  • You can alternatively call another constructor using this keyword.
  • You must call constructor using super (parent) or this (overloaded current class) in the first line only.

Casting

  • On the basis of whether we explicitly specify the target data type they are of two types:
    • Implicit
    • Explicit
  • On the basis of whether target data type is super type or sub type they are of two types:
    • Up-casting
    • Down-casting
  • You can implicitly up-cast to a super type.
  • You must explicitly down-cast to a subtype.
  • In case of primitives down-casting happens when data type converts from higher precision to lower precision generally data is lost doing this.
double d = 3.2d;
int i = (int) d;
System.out.println(i); // 3
  • In case of non primitives down-cast will only succeed if the target data type follows IS-A relationship with a source type. ****
  • If down-casting fails it will throw ClassCastException at runtime.
  • Generally we check if down-casting is possible or not using instanceof operator.
Animal animal1 = new Animal("animal");
Animal animal2 = new Cat();
if (animal1 instanceof Cat) {
	Cat c = (Cat) animal1;
	System.out.println("animal1 is successfully type casted to Cat");
} else {
	System.out.println("animal1 cannot be type casted to Cat"); // will execute
}
 
if (animal2 instanceof Cat) {
	Cat c = (Cat) animal2;
	System.out.println("animal2 is successfully type casted to Cat"); // will exwcute
} else {
	System.out.println("animal2 cannot be type casted to Cat");
}