Tuesday 18 June 2019

Differences between Abstract class and interface? tccicomputercoaching.com

What is Interface?

The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.
An interface cannot be instantiate. However, classes that implement interfaces can be instantiated.


What Is Abstract Class?

A class which has the abstract keyword in its declaration is called abstract class. Abstract classes should have at least one abstract method. , i.e., methods without a body. It can have multiple concrete methods.

Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.

Abstract classes cannot be instantiated.

Let us see Differences between An interface and an Abstract class.
  1. Interfaces are used to achieve abstraction. Abstract classes offer default functionality for the subclasses.
  2. Everything defined inside the interface is assumed public modifier. Abstract Class can have an access modifier.
  3. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
  4. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  5. A Java class can implement multiple interfaces but it can extend only one abstract class.
  6. The interface cannot contain data fields. the class can have data fields.
  7. An interface is abstract so that it can't provide any code. An abstract class can give complete, default code which should be overridden.
  8. An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces.
  9. An interface cannot declare constructors or destructors. An abstract class can declare constructors and destructors.
  10. It can extend any number of interfaces. It can extend only one class or one abstract class at a time.
  11. In an abstract interface keyword, is optional for declaring a method as an abstract. In an abstract class, the abstract keyword is compulsory for declaring a method as an abstract.
  12. An interface can have only public abstract methods. An abstract class has protected and public abstract methods.
13. Syntax:
1. Interface
               interface name
               {
                               //methods
               }
               
               2. abstract class
               abstract class name
               {
               // code
               }

To learn more about Java Programming Languages You can join at 

TCCI Computer Coaching Institute in Ahmedabad.


Call us @ 9825618292

Visit us @ www.tccicomputercoaching.com

No comments:

Post a Comment