Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I need to have the same method in all subclasses  RSS feed

 
Luk Cora
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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....
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16029
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16029
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 368
3
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16029
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So by this you(op) can achieve what you were looking for
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • 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: 368
3
  • Mark post as helpful
  • send pies
  • 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: 55799
164
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!