aspose file tools*
The moose likes Beginning Java and the fly likes Need help on inheritance... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help on inheritance..." Watch "Need help on inheritance..." New topic
Author

Need help on inheritance...

Wee Keong Soh
Greenhorn

Joined: Apr 08, 2009
Posts: 18
Hi there,

class Vehicle {

private double price;

public double getPrice() {
return price;
}


public void setPrice(double price) {
this.price = price;
}

=============================================
class Car extends Vehicle
{

}
=============================================
public class Main
{
public static void main (String [] args)
{
Car Toyota = new Car();
Toyota.setPrice(20000.00);
System.out.println("The price of Toyota is " + Toyota.getPrice());
}
}

==============================================

The above works but i have 1 question. Car inherits from Vehicle only the 2 public methods but not the private double price. So can anyone explain to me what does this still work?

Do pardon me for my ignorance and thanks for the help in advance!




amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
Hi,
Variable "price" is private and is not visible in Class Car that extends Vehicle.
It works because you are calling the getPrice() method which is having "public" access specifier and hence visible for class Car.
Hope this helps.


Regards,
Amit
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I think I see what you are thinking. Since you have an instance of Car (Toyota) and are calling the setPrice and getPrice methods from that instance they must be referring to the price in Car (which it doesn't have).

The way I understand it since you have not overridden the methods, you are essentially calling the methods in the superclass from the instance of the subclass. Since you are calling the methods in the super class you have access to that variable. However if you were to override the methods it would fail, unless you specifically called the superclass method.

I hope that makes sense (and is correct.....I think it is).


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Wee Keong Soh
Greenhorn

Joined: Apr 08, 2009
Posts: 18
Hi there,

Thanks, you are very much spot on! I tried to override the methods in class Car and it throws out an error which is resolved by declaring the instance variable price in class Car.

Thanks all the help!
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Please use code tags to write code


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Inheritance is a concept by which a child inherits a parent's Qualities.

It's a bit counter-intuitive but the child is more powerful than the parent (as it has its own qualities and its parent's too)

here is your code with a bit of modifications to make you understand....


Output:
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Hi friends,
As per the above discussion the private variable is not accesble to subclass but in setter method of superclass you are written the code

this method has been visible to subclass as it is public but my question is what does it do for the above line in subclass this.price means where it is going to assign


Creativity is nothing but Breaking Rules
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

The super class (Vehicle).


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
hi Wee Keong Soh,

when you decompile the car class code you will get the java file as shown below....

this means the car reference has as license to excess the methods in super class which are public.and even though the private variable is not accessible with the use of public methods we can assign the value to the super class private variable. so when you pass 20000.00 to the setter method it will pass that value the superclass setmethod() and there it is assigning the value to the price........ i think it will clear your doubt.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

santhosh.R gowda wrote:Hi friends,
As per the above discussion the private variable is not accesble to subclass but in setter method of superclass you are written the code

this method has been visible to subclass as it is public but my question is what does it do for the above line in subclass this.price means where it is going to assign


my friend,
"this" refers to the current class
in Vehicle setPrice, i have declared local variable price

now of couse code will always refer to local variable and not to class variable.

hence "this" keyword is used.

the code would work fine in situation:



salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

santhosh.R gowda wrote:this method has been visible to subclass


only the method, not the variable.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
I've never seen the point to making a variable private, then having public methods to get and set it's values.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Its all to do with encapsulation, hiding away the internals from a client, so that changes to those internals do not have a negative, impact on client code.

public getter/setter and a private field allows for the implementation of the field to be hidden form the client.

So if for example you have a simple field that returns a value, it could in future become a computed value and not actual be a field anymore, because you have the accessors it means a client is impacted by the change. Where as with direct access the client would have to call the
method that computes the new value directly.
(Ok its a fairly contrived example)

By using accessors and hiding the implementation of the field, you also allow for a field to be read only, and I dont believe you can synchronise on a field.
By making a field public the class losses control other the field, it can be changed without the classes knowledge, this could be lead to very hard to find bugs. Espically in multi threaded applications.

Imagine a class with a static counter field, that is incremented each time the class is created, now imagine if that field was public, how could you be sure that the number of objects of that type is the same as the counter?

There maybe a few very very rare cases where a public field is a good idea but I cnat think of one.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I can see your point, Fred, but public get and set methods are not as simple as they look. You can put validation code into those methods, copy a reference being returned, etc.
It may be necessary for outside code to gain access to the value of those variables. You can also restrict alterations by restricting access to set methods.

Remember if there is a public get or set method in the superclass, the same public set or get method will be available FROM the subclass too.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Both responses make so much sense, I'm sure I learned this stuff at one point, then forgot it.

Such a programming technique might be unnecessary in someone's personal project, but in a large system with possible several programmers, it would be a different story.

regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help on inheritance...
 
Similar Threads
Instance of Operator
casting
Guess The result
please check this program
Static method can access in a subclass