Why are there abstract classes in the first place?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I know about the conceptual use behind creating an abstract class, i.e. defining a common interface for its subclasses where some of the implementation is left to the individual subclasses.
Am I correct in my assumption that there is technically no necessary need for abstract classes, since you can overwrite a superclass method anyway? Were abstract classes just created to make the intention of the classes clearer to the developer?
Example of what I mean:
// Using an abstract class
abstract class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract String getColor();
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
// Without an abstract class
class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
String getColor()
{
return "defaultColor";
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
java class oop object abstract-class
|
show 1 more comment
I know about the conceptual use behind creating an abstract class, i.e. defining a common interface for its subclasses where some of the implementation is left to the individual subclasses.
Am I correct in my assumption that there is technically no necessary need for abstract classes, since you can overwrite a superclass method anyway? Were abstract classes just created to make the intention of the classes clearer to the developer?
Example of what I mean:
// Using an abstract class
abstract class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract String getColor();
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
// Without an abstract class
class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
String getColor()
{
return "defaultColor";
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
java class oop object abstract-class
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
2
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45
|
show 1 more comment
I know about the conceptual use behind creating an abstract class, i.e. defining a common interface for its subclasses where some of the implementation is left to the individual subclasses.
Am I correct in my assumption that there is technically no necessary need for abstract classes, since you can overwrite a superclass method anyway? Were abstract classes just created to make the intention of the classes clearer to the developer?
Example of what I mean:
// Using an abstract class
abstract class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract String getColor();
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
// Without an abstract class
class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
String getColor()
{
return "defaultColor";
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
java class oop object abstract-class
I know about the conceptual use behind creating an abstract class, i.e. defining a common interface for its subclasses where some of the implementation is left to the individual subclasses.
Am I correct in my assumption that there is technically no necessary need for abstract classes, since you can overwrite a superclass method anyway? Were abstract classes just created to make the intention of the classes clearer to the developer?
Example of what I mean:
// Using an abstract class
abstract class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract String getColor();
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
// Without an abstract class
class Car
{
int fuel;
int getFuel()
{
return this.fuel;
}
String getColor()
{
return "defaultColor";
}
class RedCar extends Car
{
String getColor()
{
return "red";
}
}
java class oop object abstract-class
java class oop object abstract-class
asked Jan 4 at 0:11
user3545063user3545063
116212
116212
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
2
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45
|
show 1 more comment
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
2
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
2
2
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45
|
show 1 more comment
2 Answers
2
active
oldest
votes
Were abstract classes just created to make the intention of the classes clearer to the developer?
That's right, but it also prevents developers from doing "silly" things.
For example, you cannot create instances of abstract classes. In the context of your code, it does not make sense to create a "general" Car
. You can only create a BlueCar
or a RedCar
or some other subclass. While instances of an anonymous subclass may seem like instances of an abstract classes, they are in the end constructed from subclasses. If you made the Car
class abstracted, however, developers will not accidentally create an instance of Car
, because the compiler would complain.
Abstract classes also forces developers to implement the abstract methods. With your non-abstract Car
class, I can inherit it and forget that I need to override getColor
. Now some other parts of the code might call getColor
and gets a nonsensical result "defaultcolor". What the heck is a default color? Making the method abstract forces the subclasses to think about implementing the required methods.
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
add a comment |
Most people I know avoid abstract classes. From an application programmers point of view, my opinion is that abstract classes should be avoided. Using interfaces are so much more flexible not forcing your work to adhere to a class hierarchy.
a bit formally: an abstract class is strict inheritance, while interfaces are composition.
An example where abstract classes are useful is when making class libraries. The hierarchy that abstract classes enforces, can make the library more easily understandable for application programmers. However, I do believe this comes at the cost of longer development times of the library.
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031545%2fwhy-are-there-abstract-classes-in-the-first-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Were abstract classes just created to make the intention of the classes clearer to the developer?
That's right, but it also prevents developers from doing "silly" things.
For example, you cannot create instances of abstract classes. In the context of your code, it does not make sense to create a "general" Car
. You can only create a BlueCar
or a RedCar
or some other subclass. While instances of an anonymous subclass may seem like instances of an abstract classes, they are in the end constructed from subclasses. If you made the Car
class abstracted, however, developers will not accidentally create an instance of Car
, because the compiler would complain.
Abstract classes also forces developers to implement the abstract methods. With your non-abstract Car
class, I can inherit it and forget that I need to override getColor
. Now some other parts of the code might call getColor
and gets a nonsensical result "defaultcolor". What the heck is a default color? Making the method abstract forces the subclasses to think about implementing the required methods.
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
add a comment |
Were abstract classes just created to make the intention of the classes clearer to the developer?
That's right, but it also prevents developers from doing "silly" things.
For example, you cannot create instances of abstract classes. In the context of your code, it does not make sense to create a "general" Car
. You can only create a BlueCar
or a RedCar
or some other subclass. While instances of an anonymous subclass may seem like instances of an abstract classes, they are in the end constructed from subclasses. If you made the Car
class abstracted, however, developers will not accidentally create an instance of Car
, because the compiler would complain.
Abstract classes also forces developers to implement the abstract methods. With your non-abstract Car
class, I can inherit it and forget that I need to override getColor
. Now some other parts of the code might call getColor
and gets a nonsensical result "defaultcolor". What the heck is a default color? Making the method abstract forces the subclasses to think about implementing the required methods.
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
add a comment |
Were abstract classes just created to make the intention of the classes clearer to the developer?
That's right, but it also prevents developers from doing "silly" things.
For example, you cannot create instances of abstract classes. In the context of your code, it does not make sense to create a "general" Car
. You can only create a BlueCar
or a RedCar
or some other subclass. While instances of an anonymous subclass may seem like instances of an abstract classes, they are in the end constructed from subclasses. If you made the Car
class abstracted, however, developers will not accidentally create an instance of Car
, because the compiler would complain.
Abstract classes also forces developers to implement the abstract methods. With your non-abstract Car
class, I can inherit it and forget that I need to override getColor
. Now some other parts of the code might call getColor
and gets a nonsensical result "defaultcolor". What the heck is a default color? Making the method abstract forces the subclasses to think about implementing the required methods.
Were abstract classes just created to make the intention of the classes clearer to the developer?
That's right, but it also prevents developers from doing "silly" things.
For example, you cannot create instances of abstract classes. In the context of your code, it does not make sense to create a "general" Car
. You can only create a BlueCar
or a RedCar
or some other subclass. While instances of an anonymous subclass may seem like instances of an abstract classes, they are in the end constructed from subclasses. If you made the Car
class abstracted, however, developers will not accidentally create an instance of Car
, because the compiler would complain.
Abstract classes also forces developers to implement the abstract methods. With your non-abstract Car
class, I can inherit it and forget that I need to override getColor
. Now some other parts of the code might call getColor
and gets a nonsensical result "defaultcolor". What the heck is a default color? Making the method abstract forces the subclasses to think about implementing the required methods.
edited Jan 4 at 1:23
Cardinal System
1,90421425
1,90421425
answered Jan 4 at 0:27
SweeperSweeper
72.5k1075144
72.5k1075144
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
add a comment |
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
I believe this is worth mentioning in your answer. It could serve some good to future viewers.
– Cardinal System
Jan 4 at 0:47
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
@CardinalSystem I am not quite sure how to insert that link in a place that feels natural. Feel free to suggest an edit.
– Sweeper
Jan 4 at 0:54
add a comment |
Most people I know avoid abstract classes. From an application programmers point of view, my opinion is that abstract classes should be avoided. Using interfaces are so much more flexible not forcing your work to adhere to a class hierarchy.
a bit formally: an abstract class is strict inheritance, while interfaces are composition.
An example where abstract classes are useful is when making class libraries. The hierarchy that abstract classes enforces, can make the library more easily understandable for application programmers. However, I do believe this comes at the cost of longer development times of the library.
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
add a comment |
Most people I know avoid abstract classes. From an application programmers point of view, my opinion is that abstract classes should be avoided. Using interfaces are so much more flexible not forcing your work to adhere to a class hierarchy.
a bit formally: an abstract class is strict inheritance, while interfaces are composition.
An example where abstract classes are useful is when making class libraries. The hierarchy that abstract classes enforces, can make the library more easily understandable for application programmers. However, I do believe this comes at the cost of longer development times of the library.
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
add a comment |
Most people I know avoid abstract classes. From an application programmers point of view, my opinion is that abstract classes should be avoided. Using interfaces are so much more flexible not forcing your work to adhere to a class hierarchy.
a bit formally: an abstract class is strict inheritance, while interfaces are composition.
An example where abstract classes are useful is when making class libraries. The hierarchy that abstract classes enforces, can make the library more easily understandable for application programmers. However, I do believe this comes at the cost of longer development times of the library.
Most people I know avoid abstract classes. From an application programmers point of view, my opinion is that abstract classes should be avoided. Using interfaces are so much more flexible not forcing your work to adhere to a class hierarchy.
a bit formally: an abstract class is strict inheritance, while interfaces are composition.
An example where abstract classes are useful is when making class libraries. The hierarchy that abstract classes enforces, can make the library more easily understandable for application programmers. However, I do believe this comes at the cost of longer development times of the library.
answered Jan 4 at 0:39
Stefan KarlssonStefan Karlsson
487211
487211
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
add a comment |
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
To be fair, abstract classes were a lot more popular in the days before languages supported interfaces. Even so, the use of inheritance to enhance or restrict or refine base classes is still a major reason to use abstract classes. In this regard, interfaces makes abstract classes more powerful as it allows even the changes to classes to be abstracted.
– Peter Camilleri
Jan 4 at 2:24
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
Almost no one uses abstract classes when writing applications. What often happens in the end, near delivery, is that there is argument around which interfaces should be morphed into abstract classes. If the final deliverable is something like a class library, then I admit that this work is worthwhile. However, if its an application that will likely change a lot in the future, it makes more sense to keep all the interfaces as is.
– Stefan Karlsson
Jan 4 at 9:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031545%2fwhy-are-there-abstract-classes-in-the-first-place%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Before default methods were added to interfaces, the abstract class was one way to allow classes to share common functionality (and code). If your abstract class consisted of only abstract methods, then an interface was always a better choice.
– Elliott Frisch
Jan 4 at 0:14
2
There is "technically" no reason to have anything except bytecode, so that argument goes right out the window. Abstract classes let you define the underlying common functionality of a set of objects, without the common aspects themselves constituting a sensible class that should be instantiable. For instance, a bank account is an abstract concept: it's tied to a person, it can be use for deposits and withdrawals, but "a bank account" is nothing, and allowing code to make "a bank account" rather than "a chequeing account" or "a savings account", etc. makes no sense. So, abstract it is.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Also any half-decent website or book that teaches Java will explain why abstract classes are useful.
– Mike 'Pomax' Kamermans
Jan 4 at 0:27
Abstract classes allows to implement algorithms with variable parts, where the variable parts are the abstract methods. An example would be template method design pattern, check Implementing the Template Method Pattern in Java for more details
– Valentin Carnu
Jan 4 at 0:34
It's like a class-interface hybrid. It must be extended if you want to construct an object from it (like an interface), it can have methods that are already defined (like a regular class), and it can have methods that must be defined by the subclass (like an interface).
– Cardinal System
Jan 4 at 0:45