• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Using Getters/Setters from an "EXTENDS" class

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In "... class ClassM extends ClassU...", where ClassM is the Main (lead-off) class, I found thru the debugger that ClassU executes first. ClassU may need to get info from ClassM via a Getter/Setter mechanism, but I don't know how to do this. I can't instantiate ClassM from ClassU so how ?

How about the reverse? Can ClassM reach ClassU's getter/setters, if can that be done; if so, how?

Thank you!

PS in Java class these types of issues were never discussed, and I didn't know enough to ask
 
Marshal
Posts: 27365
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mort Bundersnatch wrote:In "... class ClassM extends ClassU...", where ClassM is the Main (lead-off) class, I found thru the debugger that ClassU executes first. ClassU may need to get info from ClassM via a Getter/Setter mechanism, but I don't know how to do this.



That's because you shouldn't do it. A class should never depend on its subclasses and ideally it shouldn't even know that they exist. If you find yourself breaking that rule then your design has problems. Would you like to post the details for discussion?

PS in Java class these types of issues were never discussed, and I didn't know enough to ask



Yeah, Java is supposed to be an object-oriented language but you'd never know it based on some Java courses.
 
Paul Clapham
Marshal
Posts: 27365
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Saloon Keeper
Posts: 26011
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can invoke a parent class's set.get methods from code in a subclass, and contrary to what some here seem to be saying, it is, in fact a good and reasonable thing to do. Better than accessing the raw parent properties directly, for certain, and after all, what's the point of inheritance if you cannot inherit?

However, there is a constraint on this. To access any method or property of a parent class, that method/property must have an allowable scope. If the method or property is defined with private scope, it's inaccessible, even to subclasses. If it's protected scope, then it's inaccessible to external classes, but not to subclasses. If it's package scope, then it's accessible to any class in the same package, but not to objects of classes defined in other packages.

The guiding rule is always to make stuff accessible to those who truly need it, and no one else. Mostly you want properties to be private, accessors are generally public. Some accessors may be for internal support and/or subclasses or test-framework classes, so they would have their scopes defined accordingly.
 
Paul Clapham
Marshal
Posts: 27365
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Robert Houston wrote:How about the reverse? Can ClassM reach ClassU's getter/setters, if can that be done; if so, how?



Of course. ClassM is a subclass of ClassU, so all the methods of ClassU are automatically methods of ClassM.

So yeah, you can access them from ClassM -- unless you declared them private. But you didn't do that, right? They are getters and setters so it wouldn't make sense for them to be private.
 
Robert Houston
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! Your response helped me stay out of some rabbit holes. Shows me how inadequate I am at Java.
I have some new Q's I'll post soon.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic