ABSTRACT CLASS AND INTERFACE

ABSTRACT CLASS

  • Abstract function is a function which is just declared but not defined.
  • Abstract function is only declared in Abstract Class.
  • Abstract class may contain concrete methods.
  • Abstract class supports run-time polymorphism.
  • Objects of Abstract classes can’t be created ; only reference variables can be created.

Example :


class A{
abstract void meth();   //Abstract method
}

class B extends A{
void meth(){           //Definition of Abstract method provided by Child class B
System.out.println("B");
}

public static void main(String args[]){
A ref= new B();
ref.meth();
}


Interface

Interface  is more refined version of Abstract class.It is a collection of only abstract methods. Multiple Implementation is possible through interface. Methods in interface are public and abstract by default.

Example :


interface A{
void meth();   //Abstract method by default
}

class B implements A{
void meth(){
System.out.println("B");
}

public static void main(String args[]){
A ref= new B();
ref();
}

Leave a comment