What is the purpose of abstract classes in Java? [duplicate] - java

This question already has answers here:
Understanding the purpose of Abstract Classes in Java
(8 answers)
Closed 7 years ago.
I have some questions around abstract classes and their use. I know the basics about them; for example, that they can't be instantiated, they can have concrete and abstract methods, ... But I guess what I wanna know is, what purpose do they serve in life (in software engineering)?
What is the purpose of abstract classes in Java?
Why and when should one use an Abstract class? If you can use a normal class and then inherit it, why would you inherit an abstract class?
How would using abstract classes make our life easier? would it provide better maintainability? more flexibility? ...
btw, I've already looked at some similar questions and answers, including Understanding the purpose of Abstract Classes in Java, but they don't answer my question. I'm more looking for answers that shed light on the philosophy behind the introduction of abstract classes in the first place in Java.
Thanks

An abstract class can be used as a type of template for other classes, and most commonly used for inheritance.
The best example is from the book head first java:
abstract class Animal(){}
then you can extend child class such as: dog, cat, fish.

Related

how is abstract class different from an interface in terms of abstraction concept [duplicate]

This question already has answers here:
Interface vs Abstract Class (general OO)
(36 answers)
Closed 4 years ago.
I have read number of articles about differences between abstract classes and interface. can someone please specify the conceptual difference between the two, i am summarizing my understanding so far as:
When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is.
When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object can do
Please refrain from using Car, Animal examples for differentiating between the two terms.
I am more interested to understand conceptually how abstract class and interface fits into the concept of abstraction. If someone could additionally talk about abstraction concept in relation to abstract class or interface
As far as "abstraction" is concerned, interfaces and abstract classes are very similar.
In Java, "abstraction" is made concrete (couldn't help using this oxymoron) by specified, but unimplemented behavior. In this regard, interfaces and abstract classes are the same. And the biggest sign is that no objects can be made directly from either.
What makes interfaces different from abstract classes has little to do with the concept of abstraction. For example, the fact that abstract classes can have state and constructors gives them some "concrete" nature, although this doesn't change much of their difference from interfaces, as far as "specified/unimplemented" behavior is concerned.

Can someone help explain abstract classes and interfaces to me (novice)? [duplicate]

This question already has answers here:
What is the difference between an interface and abstract class?
(38 answers)
Closed 7 years ago.
So I'm currently reading my java book and it doesn't seem to be to clear on the abstract classes and interfaces. Here is my understanding: Abstract classes are created to basically be the most generic form of a superclass, one of which no instance can be created, and an interface contains methods to be implemented by subclasses? Any help on what I'm missing to these aspects of coding. It would be greatly appreciated, thanks!
(I'm not asking what is different between the two, I just want an understanding of what each is)
Well, all that interfaces do are state required implementation. A contract, if you will, that the inheriting class will implement their own versions of those methods with the same parameters and return values.
Abstract classes are similar, except they can implement generic implementation without requiring the inheriting classes to implement it.
Another difference is, that one class can implement multiple interfaces but only inherit from one possibly abstract class.
That's my understanding of it anyways. Hope I helped!

Why to use interface while we can make all methods abstract? [duplicate]

This question already has answers here:
Abstract class vs Interface in Java
(15 answers)
Closed 9 years ago.
In abstract class we can make all methods abstract so that it can work like an interface, so why to use interface at all?? One of the reason I could come up was that we can implement multiple interface not extend multiple class.. Is there any design or performance related thing involved??
You already got the answer. Using Interfaces we can enforce multiple types of behaviours where as using classes will not work for you. For example, you can enforce a class to be IComparable as well as INumerable however it is not possible if you want to do it with classes.
You've already identified the one thing that interfaces allow that abstract classes don't allow. One class can't extend multiple abstract classes.
Is there any design or performance related thing involved??
There is no performance difference.
You could argue that the single inheritance restriction of abstract classes (in fact, all classes) makes this "a design thing" though. Certainly it would seriously restrict your use of polymorphism in an OO design.
(You could also argue that you can't follow the maxim of "programming to the interface" when you don't have interfaces. However, that is a weak argument ... a terminological quibble.)
Design wise it is preferred guidelines to use Interface for you code behavior/contract/functionality definition (see List interface) and use Abstract class where you have atlease some reusable (via inheritance) method implementation .
Though having all abstract method is possible, but in such cases an Interface is preferred.

Abstract Class V/s Interface in Design Java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to use abstract class or interface?
I am a newbie in Java , can anyone please explain a scenario where abstract class will be
useful and interface will not be and vice versa.
I believe in not so complex problems both can solve the issue with equal ease.
Please explain in layman's term and pardon my ignorance!
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Interface and an Abstract class? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interface vs Abstract Class (general OO)
I want to know the difference between an Interface and an Abstract class ?
I'm so confused about this.
Thanks already.
In an interface you simply define the methods that you are going to implement. In an abstract class you can actually write methods that contain some code. I'm sure this question has been asked a thousand times so look at some of the other posts.
Interfaces define contracts. Abstract classes provide for code reuse. An object interacts with other objects via their contracts (Interfaces). An object shares code with other, related, objects, by inheriting it from an abstract class.

Categories

Resources