What is an Abstract class in java | Abstract class in java example

What is an Abstract class in java | Abstract class in java example

What is an Abstract class in java

What is an Abstract class in java ?

(B) What is an Abstract class?

Abstract class defines an abstract concept which can not be instantiated and comparing to interface it
can have some implementation while interfaces can not. Below are some points for abstract class; V We can not create object of abstract class it can only be inherited in a below class V Normally abstract classes have base implementation and then child classes derive from the

Abstract class to make the class concrete.

Ok let's try to get the abstract class fundamentals just to make sure that you do not stumble in interview: Abstract classes normally represent concept. Just to make it precise it represents half implemented concepts. What does that mean fundamentally?. You can have concept like food but you can not have an instance of the same. You can have instance of cheese, milk etc. 

So food represents an abstract concept while cheese, milk ete represent the actual instance. In your project can have a general concept called as "Transaction" but then further your project can make the "Transaction" class more concrete by saying "Bank Transaction" or "Ledger Transaction" class. 

So you would like to model the "Transaction" class but not create objects of the same. It makes more sense to create "Bank Transaction" class object or "Ledger Transaction" class objects.

In order to understand abstract class fundamentals lets do a sample project. You can get the sample project in CD in "Sourcecode\Abstract" folder. Below is the class diagram of the project. "clsAbstract Person" class represents a person. It has three methods "Numberoflegs", "NumberofEyes" and "Sex".

 Now conceptually all humans have two legs and two eyes, but sex is something which can vary from person to person. So what we do is we write implementation for "numberofEyes" method and "numberOflegs" method and leave the "Sex" method implementation to subclasses.

Nothing special has been done in the "Sex" method implementation other than just displaying the whether its "Female" or "Male" implementation according to the class. You can now visualize the concept of using "Abstract dass In this situation "Person" is a concept but it can not have instance as there is no point in creating an object of "Person".

 It's a half made class which does not have "Sex" implementation. Conceptually "Penson" can be either female or male. But later when subclasses inherit the "ChAbstractPerson" class they provide a true implementation by providing functionality for the "Sex" method. It sounds sense for creating objects for "cliFemale" or "chaMale" class

"ChiRun" class is the driver which creates objects for both classes "csMale" and "cliFemale" and calls all the three methods. You can see the output "Eyes" and "Leg" implementation remain same for both the classes but "Sex" implementation varies. So basically abstract class "clsAbstractPerson provides conceptual implementation but later the concrete classes give more detail implementation and complete meaning to the class.

Abstract class in java example | What is an Abstract class in java

Post a Comment

Thanks for Comment

Previous Post Next Post