Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 30 July 2017

Difference Between Abstract Class and Interface In Java

Java Abstract Class vs Interface

Java Abstract Class vs Interface

Now, Here we will learn what is the difference between abstract class and interface. This topic is very important for java interview point of view.

So let's starts.

Abstract Class

  • An abstract keyword is used to declare the abstract class.
  • An abstract class can extend only one class or one abstract class at a time.
  • An abstract class can extend from a class or from an abstract class.
  • It can contain abstract method (without body) and non-abstract method(with a body).
  • It doesn't support multiple inheritance.
  • It can have static or non-static variables and final or non-final variables and static final variables.


  • It can provide the implementation of an interface.
  • By the help of the abstract class, we cannot achieve full abstraction.
  • We can declare a constructor inside the abstract class.
  • In an abstract class, the data members are not public, static and final by default.
  • In an abstract class, the method is not public and abstract by default.
  • We can use public, private and protected access modifiers with the abstract class method.


Syntax :

(1) How to declare an abstract class?

abstract class Test
{
-----
-----
}

(2) How to declare an abstract method(without body) or non-abstract method(with a body) in the abstract class.

abstract class Test
{
abstract void show();//abstract method
void show()
{
-----
-----
}
}



Java Abstract Class Example

In this example, we are going to use one abstract method and non-abstract method in an abstract class.

abstract class Tiger
{
abstract void eat();//abstract method
void run()
{
System.out.println("Lion run fast");
}
}
class Dog extends Tiger
{
void eat()//provide impelementation of Tiger eat() method
{
System.out.println("Dog eat cat");
}
public static void main(String args[])
{
Dog d = new Dog();
d.eat();
d.run();
}
}

Output : Dog eat cat
              Lion run fast


Java Interface

  • The interface keyword is used to declare the interface.
  • An interface can extend more than one interface at the same time.
  • An interface can extend only from an interface i.e a class implements interface but an interface extends another interface not implement.
  • An interface can have the only abstract method but since java 8  it can have default and static method also.
  • By using interface, we can achieve multiple inheritance.
  • In an interface, all the data members or variables are by default public static and final.
  • An interface cannot provide the implementation of the abstract class.
  • By using the interface, we can easily achieve the full abstraction.
  • We can't declare the constructor inside the interface.
  • In an interface, all the data members are by default public static and final.
  • In an interface, all the method are public abstract by default.
  • But here we cannot use private, protected access modifiers with interface method because interface method is by default public.

Java Interface Example

interface Parent
{
void task();//by default public and abstract
public default void drink()
{
System.out.println("Parent drink water");
}
}
class Child implements Parent
{
public void task()
{
System.out.println("Parent handle child");
}
public static void main(String args[])
{
Child c = new Child();
c.task();
c.drink();
}
}

Output : Parent drink water
Share:

4 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate