• 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

I need to have the same method in all subclasses

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Suppose that i have a series of classes that extends another class.
Each oh those sublclasses have a property "name".
So:







What i need is that every "Son" should have this method:



I can create it manually in each subclass, but isn't there a system for automatically make it avaible in all sublcasses? If i create it into the Father class, i think that the "this" keyword only refers the Father itself....
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method is exactly the same in each of the subclasses, then you should put the method (and the member variable "name") in the superclass and not in the subclasses - the subclasses will then inherit the method from the superclass.

If it needs to be different in some of the subclasses, then you can override the method in those subclasses to provide the right implementation for that subclass.

Luk Cora wrote:If i create it into the Father class, i think that the "this" keyword only refers the Father itself....


No, that's incorrect. "this" will refer to the actual object, which is an instance of one of the subclasses.

If you feel you need to copy-and-paste code in multiple classes, then a warning bell should go off in your head, it's an indication that you're not designing your classes correctly.
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I tried solving problem of op but got trapped in the problem myself,
This code is printing:
My name isnull
My name isnull


If someone can help?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sachin: That is because line 6 in your code is the wrong way around; it should be: this.name = name; instead of name = this.name;

Also, you should remove line 20. The superclass constructor has already done the assignment, it's not necessary to do it again in the subclass.

Also, you are passing a variable "name" to the constructor of Sub, but that name is ignored, because you are calling the superclass constructor with a fixed string "Varun". Line 19 should have been: super(name);
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i remove constructor like this



Then it prints:
My name isVarun
My name isVarun
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables cannot be overridden. The member variable "name" in class Sub does not override the member variable "name" in class Super. (Only non-static, non-final methods can be overridden).
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So by this you(op) can achieve what you were looking for
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name field should be removed altogether. You should not initialise the field where you declare it, but in the constructor.
This is what you wrote:-This is what it would have looekd like if you had indented it (really, posting unindented code!)… and this is what it would have looked like if you had used propper object‑oriented design:-Must be called with
java NameDemo Sachin Varun
or similar.

Note what is in Sub. No methods. No fields. No nothing … well, maybe a constructor. I have even marked the methods final to emphasise that.
 
Sachin Tripathi
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too can't believe I posted code without proper indentation, actually I was working on low battery laptop, so had to finish my work before it gets shutdown

I totally agree with your explanation Campbell,but what about the method signature given by op
 
Campbell Ritchie
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sure you can write a sayMyName method for that Super class, even before your battery packs in. Mark it final.

And sorry for any rudeness about your bad luck with the battery.
 
Everybody's invited. Even this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic