Abstract Methods:
Abstract Classes:
/**
* ABSTRACT CLASS
* /
public abstract class AbstractTest {
public abstract void testPrint1();
protected abstract void testPrint2();
}
/**
* ABSTRACT CLASS IMPLEMENTED
* NOTE: IMPL CLASS MUST IMPLEMENT ALL ABSTRACT METHODS IN ABSTRACT CLASS
*/
public class AbstractTestImpl extends AbstractTest {
public void testPrint1() {
System.out.println("Test Print One!");
}
public void testPrint2() {
System.out.println("Test Print Two!");
}
public static void main(String args[]) {
AbstractTestImpl obj = new AbstractTestImpl();
obj.testPrint1();
}
}
- They are present in abstract classes
- The keyword "abstract" is prepended to the methods
- Abstract methods are implemented only in sub-class / inherited class and not in "Abstract" class
- Since abstract methods are implemented in sub-class, the return type of abtract methods is either "protected" or "public"
Abstract Classes:
- A class that contains "abstract" methods is an abstract class
- Abstract classed can contain "non static" and "non final" member variables
- They can NEVER be instantiated. They can be ONLY INHERITED
- One abstract class can inherit another abstract class
- Abstract classes can contain ordinary methods as well as abstract methods
/**
* ABSTRACT CLASS
* /
public abstract class AbstractTest {
public abstract void testPrint1();
protected abstract void testPrint2();
}
/**
* ABSTRACT CLASS IMPLEMENTED
* NOTE: IMPL CLASS MUST IMPLEMENT ALL ABSTRACT METHODS IN ABSTRACT CLASS
*/
public class AbstractTestImpl extends AbstractTest {
public void testPrint1() {
System.out.println("Test Print One!");
}
public void testPrint2() {
System.out.println("Test Print Two!");
}
public static void main(String args[]) {
AbstractTestImpl obj = new AbstractTestImpl();
obj.testPrint1();
}
}