-2

Super class being an abstract class creates an overhead for all its sub classes to compulsorily define its abstract methods. I understand that it's very basic but I need to know why do programmers usually make super class as an abstract class, though we can do similar things using a super class as a non abstract class.

zeeali
  • 1,524
  • 20
  • 31

5 Answers5

2

An abstract superclass is one way to provide re-usable code.

  • You can extend the abstract class and inherit the code. This is sometimes more convenient than using static methods or object composition to share code.

  • The abstract class can "fix" parts of the code (by making it final). This is called the "template method" pattern (and this is not possible with an interface, which cannot provide final methods).

Of course, you can achieve both with a non-abstract superclass as well.

An abstract class has the additional benefit that it does not have to provide a complete implementation (that would make sense to instantiate on its own), some parts can be left specified, but unimplemented (the abstract methods).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

imagine you have a common behaviour where only small details are specific to the implementation - then you can put all the common behaviour in a abstract base class and having some abstract methods that the implementing classes need to fill.

For example a abstract repository base class might implement all the details to contact your server, etc. and concrete repositories just need to fill in the details to read the right object from the right table, etc.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
0

Abstarct classes are meant for 'abstracting'. means if some classes are having common behaviour, instead of writing evry time the same thing in each class, write that in one class and ask the other classes to use it [by making the classes as subclasses to the abstract class]. This is nothing but inheritance. To summarise: Use abstract classes when you want default behaviour for some classes Use interfaces when you want different behaviour different classes.

For More explanations Refer below links:

http://www.javacoffeebreak.com/faq/faq0084.html

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
K.Raj.
  • 643
  • 6
  • 22
  • can you please give any real world example of both abstract class and interface? – zeeali Jul 28 '14 at 06:46
  • abstract class and interface both are different. I think you know about it. Abstract class can have normal methods while interface only have abstract methods. – K.Raj. Jul 28 '14 at 09:11
  • abstract class methods are used for data abstraction in which you can only what will be done by this method but you can not know what is the logic behind that method. if you have abstract method private abstract ab() then another developer can call ab() as per his/her requirement but that developer can not see the real logic behind that method – K.Raj. Jul 28 '14 at 09:11
  • i just want a real world example for both to understand well. – zeeali Jul 28 '14 at 10:37
0

several usage of abstract class:

  1. act as the protocol when tranfering data between objects, the two customers need not to know other class's structure. ----- just like the interface

  2. define the abstract operation, which hides the detailed implementation of concrete class, for example, I have a class called AbstractPayment, which define the opration of charging money from customer, then the concrete classese of it could be: PaypalPayment, AlipayPayment, BankPayment and others. BUT, for the class customer, it only needs to know the AbstractPayment. after some time, if you need to add another ConcretePayment, or modify one other payment, the customer class won't change.

Chy. King
  • 39
  • 1
  • 5
-1

Abstraction is largely used in design patterns, I suggest you to read following:

Abstract Factory Pattern

STO

Community
  • 1
  • 1
Usman Waheed
  • 555
  • 5
  • 14