Why interface must be present after implementation? | Java Tutorial

This Java Tutorial “Why interface must be present after implementation?” is providing clear information about why we are maintaining interface after implementing all the specifications of interface in java application.


 

Observe the following Code:

  • We are overriding 2 specifications of interface into class in the following program.
  • Every class implements all the specification of interface.

 

  • Interface is a standard set of specifications.
  • Every standard(interface) must be implemented with a Duplicate name(class).
  • After it’s implementation with duplicate name(class), we need to set Standard (interface) name to that object.

 

Note: If one object definition(AssembledComputer) is based on multiple interfaces(Standards like Intel,AMD,LG), we cannot provide one standard name to the entire object.

 

Question :

  • Why it is allowed to store Child object address into Parent reference variable?
    Lenovo obj = new HarshaComputer();
    Parent obj = new Child();
    Owner obj = new Tenant();

Answer:

  • Java supports Primitive Casting and Object casting.
  • Casting can be either implicit or explicit.
  • In java, Storing Parent object address into Child type variable is called Up-casting.
  • Up-casting is implicit in java application.
Share this