1Sometimes we may come across a situation where we cannot provide
2implementation to all the methods in a class. We want to leave the
3implementation to a class that extends it. In such case we declare a class
4as abstract.To make a class abstract we use key word abstract.
5Any class that contains one or more abstract methods is declared as abstract.
6If we don’t declare class as abstract which contains abstract methods we get
7compile time error.
8
9 1)Abstract classes cannot be instantiated
10 2)An abstarct classes contains abstract method, concrete methods or both.
11 3)Any class which extends abstarct class must override all methods of abstract
12 class
13 4)An abstarct class can contain either 0 or more abstract method.
1//abstract parent class
2abstract class Animal{
3 //abstract method
4 public abstract void sound();
5}
6//Dog class extends Animal class
7public class Dog extends Animal{
8
9 public void sound(){
10 System.out.println("Woof");
11 }
12 public static void main(String args[]){
13 Animal obj = new Dog();
14 obj.sound();
15 }
16}
1/**
2 * Simple Java program to prove that abstract class can have constructor in Java.
3 * @author http://java67.blogspot.com
4 */
5public class AbstractConstructorTest {
6
7 public static void main(String args[]) {
8 Server server = new Tomcat("Apache Tomcat");
9 server.start();
10 }
11}
12
13abstract class Server{
14 protected final String name;
15
16 public Server(String name){
17 this.name = name;
18 }
19
20 public abstract boolean start();
21}
22
23class Tomcat extends Server{
24
25 public Tomcat(String name){
26 super(name);
27 }
28
29 @Override
30 public boolean start() {
31 System.out.println( this.name + " started successfully");
32 return true;
33 }
34
35}
36
37Output:
38Apache Tomcat started successfully
39
40