Search

Wednesday, March 23, 2011

Rules to remember : Abstract class - Java


package abstractclass;


/**
 * Rule to remember : 
 * 1. Abstract call can never be instantiate either it has defination 
 * or not have defination(ERROR4 and ERROR5). 
 * 2. Compiler treat abstract keyword specially not to instantiate and not to defined(ERROR1 and ERROR4).
 * 3. Only and Only Method can be declare abstract into abstract class - ERROR 2
 * 4. An Abstract class is possible without any abstract method in java but there is not sense then .
 * 5. Abstract methods only possible to have public and protected modifier - ERROR 3 Private is not possible 
 6. Abstract class can have static members since static loads with class loading (no need to create instance) we can directly call static members with class name  ..)
 * 
 * @author Kapil
 *
 */
abstract class abs1 {
//protected abstract void show(){} //  ERROR 1 - abstract method can not specify body
protected String show(String name) {  //CASE 1 - a class can be declare abstarct without abstract member(method)
return name;
}

//public abstract int num;        // ERROR 2- not possible , only mehotd can declare abstract
//protected abstract void show(); // CORRECT
//private abstract void show(); // ERROR 3- Not possible because no sense to keep abstract method private.it should be public or protected
}

public class abstractDemo1 {
public static void main (String args[]) {
//abs1 a = new abs1(); // ERROR 4 - can not be instantiates - compiler error
//abs1.class(); // ERROR 5 - not possible - compiler error
}
}

No comments:

Post a Comment