Inheritance [ class – abstract class (extends) ]
Pasindu Jayasundara
Full-Stack Software Engineer (UG) | BICT (UG) | Freelancer | Postman Student Expert | AIESECer | Gaveller
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:
?? ??? ??? ?a.? ?extend to a concrete class:
??? ??? ??? ?b.?????extend to an abstract class:
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: