abstract
- Specify the behavior of subclasses
- To prevent code duplication
- To group subclasses
- No instance (object)
- Abstract class != Polymorphism
- May have abstract or concrete methods/subclass
- An abstract class is a class that defined the common instances and methods from the actual classes
- An abstract class can not create an object. Because it doesn't have instance of abstract class
- An abstractclass and subclasses are inheritance
- An abstractclass cannot be instantiated (cannot create new instances of an abstract class)
- purpose : to function as a base for subclasses
How to use?
- I can simply add "abstract" to the abstract class
- The abstract method should not have a body
public class Main {
public static void main(String[] args) {
Man d = new Man();
d.print();
Woman e = new Woman();
e.print();
}
}
//simply add abstract
public abstract class Human {
//abstract method does not have a body!!
public abstract void print();
}
public class Woman extends Human{
//Need to override
public void print() {
System.out.println("Woman");
}
}
public class Man extends Human{
//override here!
public void print() {
System.out.println("Man");
}
}