• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help on inheritance...

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!




 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Wee Keong Soh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags to write code
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The super class (Vehicle).
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



only the method, not the variable.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never seen the point to making a variable private, then having public methods to get and set it's values.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic