• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

is this.setSuspension(suspensionType); same as suspension = suspensionType; ???

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys

will it be same if we type suspension = suspensionType; instead of this.setSuspension(suspensionType); ???
i know it will be the same because i tried it .....
but could you tell me why they have called the method instead of simply typing suspension = suspensionType; i mean what is the reason behind using it?

 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:hey guys
will it be same if we type suspension = suspensionType; instead of this.setSuspension(suspensionType); ???



Yes. It is Same. this refers to the currently executing object .
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:hey guys
but could you tell me why they have called the method instead of simply typing suspension = suspensionType; i mean what is the reason behind using it?


In constructor you can use both :
this.suspension = suspensionType; and setSuspension(suspensionType);

I think It is better to use setSuspension(suspensionType) If you need to make some changes to the passed argument or validate if before assigning it to the instance variable.

Even if you dont want to make the validations etc. now, but in future you can add that code in setSuspension() method easily and everything will work fine.


 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Joshi wrote:I think It is better to use setSuspension(suspensionType) If you need to make some changes to the passed argument or validate if before assigning it to the instance variable.



There is one problem with this, though. If it is possible for a subclass to override the method (which it is in this case), you can get a situation where the subclass can break the initialisation of superclass state - which ideally it wouldn't be able to do. It's often recommended not to call methods in a constructor that can be overridden.

If the method is private, or is marked final, it cannot be overridden, which is one solution.
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:
There is one problem with this, though. If it is possible for a subclass to override the method (which it is in this case), you can get a situation where the subclass can break the initialisation of superclass state - which ideally it wouldn't be able to do.


Even if subclass overrides the setSuspension() method, subclass wont be able to initialize the instance variable unless it calls super.setSuspension() because suspension is private instance variable. Can you please explain the situation you are talking about.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Joshi wrote:Even if subclass overrides the setSuspension() method, subclass wont be able to initialize the instance variable unless it calls super.setSuspension() because suspension is private instance variable. Can you please explain the situation you are talking about.



OK, this is a slightly silly example. But I can do this:
And now suspension is never initialised, even though the MountainBike constructor thinks it has been.

The point is that while the superclass obviously can't prevent subclasses doing anything stupid, what it can do is ensure that all state associated with the superclass is correctly initialised. The subclass shouldn't be able to stop that.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piyush Joshi wrote:Can you please explain the situation you are talking about.





tested? got the value you have set while object creation? now just comment (1) and uncomment (2) and test again !
you should get your original value. This is the exact situation what Matthew was told.
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:And now suspension is never initialised, even though the MountainBike constructor thinks it has been.

The point is that while the superclass obviously can't prevent subclasses doing anything stupid, what it can do is ensure that all state associated with the superclass is correctly initialised. The subclass shouldn't be able to stop that.



I think superclass can always ensure correct initialisation of it's state even if subclass overrides setSuspension().
It's upto the subclass what value it wants to pass to the superclass part.



Got it! I Should have tested the code before posting. Thanks
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic