Java By Kiran
Java Training Institute in Pune
Java Training Institute in Pune
It refers to the ability to make a class abstract in OOP.
It helps to reduce the complexity and also improves the maintainability of the system..
These classes cannot be instantiated and are either partially implemented or not at all implemented.
This class contains one or more abstract methods which are simply method declarations without a body.
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.
Ans: Yes, an interface can extend another interface in Java.
This is what the code for something like that would look like:
// this interface extends from the Body interface:
public interface FourLegs extends Body
public void walkWithFourLegs( );
Remember that any class that implements an interface must implement the method headings that are declared in that interface.
And, if that interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from.
So, in the example above, if we have a class that implements the FourLegs interface,
then that class must have definitions for any method headings in both the FourLegs interface and the Body interface.
An abstract class may have instance methods, which can implement a default behaviour.
On the other hand, an interface can’t implement any default behaviour.
However, it can declare different constants and instance methods.
While an interface has all the public members, an abstract class contains only class members like private, protected and so on.
Marker interface in Java is interfaces with no field or methods.
Uses of Mark Interfaces are following:
• We use Marker interface to tell java compilerto add special behavior to the class implementing it.
• Java marker interface has no members in it.
• It is implemented by classes in get some functionality.
Example: when we want to save the state of an object then we implement serializable interface.
Yes,an inner class may be built an Interface
public interface xyz
static int p=0;
public static void main(String c)
A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.
Example of Abstract class :
abstract class testAbstractClass
protected String myString;
public String getMyString()
public abstract string anyAbstractFunction();
In Java Interface defines the methods but does not implement them. Interface can include constants.
A class that implements the interfaces is bound to implement all the methods defined in Interface.
Example of Interface :
public interface sampleInterface
public void functionOne();
public long CONSTANT_ONE = 1000;
• java.util.Comparator compares some other class’s instances,
• java.lang.Comparable compares another object with itself .
Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes.
Interface is a Java Object containing method declaration and doesn't contain implementation.
The classes which have implementing the Interfaces must provide the method definition for all the methods Abstract class is a Class prefix with a abstract keyword followed by Class definition.
Interface is a Interface which starts with interface keyword.Abstract class contains one or more abstract methods.
where as Interface contains all abstract methods and final declarations
Abstract classes are useful in a situation that Some general methods should be implemented and specialization behavior should be implemented by child classes.
Interfaces are useful in a situation that all properties should be implemented.
---Differences are as follows ::
* Interfaces provide a form of multiple inheritance. A class can extend only one other class.
* Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
* A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
* Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.
* Neither Abstract classes or Interface can be instantiated.