Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

confused over use of this versus getters  RSS feed

 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have super class Vehicle which has fields topSpeed and numWheels.
Then a class Car which extends Vehicle. It has fields manufacter and model.
And finally a class named ToyotaSupra,extends class Car and has just one more field, color.

Now in this Toyota Supra what would be the right way of printing the fields in the screen with a function in this class?
numWheels is part of Vehicle which is the leader. Should i write or ?

Similarly for field manufacter which is part of class Car. In main to print should i write or ?

EDIT; sorry i want to have a print function on this ToyotaSupra class but i dont know what is the correct way of getting the values.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the member variables (numWheels and manufacturer) are accessible in the subclass (i.e. they are not private in class Vehicle) then it doesn't matter. You can even leave off the "this." prefix.

Normally, it's good practice to make member variables private unless there's a really good reason to use a different access modifier.

If a member variable is private, it's only accessible directly from the class in which it is defined. If you'd try to access it from a subclass, for example by using this.numWheels, you'd get an error.
 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They were indeed private fields. So whenever i want to acess something private from another class getters and setters is the way to go?
Now if i'am printing a field specific to that class. In above eample in class ToyotaSupra if i want to print the color it doesnt matter which way i go, i can use getters and this, right?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Yates wrote:They were indeed private fields. So whenever i want to acess something private from another class getters and setters is the way to go?

It's the only way to go, in fact.

Now if i'am printing a field specific to that class. In above eample in class ToyotaSupra if i want to print the color it doesnt matter which way i go, i can use getters and this, right?

For me, it depends. If my class is final (which it usually is) I use the fields directly. If they're not, I use the getters, because a subtype can override them to give a different implementation.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put as many methods, fields, etc, into the Vehicle class as possible, instead of the subclasses.
Declare all your subclass references as Vehicle.Make the Vehicle class abstract.
Use the Vehicle version of the methods until you need something different, when you can always override the method.
Remember the super.foo() idiom which allows you to compose an overridden method mostly from the superclass' method with only small additions.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C[quote=Stephan van Hulst wrote:If they're not, I use the getters, because a subtype can override them to give a different implementation.


Can you show us how?(code snippet)
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the getters in the subtypes? Put all the code in the abstract class and there is no need to use it in the subtypes (well, that applies for about 98% of cases). Judicious use of getClass in the toString method allows you to write one toString method and not have to override it again.Additional:

Once you have filled in the arguments in the constructor, that is the SpysCar class complete. Apart from it showing it is doing 65mph when it is actually travelling at 130, you don't need to override anything else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!