Abstract class declaration
Specifying abstract keyword before the class during declaration, makes it abstract. Have a look at below code:
Specifying abstract keyword before the class during declaration, makes it abstract. Have a look at below code:
// Declaration using abstract keyword abstract class AbstractDemo{ // Concrete method: body and braces public void myMethod(){ //Statements here } // Abstract method: without body and braces abstract public void anotherMethod(); }
Example of Abstract class and method
abstract class Demo1{
public void disp1(){
System.out.println("Concrete method of abstract class");
}
abstract public void disp2();
}
class Demo2 extends Demo1{
/* I have given the body to abstract method of Demo1 class
It is must if you don't declare abstract method of super class
compiler would throw an error*/
public void disp2()
{
System.out.println("I'm overriding abstract method");
}
public static void main(String args[]){
Demo2 obj = new Demo2();
obj.disp2();
}
}
Output:
I'm overriding abstract method
No comments:
Post a Comment