Proxy > Gmail Facebook Yahoo!

Java Abstract class and Interface Part-2


Polymorphism means one name, many forms. There are 3 distinct forms of Java Polymorphism;
  • Method overloading (Compile time polymorphism)
  • Method overriding through inheritance (Run time polymorphism)
  • Method overriding through the Java interface (Run time polymorphism) Polymorphism allows a reference to denote objects of different types at different times during execution. A super type reference exhibits polymorphic behavior, since it can denote objects of its subtypes.
interface Shape {

 public double area();
 public double volume();

class Cube implements Shape {

 int x = 10;
 public double area( ) {

     return (6 * x * x);

 public double volume() {
  return (x * x * x);


class Circle implements Shape {

 int radius = 10;
 public double area() {
  return (Math.PI * radius * radius);
 public double volume() {
  return 0;

public class PolymorphismTest {

 public static void main(String args[]) {
  Shape[] s = { new Cube(), new Circle() };
  for (int i = 0; i < s.length; i++) {
   System.out.println("The area and volume of " + s[i].getClass()
     + " is " + s[i].area() + " , " + s[i].volume());

&lt;br /&gt;&lt;font size=-1&gt;
The area and volume of class Cube is 600.0 , 1000.0
The area and volume of class Circle is 314.1592653589793 , 0.0
Download Java Polymorphism Program Example
The methods area() and volume() are overridden in the implementing classes. The invocation of the both methods area and volume is determined based on run time polymorphism of the current object as shown in the output.

Interface vs Abstract Class

Interface vs Abstract Class
Taken from
1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn’t contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.
2. Abstract class definition begins with the keyword “abstract” keyword followed by Class definition. An Interface definition begins with the keyword “interface”.
3. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses
4. All variables in an Interface are by default - public static final while an abstract class can have instance variables.
5. An interface is also used in situations when a class needs to extend an other class apart from the abstract class. In such situations its not possible to have multiple inheritance of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.
6. An Interface can only have public members whereas an abstract class can contain private as well as protected members.
7. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.
8. The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass
9. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast
10.Interfaces are often used to describe the peripheral abilities of a class, and not its central identity, E.g. an Automobile class might
implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.
Note: There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.
Note: If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they
share is a set of method signatures, then tend towards an interface.
Neither Abstract classes nor Interface can be instantiated.


0 Respones to "Java Abstract class and Interface Part-2"

Send mail to your Friends.  

Expert Feed

Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar