Bloggers unite

"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." – Albert Einstein


Abstract and Interface


Abstract Class: An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static fields and static methods. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

An abstract method is a method that is declared without an implementation (without braces and followed by a semicolon), like this:

abstract void sum(int a, int b);


 

Consider using abstract classes if any of these statements apply to your situation:

    1. You want to share code among several closely related classes.

       

 

  • You expect that classes that extend your abstract class have many common methods or fields or require access modifiers other than public (such as protected and private).

     

 

 

  • You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

     

 

 

 

Interface: An interface is just the declaration of methods of an object; it’s not the implementation. In an interface, we define what kind of operation an object can perform. These operations are defined by the classes that implement the interface. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.

interface Vehical { // declaration void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement);

}
	

class Car implements Vehical { // implementation

int speed = 0;

int gear = 1;

void changeGear(int newValue) {

gear = newValue;

}

void speedUp(int increment) {

speed = speed + increment;

}

void applyBrakes(int decrement) {

speed = speed – decrement;

}

void printStates() {

System.out.println(” speed:” + speed + ” gear:” + gear);

}

}

Consider using interfaces if any of these statements apply to your situation:

  1. You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.

  2. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.

  3. You want to take advantage of multiple inheritances.



Leave a comment