Sullivan Market

a web based full-circle development system



My Java Tutorials - Abstract Classes


Here is some example code.... Copy and paste it to your page if you wish to use it.

abstract class Cat {

String abstractClassName = "Cat"; //abstract classes can have variables

String getAbstractClassName() //abstract classes can have methods
{
return abstractClassName;
}

abstract String getClassName(); //this must be implemented in any class
extending Cat

}

class Himalayan extends Cat{ //you can't extend from two classes at
once

String className = "Himalayan";

public Himalayan() {}

String getClassName() //must have this method, because Cat
declared it
{ //as an abstract
return className;
}

public static void main(String[] args) {

//Cat percy = new Cat(); //can't instantiate an abstract class

Himalayan cappuccino = new Himalayan();

System.out.println(cappuccino.getAbstractClassName());
//output is: Cat
System.out.println(cappuccino.getClassName());
//output is: Himalayan

}
}

Back To Java Tutorials

©2012 SullivanMarket.com