Abstract class in Java

后端 未结 14 1297
孤独总比滥情好
孤独总比滥情好 2020-11-22 12:32

What is an \"abstract class\" in Java?

14条回答
  •  死守一世寂寞
    2020-11-22 13:13

    An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.

    In other words, a class that is declared with abstract keyword, is known as abstract class in java. It can have abstract(method without body) and non-abstract methods (method with body).

    Important Note:- Abstract classes cannot be used to instantiate objects, they can be used to create object references, because Java's approach to run-time Polymorphism is implemented through the use of superclass references. Thus, it must be possible to create a reference to an abstract class so that it can be used to point to a subclass object. You will see this feature in the below example

    abstract class Bike{  
      abstract void run();  
    }  
    
    class Honda4 extends Bike{  
        void run(){
            System.out.println("running safely..");
        }  
    
        public static void main(String args[]){  
           Bike obj = new Honda4();  
           obj.run();  
        }  
    } 
    

提交回复
热议问题