Abstract Class:-Real Time Example

前端 未结 5 1883
遥遥无期
遥遥无期 2020-12-14 16:27

Recently in a interview I was asked a very general question \"what is abstract in java\".I gave the definition and it was followed with some other question on abstract as wh

5条回答
  •  没有蜡笔的小新
    2020-12-14 17:01

    Here, Something about abstract class...

    1. Abstract class is an incomplete class so we can't instantiate it.
    2. If methods are abstract, class must be abstract.
    3. In abstract class, we use abstract and concrete method both.
    4. It is illegal to define a class abstract and final both.

    Real time example--

    If you want to make a new car(WagonX) in which all the another car's properties are included like color,size, engine etc.and you want to add some another features like model,baseEngine in your car.Then simply you create a abstract class WagonX where you use all the predefined functionality as abstract and another functionalities are concrete, which is is defined by you.
    Another sub class which extend the abstract class WagonX,By default it also access the abstract methods which is instantiated in abstract class.SubClasses also access the concrete methods by creating the subclass's object.
    For reusability the code, the developers use abstract class mostly.

    abstract class WagonX
    {
       public abstract void model();
       public abstract void color();
       public static void baseEngine()
        {
         // your logic here
        }
       public static void size()
       {
       // logic here
       }
    }
    class Car extends WagonX
    {
    public void model()
    {
    // logic here
    }
    public void color()
    {
    // logic here
    }
    }
    

提交回复
热议问题