Why Parent class is allowing to Child class to make Parent class's methods as abstract in Child class?

Viewed 104
class Parent 
{
    def m1()
    {
        System.out.println("m1 method");
    }
}

abstract class Child extends Parent
{
    def m1()
}

The above code compiles succesfully, and my question is:

Why does the Parent class allow the Child class to make the m1() method as an abstract method?

Where would we use this kind of scenario?

1 Answers

Now it can so happen that you want to create multiple variation of the parent class. Now Parent class being a concrete class it is very hard to achieve that. Because you can either try to make the parent as abstract and then provide implementation. But if the concrete class is used in several places of your big code base you have little choice but to go as follows.

Hence the strategy is to create abstract child it goes as follows

abstract class Child extends Parent
{
    def m1()
}

class SpecialChild extends Child {
    //.. some implementation of m1
}

Now we can still use the original child

child = new SpecialChild();

Hope this makes sense.

Related