Abstraction Interview Questions

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.
An abstract method is declared in the parent class when we want a class which contains a particular method but on the other hand we want its implementation to be determind by child class.

An interface can be extended by another interface in Java. The code for the same would be like as shown below:
// 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. If that particular interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from. As shown in the example above, if we have a class that implements the FourLegs interface, then that class must define the method headings in both the 'FourLegs' interface and the 'Body' interface.
An abstract class may have many instance methods which sport default behavior. On the other hand, an interface cannot implement any default behaviour. However, it can declare different constants and instance methods. Whereas an interface has all the public members, an abstract class contains only class members like private, protected and so on.
Marker interface is an interface with no fields or methods in Java. Uses of marker interface are as follows:
  • We use marker interface to tell java compiler to add special behavior to the class implementing it.
  • Java marker interface has no members in it.
  • It is implemented by classes in order to get some functionality.
For instance when we want to save the state of an object then we can implement serializable interface.
A class containing abstract method is called an abstract class. An Abstract class cannot be instantiated.

Example of Abstract class :


abstract class testAbstractClass
{
protected String myString;
public String getMyString()
{
return myString;
}
public abstract string anyAbstractFunction();
}


In Java an interface just defines the methods and not implement them. Interface can include constants.
A class that implements the interfaces is bound to implement all the methods defined in an interface.
Example of Interface :


public interface sampleInterface 
{
public void functionOne();
public long CONSTANT_ONE = 1000;
}

  • java.util.Comparator
  • java.util.Comparator:compares some other classes instances,
  • java.lang.Comparable
  • java.lang.Comparable:compares another object with itself.
An abstract class is a class which contains one or more abstract methods and has to be implemented by sub classes. An interface is a Java Object which contains method declaration but does not contain its implementation. The classes which implement the interfaces must provide the method definition for all the methods. An abstract class has a class prefix with an 'abstract' keyword followed by 'class definition'. On the other hand interface starts with an 'interface' keyword. An abstract class may contain one or more abstract methods whereas an interface contains all abstract methods and final declarations. Abstract classes are useful in a situation where general methods need to be implemented while specialization behavior should be implemented by child classes. Interfaces are useful in a situation where all properties need to be implemented.

---Difference between an interface and an abstract class is as follows:

* Interfaces provide a kind of multiple inheritance. A class can extend only one class.
* Interfaces are limited to public methods and constants with no implementation of methods. Abstract classes can have a partial implementation,protected methods, static methods, etc.
* A class may implement several interfaces. But in case of an abstract class, a class may extend only one abstract class.
* Interfaces are slow as it requires extra direction to find corresponding method in the actual class whereas abstract classes are fast.

---Similarities :

*Neither abstract classes nor interfaces can be instantiated.