Inheritance [ class – abstract class (extends) ]

Inheritance [ class – abstract class (extends) ]

First let’s understand what is an abstract class is.

So, an abstract class is like a house that haven’t been fully developed yet, so the house can have some completed parts and also can have incomplete parts. As same as this the abstract class can have concrete methods as well as abstract methods which doesn’t have method body.


When creating an abstract method there are few things that we need to consider:

  1. If any class contains any abstract method, that class needs to be marked as abstract using?abstract keyword.
  2. abstract methods cannot contain static keyword. In other words, static & abstract?keyword cannot be in the same method.
  3. There are two ways that an abstract class can be extended by a child class:

?? ??? ??? ?a.? ?extend to a concrete class:

  • when extend happens between concrete class and an abstract class the abstract methods available within the abstract class needs to be override. It is a must. This is due to that concrete class cannot keep methods without a method body.

??? ??? ??? ?b.?????extend to an abstract class:

  • when extend one abstract class to another, since both classes are abstract there is no need to override the existing abstract methods.



Correct way to extend:

?? ??? 1.?concrete_class extends abstract_class

As a example lets get the situation where there is a abstract class called Test and a concreate class called App. Concrete class is the child class and also contains the main method. In the parent abstract class there are two methods, first one is a concrete method called concreteMethod and an abstract method called abstractMethod

After executing this code we get the output as:

? ?????2.?abstract_class extends abstract_class

As a example lets get the situation where there is one abstract class called Test and a another abstract class is called App. In the child abstract class either we can override the parent's abstract method or keep it as it is.

overriding in child class

not-overriding in child class

Errors

Now let’s identify some errors that can be occurs as a result of programming errors:

?? ??1. ?When use both abstract and static keywords:


? ? ?2.? When abstract methods not overridden in concrete child class:


Visit Blog


要查看或添加评论,请登录

Pasindu Jayasundara的更多文章

  • Explore the Future of Cloud with Microsoft Azure!

    Explore the Future of Cloud with Microsoft Azure!

    Imagine a platform where your applications scale effortlessly, your data is secure, and AI-driven tools help you make…

  • OOP

    OOP

    Object Oriented Programming is not a completely new concept since it has been around since like 1960s. the earliest…

社区洞察

其他会员也浏览了