javascript
Brief description  about Online courses   join in Online courses
OR

Interface vs Abstract Class

Shuchita   Shukla
Shuchita Shukla
Senior Java Developer

an abstract class can have a mix of abstract and non-abstract methods, so some default
implementations can be defined in the abstract base class. An abstract class can also have static
methods, static data, private and protected methods, etc. In other words, a class is a class, so it
can contain features inherent to a class. The downside to an abstract base class, is that since their
is only single inheritance in Java, you can only inherit from one class.
• an interface has a very restricted use, namely, to declare a set of public abstract method
singatures that a subclass is required to implement. An interfaces defines a set of type
constraints, in the form of type signatures, that impose a requirement on a subclass to implement
the methods of the interface. Since you can inherit multiple interfaces, they are often a very
useful mechanism to allow a class to have different behaviors in different situations of usage by
implementing multiple interfaces

An abstract class is a class that leaves one or more method implementations unspecified by
declaring one or more methods abstract. An abstract method has no body (i.e., no implementation). A
subclass is required to override the abstract method and provide an implementation. Hence, an
abstract class is incomplete and cannot be instantiated, but can be used as a base class.
abstract public class abstract-base-class-name {
// abstract class has at least one abstract method
public abstract return-type abstract-method-name ( formal-params );
... // other abstract methods, object methods, class methods
}
public class derived-class-name extends abstract-base-class-name {
public return-type abstract-method-name (formal-params) { stmt-list; }
... // other method implementations
}
It would be an error to try to instantiate an object of an abstract type:
abstract-class-name obj = new abstract-class-name(); // ERROR!
That is, operator new is invalid when applied to an abstract class.

 

Write your comment now