This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the pupose of abstract class extending to concrete class  RSS feed

 
sam liya
Ranch Hand
Posts: 1271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw some places that abstract classes extend to the concrete classes.what is the purpose of this architecture?I think it should be other way?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the example that you're talking about?
 
Tim Holloway
Bartender
Posts: 18705
71
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An abstraction provides a template. It can prototype methods without actually supplying their logic. It's a lot like how Interfaces work, except that unlike an Interface, an abstract class definition provides part of - but not all - of what is necessary to be a complete (concrete) class. Such as, for example, member and static properties.

You cannot instantiate an abstract class because it isn't complete. But you can use it as a parent class for a class that is complete, and that class will be a concrete class.
 
Paul Clapham
Sheriff
Posts: 22476
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that abstract classes exist for the purpose of being extended by concrete classes. And hopefully you understand the architectural idea behind that.

But that says nothing about where the abstract class should come from in the first place. And since it's extremely common for an abstract class to extend Object -- which is clearly a concrete class -- I don't see what your question is for. Could you explain a bit more clearly why you don't think an abstract class should extend Object? And also what you think it should extend instead?

At least, I think that was your question. The phrase "extend to" doesn't mean anything so I interpreted it as "Why does an abstract class sometimes extend a concrete class?" Sorry if I guessed wrong about that.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sam liyanage wrote:I saw some places that abstract classes extend to the concrete classes.what is the purpose of this architecture?I think it should be other way?

Can you explain why you think that?

However, a quick example of extending an abstract class:and there you have it - a class that converts an array to a fixed-length, read-only List in 19 lines. And given all the things that Lists can do, that's a pretty powerful use of an abstract class.

Winston
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible the original poster meant to ask why sometimes we have an abstract class extending from a concrete class? Rather than extending to a concrete class? Because both are possible, but only the former case is non-obvious, I think.

At one level, all classes extend a concrete class - Object. So there's nothing unusual in that. It would be unusual to have another concrete class, and then have an abstract class extending that. Which would mean that in order to ever have an instance, you'd have to extend the class further to another concrete class. E.g.:

It's uncommon, but if you had a concrete class that did something useful (Foo in this example), and you later came up with additional functionality you'd like to add, with multiple possible implementations, then you could add Bar and Baz later. Probably it would make more sense to add the newMethod() to Foo, and make that abstract - but maybe it's too late to do that, because Foo is already being used by people. So you do this more complicated route instead. It's not common, but it's possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!