Skip to main content

SUMMARY

Runtime Polymorphism

  1. Runtime polymorphism can be achieved using an instance method with a superclass reference variable which contains a subclass object. Then method calls depend on the type of object available in the variable.

  2. When you are calling a static method with a superclass reference variable, which contains a subclass object, then method calls depend on the type of reference variable.

  3. When you are calling a method with superclass reference variable, which contains subclass object, then at runtime JVM will verify the object type and invoke the method accordingly.

  4. When you have variables in superclass and subclasses with the same name, then:

    a. When you call variable with superclass reference which contains subclass object, then variable from superclass only is invoked.

    b. When you call variable with subclass reference which contains subclass object, then variable from subclass only is invoked.

    c. When you downcast superclass reference into subclass reference, then subclass variables can be accessed.

  5. Object reference variable should be available in superclass variable when you define the method with variables having the same name in superclass and subclass.


Abstract Classes and Methods

  1. When you don't know the implementation of a method, then you can avoid the body of the method by declaring the method as abstract.
  2. When you want to force subclasses to provide their own implementation for some methods, then declare those methods as abstract.
  3. When a class contains one or more abstract methods, then that class must be declared as abstract.
  4. When a class contains one or more instance variables, constructors, instance methods, static methods, and blocks, then you can define one or more abstract methods in the same class.
  5. When you don't want to instantiate the class, then you can declare that class as abstract.
  6. When a class extends an abstract class, then that class has to override all the abstract methods; otherwise, the subclass must also be declared abstract.
  7. An abstract class can contain one or more abstract methods.
  8. You can achieve reference polymorphism using abstract classes.
  9. You cannot instantiate an abstract class, but you can declare a reference variable for it.
  10. Abstract keyword must be used for class and method only. It cannot be used for variables either instance or static or local primitive or reference.
  11. Methods declared as abstract must be overridden in concrete class.
  12. Abstract class cannot be final.
  13. You cannot use following modifiers with abstract methods:
  • private
  • static
  • strictfp
  • synchronized
  • final
  • native
  1. Static members of an abstract class can be accessed with class name.
  2. Instance members of an abstract class will be accessed with subclass object.
  3. When you create the object of subclass then instance block and constructor of abstract class will be called.

Interfaces

  1. You can use implements keyword to inherit the functionality of an interface.

  2. When you write subclass of an interface then:

    a. Subclass can use all the final static variables.

    b. When subclass overrides all the methods in implemented interfaces, then subclass is concrete.

    c. When subclass is not implementing one or more methods, then subclass must be abstract.

  3. You cannot instantiate interface i.e. object of interface cannot be created.

  4. You can declare the reference variable of interface.

  5. When you write subclass of interface implementing these methods remember to use public modifier in subclasses.

  6. When you are calling a member by interface reference variable which contains subclass object then that member should be available in interface.

  7. Variables declared in interface are public static final by default.

  8. When you are implementing two or more interfaces which contain common methods then one implementation is enough. The implementation only once and single implementation will be valid for all the interfaces.

  9. When you are implementing two or more interfaces which contain common variables and you are calling those variables with reference to that variable in subclass, it will be ambiguous. So to resolve ambiguity you have to use interface name.

  10. You cannot use following modifiers for interface variables:

  • private
  • protected
  • volatile
  • transient

32) You cannot use following modifiers for interface methods:

  • private
  • protected
  • static
  • final
  • strictfp
  • synchronized
  • native

33) You cannot define following members in interface:

  • Instance Variables
  • Constructors
  • Static Methods
  • Initialization Blocks

34) When you define an interface without any members then it is called as:

  • MARKER INTERFACE or
  • EMPTY INTERFACE or
  • TAGGED INTERFACE

Misc (Imp)

35) You cannot assign one reference variable to another reference without having any inheritance relationship between corresponding classes.

Example:

classHello{}

classHai{}

Hellohello=newHello();

Haihai=newHai();

hello=hai;// Invalid

hai=hello;// Invalid

36) You can assign subclass object to super type reference variable directly. This process is called as UPCASTING.

Example:

classHello{}

classHaiextendsHello{}

Hellohello=newHello();

Haihai=newHai();

hello=hai;// Valid -> Upcasting

37) You cannot assign super class object to sub type reference variable directly. You can do it by using explicit type casting. This process is called as DOWNCASTING.

Example:

classHello{}

classHaiextendsHello{}

Hellohello=newHai();

Haihai=hello;// Invalid

Haihai= (Hai)hello;// Valid -> Downcasting

38) When you perform down casting it may result in ClassCastException

Ex:

classA {}

classBextendsA {}

classCextendsA {}
Aaobj=newA();

Bbobj= (B)aobj;// ClassCastException

Ccobj= (C)aobj;// ClassCastException
Aaobj=newB();

Bbobj= (B)aobj;// Valid

Ccobj= (C)aobj;// ClassCastException
Aaobj=newC();

Bbobj= (B)aobj;// ClassCastException

Ccobj= (C)aobj;// Valid

39) You can use instanceof operator before down casting to check whether required object is available in reference variable.


40) When you create the array of a class then that array can hold either same class object or subclass object but not superclass object.


41) When you create the array of an abstract class then that array can hold objects of subclass of that abstract class.


42) When you create the array of an interface then that array can hold objects of subclass of that interface.

43) When you store object in array it may give ArrayStoreException.

Ex:

classA {}

classBextendsA {}

classCextendsB {}

classDextendsA {}
Aarr[]=newB[2];

arr[0]=newB();// Valid

arr[0]=newC();// Valid

arr[0]=newD();// ArrayStoreException