Results 1 to 16 of 16
- 12-06-2010, 05:46 PM #1
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Still trying to grasp Polymorphism and Inheritance
Let's say I have the following abstract class
And some classes that extends my abstract class:Java Code:abstract class Family { private String name; public void someMethod() { //...Some code here to do stuff } }
Is it correct to have the Family class hold the String name since all subclasses of Family would have a String name variable? And if so, what is the best way to access that variable?Java Code:public class Dad extends Family { public Dad() { } public void methodA () { //...code here to do stuff } }
What I've tried that works:
Setting the variable String name as private and creating a protected method that the subclasses can use to access/set/get the variable.
Java Code:abstract class Family { private String name; public void someMethod() { //...Some code here to do stuff } protected void setName(String string) { name = string; } } public class Dad extends Family { public Dad() { } public Dad(String string) { setName(string); } public void methodA () { //...code here to do stuff } }
Setting the String name as protected and directly accessing the variable within the subclass.
Java Code:abstract class Family { protected String name; public void someMethod() { //...Some code here to do stuff } } public class Dad extends Family { public Dad() { } public Dad(String string) { name = string; } public void methodA () { //...code here to do stuff } }
Which way is better... or is there an even better way?? Are there some guidelines to follow when designing abstract and subclasses that would be helpful??
Previous Post:
setting attributes(variables) in an Abstract class:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-06-2010, 06:54 PM #2
There isn't a better way. It's personal preference. Personally I'd do the second one as I believe in data manipulation over getters and setters. A lot of people will tell you that getters and setters are the best way to do things.
Here's the best guideline I can give you: Make your code understandable, easy to read, work properly, and allow for easy manipulation. This means that you (and anyone you're working with) can read and modify your code easily without having to struggle to understand what it does.
-
As a side note, I'm finding the thrust of most articles I have read lately being towards marginalizing inheritance and polymorphism in favor of composition. There's a place for both, but many say you should do more of the latter and less of the former.
- 12-06-2010, 07:18 PM #4
- 12-06-2010, 07:29 PM #5
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Thank you both for your responses!
Will do! (or at least try my hardest too!)
Would one of my examples be better than the other in favor of composition? Or could you give me a better code example than mine of what might be in favor of composition over inheritance and polymorphism?:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-06-2010, 07:36 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Inheritance was/is heavily overrated; in the '80s when OOP went mainstream people liked the 'miracle' of inheritance so much that they forgot that it heavily breaks encapsulation, something we all love so much. Later, when the dust settled and we all found some 'idioms' we saw that composition could also do the job while preserving encapsulation. IMHO OOP isn't the one size fits all yet, I'm waiting for another miracle to be invented ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-06-2010, 07:40 PM #7
-
- 12-06-2010, 07:44 PM #9
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Here's a good article on the issue:
Composition vs. Inheritance
-Gary-
- 12-06-2010, 07:50 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-06-2010, 07:51 PM #11
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
OK. So are both you(Jos) and Furbarable saying that you prefer the protected getter and setter methods over making the instance variables protected and reachable with the dot operator??
I just re-read a section on Encapsulation and it talked about using the setter/getter methods to protect the instance variables such that they could not be changed to a value that was inappropriate for what-ever their usage might be.
Is that what you both are referring to when you say composition over inheritance??:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-06-2010, 07:53 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
-
I can't speak for Jos, but I'm not saying that, and I don't see him or I even discussing it here, but rather we've been discussing the initial over-use of inheritance and the subsequent backlash, a related but somewhat hijack discussion -- sorry. Myself, I've used both getter/setter and protected at different times when I've felt one was better suited than the other, but having said that, I don't use inheritance all that much any more (hence our other discussion).
setter and getter doesn't offer much protection but does allow you to filter mutation events and monitor accessor (setting) events.I just re-read a section on Encapsulation and it talked about using the setter/getter methods to protect the instance variables such that they could not be changed to a value that was inappropriate for what-ever their usage might be.
No. Best for you to read the article that is linked in Gary's post above.Is that what you both are referring to when you say composition over inheritance??
- 12-06-2010, 08:18 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
If I were holier than the pope I'd say, yes, use getters and setters even in subclasses because breaking encapsulation is ABT (A Bad Thing (tm)). If the evil world breaks encapsulation we're all screaming on the top of our lungs how bad such a thing is. But I'm not holier than the pope, I'm just an old hacker so my code is sprinkled with protected member variables that I use directly all over the place in subclasses. Until I break my own encapsulation (I simply add another restriction in the superclass) and then I become holier than any pope whatsoever whereever temporarily; but that disappears again ... I don't have an absolute opinion about this matter. I think C# did a nice job here, but I don't like C# much for a lot of other reasons.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-06-2010, 08:21 PM #15
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I would advise using private instance variables and calling getters and setters. I would say by the time you have good reason to do otherwise, you'll know better than to take advice from me anyway.
-Gary-
- 12-06-2010, 08:35 PM #16
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Similar Threads
-
Polymorphism
By blug in forum New To JavaReplies: 3Last Post: 10-11-2010, 10:35 AM -
Can't seem to grasp this
By Boomer1 in forum New To JavaReplies: 6Last Post: 11-10-2009, 08:47 AM -
inheritance and polymorphism
By tester in forum EclipseReplies: 1Last Post: 12-21-2008, 04:58 AM -
Relation between Polymorphism and Inheritance
By janakiram.attuluri in forum Advanced JavaReplies: 1Last Post: 12-26-2007, 11:32 PM -
what's polymorphism?
By christina in forum New To JavaReplies: 2Last Post: 08-05-2007, 10:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks