Abstract Class:-

  • A class contains characteristics as well as behaviors.
  • If a base class provides a behavior only in the syntactical manner but it unable to provide its definition then that behavior/function is called as an abstract function.
  • In case of a class which provides an abstract function means that class knows “what to do?” but that class does not know “how to do?” means in simple terms that class does not provide function definition.
  • A class which contains at least a single abstract method then that class is called as an abstract class.
  • We cannot create an object of an abstract class. In C++ as well as in JAVA.
  • But we can create a pointer of that class in C++ and reference of that class in JAVA.
  • This is responsibility of a derived class to provide its definition of function which is derived as an abstract in base class.
  • If the derived class is unable to provide its definition then that derived class can also provide an empty definition for that function which is define as an abstract function in base class.

e.g.

int fun(in,int)=0;

  • This is the syntax which is used in C++ to provide an abstract function.

abstract int fun(int,int);

  • This is the syntax which is used in JAVA to provide an abstract function.
  • If a class contains a single abstract function then that class is called as an abstract class.
  • In java, we have to write ‘abstract’ keyword before defining a class.

e.g.

abstract class base

{

abstract int fun(int,int);

void gun(int i)

{

// code

}

}

  • In this example, class base is considered as an abstract class which contains single abstract function and one normal function.

class derived extends base

{

int fun(int i, int i)

{

// code

}

}

  • In this case, we provide the definition of function fun() inside a derived class. Now onwards object of derived class.

base b = new derived();

b.fun(); // fun of derived class is called.

base b = new base(); // it is not allowed to create an object of base class.

There is no multiple inheritance in java because of the diamond problem.

But to get the multiple inheritance in java we can use the concept called as an Interface.

Interface:-

  • Interface is similar as an abstract class.
  • An abstract class contains abstract methods as well as non abstract methods but on the other hand an Interface contains only an abstract function.
  • In case of an abstract class we have to derived that class and provide the definition of an abstract function.
  • In case of an interface we have to implements that interface.
  • While implementing that interface we have to provide definition of an abstract function which is written in the interface.
  • It is not possible to use multiple inheritances for an abstract class but it is possible to use multiple inheritances in case of interface.
  • We can also create nested interface means interface which is written inside any class if there are n methods in an interface then the class which implements that interface has to provide definition for all n methods.
  • If that class is unable to provide definition of all n methods then compiler generates an error.
  • To avoid that error we can declare that class we can declare that class as an abstract class or we can provide an empty definition for that method.
  • An interface contains declaration as well as final instance variable.
  • An instance variable means normal variable without any storage class specifier.
  • Memory for all those instance variable is allocated separately for each and every object of that class if the variable is declared as a static variable then all the objects of a same class shares same copy of that static variable means that variable is created for a whole class and that variable is called as class variable.
  • Means for that variable is not allocated for that interface then memory is allocated inside the class which implements that interface.

e.g.

interface demo

{

void fun();

void gun();

void sun();

}

class base implements demo

{

void fun()

{

// code

}

void gun()

{

// code

}

void sun()

{

// code

}

}

class base2 implements demo

{

void fun()

{

// code

}

void gun()

{

// code

}

} // error because we have not given the method sun()

abstract class base3 implements demo

{

void fun(){…}

void gun(){…}

}

class base4 implements demo

{

void fun(){…}

void gun()

{

// empty definition

}

void sun()

{

// empty definition

}

} // allowed

  • In java, multiple inheritances is not allowed due to diamond problem.
  • But we can implement more that one interfaces simultaneously.

e.g.

class demo implements A,B

{

// code

}

  • In this case A and B are interfaces and that demo class has to provide definition for all the methods which are declared interface A as well as interface B.

 


Abstract Class

 

Dynamic call dispatch in an interface:-

  • The concept of dynamic call dispatch is directly applicable in case of interface.
  • As we can create a reference of an interface then that reference refers to such a class which implements that interface.
  • But we can refer any other class which not implements the method which are provided by the interface.

e.g.

interface demo

{

void fun();

void gun();

}

class base 1 implements demo

{

void fun()

{

System.out.println(“inside fun of base1.”);

}

void gun()

{

System.out.println(“inside gun of base1.”);

}

}

class base2 implements demo

{

void fun()

{

System.out.println(“inside fun of base2.”);

}

void gun()

{

System.out.println(“inside gun of base2.”);

}

}

class hello

{

public static void main(String[] args)

{

demo d;

d=new demo(); // error because we cannot create an object of an interface

d=new base1(); // allowed

d.fun(); // inside fun of base1

d.gun(); // inside gun of base1

d=new base2();

d.fun(); // inside fun of base2

d.gun(); // inside gun of base2

}

}

Nested Interface:-

  • We can define an interface inside any class.

e.g.

class demo

{

interface hello

{

void fun();

}

}

  • In this case, interface hello is a member of a class demo.

class ABC implements demo.hello

{

void fun()

{

// code

}

}

  • In this case, we implements interface hello which is defined inside a demo class and this concept is called as nested interface.

For more reading about technology news in singapore and seo to online marketing do view more about other pages.

 

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment