File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Overriding getter in subclass is a good practice? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Overriding getter in subclass is a good practice?" Watch "Overriding getter in subclass is a good practice?" New topic
Author

Overriding getter in subclass is a good practice?

Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
Hi,

Is overriding a getter a good practice or not? following are example classes

There are total 4 classes, Demo class has the main, In Figure.getArea() method is the original line the correct way? or should i use the commented line instead of that?
The original line will give result as 0 for triangle's area, whereas the commented line when uncommented and used will give triangle's result as 121.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Please UseCodeTags next time. I've added them for you this time.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
It looks to me that you might do better to have the getArea() and similar methods declared as abstract in the Figure class. Or even better, to have Figure as an interface.
Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
I agree that the method should be declared abstract, but what if i need some default behavior in Figure class?
My question of which is best practice or acceptable practice is not answered. please reply
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

One "best practice" is to include code which you need. (This might sound obvious but apparently it isn't.) So if you needed the Figure.getArea() method to return some default, or nominal, value, then it should do that. Presumably this design feature would come from some requirement for the project.

Or as suggested earlier, you could make the Figure.getArea() an abstract method. This would require subclasses of Figure to implement something for the getArea() method and then it wouldn't be able to return a default value. So if that's a requirement for the project, then the best practice would be to do that.

So the question for you is: does the project require that all subclasses of Figure should be able to compute their own areas? Or does it require that some subclasses don't have to compute their own areas, and delegate that responsibility to the Figure class? Whichever it is, the best practice would be to design your classes to support that requirement.
Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
My doubt is whether its a good practice or not to override getMSize() in Triangle class and then make it return a different object than in the parent class Figure.
please note that the getMSize() of Triangle class overrides that of Figure class but instead of returning mSize object it returns mDuplicateSize object.

So is this a standard acceptable practice or should it be avoided?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Why are prefixing your identifiers with an m?

If you return a different Size object from Triangle, it would have to be a subclass of what you return in Shape, otherwise your Triangle object no longer "IS-A" Shape object.
Sharan Ashok Vasandani
Greenhorn

Joined: Jul 15, 2010
Posts: 6
prefixing m is just a convention for member variables, please ignore it.
Am not quite clear with rest of your comment.

Am still returning Size object called mDuplicateSize from Triangle, my doubt is should getMSize() always return mSize object as a good practice?
or is it fine to use a getter of mSize object to return a different object from the subclass Triangle.

based upon answer to above question i will be able to decide which of the following lines should be used in getArea() of Figure class.
area=mSize.getLength()*mSize.getWidth();
OR
area=getMSize().getLength()*getMSize().getWidth();
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Sharan Ashok Vasandani wrote:prefixing m is just a convention for member variables, please ignore it. . . .
A convention you ought to drop very quickly.

I didn't say anything about the identity of what you return from the getSize method, but its type.
Runa Choudhary
Greenhorn

Joined: Jul 16, 2010
Posts: 1
@sharan : I think its not about overrding the getter method is good practice or not .If you see you are deceiving the purpose of accessor method for a varaible.getters are method that are used for asscessing a private /constricted variable from outside the class (Encapsulation principle) why do you want to have an accessor method of some variable return some other variable.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Sharan Ashok Vasandani wrote:My doubt is whether its a good practice or not to override getMSize() in Triangle class and then make it return a different object than in the parent class Figure.
please note that the getMSize() of Triangle class overrides that of Figure class but instead of returning mSize object it returns mDuplicateSize object.

So is this a standard acceptable practice or should it be avoided?

If that's what your requirements say, then it's an acceptable practice. If they say something else, then it's not acceptable. I don't know what you are trying to get at with this question. Overriding methods is perfectly normal. Having an overridden method do something different than what the method it overrides is perfectly normal. That's what overriding is for.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32675
    
    4
Welcome to the Ranch Runa Choudhary
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

Sharan Ashok Vasandani wrote:prefixing m is just a convention for member variables, please ignore it.


Please, do not use hungarian notation in Java. It made sense in C back in the early 90s.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Overriding getter in subclass is a good practice?
 
Similar Threads
whats wrong with my code ?
potential gotcha in Inheritance you need to know
Are my classes shaped correctly?
User input into array
Regarding Polymorphism