Thursday, January 3, 2008

Abstract classes and interfaces

Abstract classes
Abstract classes can have both abstract methods and concrete methods.
The abstract class should be preceded by an "abstract " keyword.
The abstract methods should also be preceded by an "abstract " keyword.
The abstract class may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

 abstract class Test { 
int x;
int y;

abstract void method1();
abstract void method2();

void concretemethod1() {
System.out.println("I am a concrete method");
}
void concretemethod2() {
System.out.println("I am a concrete method");
}
}


Interfaces
Interfaces can have only abstract methods.
The data-fields inside interfaces are 'public static final' by default.
So, the constants are declared inside interfaces (coding convention).
A class can extend only parent class, but an interface can extend many interfaces.

 interface Test { 
int x = 0;
void method1();
void method2();
}

No comments: