Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is using abstract class better for inheritance than using normal class? [closed]

Consider this sample code where I have used normal class to implement generalized concept first and then using subclasses to specialize:

package check;
class figure{
  void area(){
  System.out.println("\n Superclass for any figure"); //An useless print statement
  }
}

class triangle extends figure{
  void area()
  {
    System.out.println("\n Code to determine area of a triangle");
  }
}

For the same implementation using abstract class, the code would be:

abstract class figure1{
  abstract void area();
}

class triangle1 extends figure1{
  void area()
  {
    System.out.println("\n Code to determine area of a triangle");
  }
}

Now reading my textbook in JAVA (Herbert Schildt, Complete Reference, 7th edition) it appeared to me that the writer wants to convey that somehow using abstract class is better in case of generalization-specialization approaches. However, I failed to understand how it is better than using normal class. Using abstract class essentially enforces us to extend the base class and that's all; other than that I am unable to see any huge difference in the implementation. So can anybody make me understand how using abstract class is a better practice/approach than using normal class?

like image 597
Mistu4u Avatar asked Nov 08 '13 17:11

Mistu4u


People also ask

Why should we use abstract class instead of normal class?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

Why abstract classes are used in inheritance?

If a class is declared abstract, it cannot be instantiated. To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it. If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.

Can abstract classes be used in inheritance?

An abstract class cannot be inherited by structures. It can contain constructors or destructors. It can implement functions with non-Abstract methods. It cannot support multiple inheritances.

What is the difference between a regular method inherited from a parent class and an abstract method inherited from an abstract class?

The main difference between abstraction and inheritance is that abstraction allows hiding the internal details and displaying only the functionality to the users, while inheritance allows using properties and methods of an already existing class.


2 Answers

An abstract class can never be instantiated and this is the real advantage of the abstract class.

For example, the class Animal could be an abstract class; but in real life you can never have an instance of an Animal; that makes no sense at all as you can't just create an Animal, you create an Animal, such as a Cat or Dog in real life as they are real-world entities. So in terms of the translation between the code design and the real-world concept or situation, abstract classes are much better as they can provide a much neater portrayal of the real-world situation.

like image 129
blackpanther Avatar answered Oct 21 '22 17:10

blackpanther


There is no "better" nor "worse", it depends on your purpose.

An abstract class allows declaring abstract methods that need to be implemented by the child class if the child class is not abstract itself.

A non-abstract does not, you need to implement all your declared methods.

Think of an abstract class as a "generic" object, that wouldn't exist as such in real life but provides a good generalization for a number of other concrete objects.

Think of the non-abstract class as something you could actually point your finger at in real life.

like image 34
Mena Avatar answered Oct 21 '22 15:10

Mena