Abstract Class Example
public abstract class myAbstract { myAbstract(){};// this constructor is optional public abstract void myMethod( ); //abstract method public void myPrint( ) //defined method { System.out.println("hello , I am abstract class"); } } public class myExtends extends myAbstract { myExtends(){}; //constructor needed public void myMethod( ) { System.out.println("extention of myAbstract myMethod"); } } public class testAbstract { public static void main (String args[]) { myExtends a = new myExtends(); //myAbstract c = new myAbstract();//illegal myAbstract c = a;// a reference to the abstract parent can refer to child c.myMethod(); // using the parent reference to refer to child c.myPrint(); System.out.println("main "); return; } }
Interface Example
public interface myInterface { public abstract void myMethod(); // myInterface(int a){}; //illegal line - cannot have constructors in an interface } public class implementInterface implements myInterface { public void myMethod() { System.out.println("hello world"); } } public class testInterface { public static void main (String args[]) { myInterface a; implementInterface b = new implementInterface(); //a.myMethod();//illegal - a is a reference not an instance a = b; a.myMethod();// same as b.myMethod(); System.out.println("main "); return; } }
© Nachum Danzig December 2003