. Java . Java Abstracts
Java Abstracts
using an abstract class
abstract class Cat {
//abstract classes can have variables
String abstractClassName = "Cat";
//abstract classes can have methods
String getAbstractClassName() {
return abstractClassName;
}
//this must be implemented in any class extending Cat
abstract String getClassName();
}
//you can not extend from two classes at once
class Himalayan extends Cat{
String className = "Himalayan";
public Himalayan() {}
//must have this method,
// because Cat declared it as an abstract
String getClassName() {
return className;
}
public static void main(String[] args) {
//you can not instantiate an abstract class
//Cat percy = new Cat(); //does not work
Himalayan cappuccino = new Himalayan();
System.out.println(cappuccino.getAbstractClassName());
//output is: Cat
System.out.println(cappuccino.getClassName());
//output is: Himalayan
}
}
| Sign In |
| to add the first comment for Java Abstracts. |