0

So I have two methods one in a superclass and one in a subclass with the same name and parameters lets say public void method(). I have also another method in the subclass called method1() as follows:

public void method1() {super.method();} 

The code above when executed it goes as planned (the method1 is using the method from the superclass) BUT if I change it to this:

public void method1() {((Class)this).method();} 

Where Class is the name of the superclass for some reason it does not use the superclass method but the method of the subclass with the same name. Isn't super the same thing with ((Class)this)? Why does this happen?

EDIT: This is the actual class (I added the actual code so my problem is more clear)

public class MWindow extends Window { private String message = "No message"; protected int size = 7; public MWindow(String message) { size = 2; this.message = message; System.out.println ("Window message = " + message); } public MWindow(int size, String message) { super(size); this.message = message; System.out.println ("Window message = " + message); } public void setSize1(int y) {size = y;} public void setSize2(int z) {super.setSize (z);} public void printSize() {System.out.println ("MSize="+size);} public void printSize1() {System.out.println(((Window)this).size);} public void printSize2() {((Window)this).printSize();} } 

This is the superclass

public class Window { protected int size; public Window() { size=1; System.out.println("Window size="+size); } public Window(int size) { this.size=size; System.out.println("Window size="+size); } public void setSize(int x) {size += x;} public void printSize() {System.out.println("Size=" + size);} } 

This is the main() class

 public class RunWindow { public static void main (String[] args) { Window w1=new Window(); Window w2=new Window(2); System.out.println(w1.size); System.out.println(w2.size); MWindow mw1=new MWindow("First MWindow"); MWindow mw2=new MWindow(3, "Second MWindow"); System.out.println(mw1.size); System.out.println(mw2.size); mw1.setSize1(4); System.out.println(mw1.size); mw1.setSize2(2); System.out.println(mw1.size); mw1.setSize(2); System.out.println(mw1.size); w1.printSize(); mw1.printSize(); mw1.printSize1(); mw1.printSize2(); } } 

Executing the above we get:

Window size=1 Window size=2 1 2 Window size=1 Window message = First MWindow Window size=3 Window message = Second MWindow 2 7 4 4 4 Size=1 MSize=4 5 MSize=4 

The problem is that at the last result it should be Size=5 instead of MSize=4 since the superclass printSize method is called and not the printSize method of the subclass.

1
  • What is {((Class)this).method();}????. It is better If you post complete your classes. Commented Mar 18, 2015 at 18:33

2 Answers 2

3

This is how polymorphism works (and its late-binding mechanism). It doesn't take type of reference into account, but actual type of instance.

That is why your ((SuperClass)this).method(); will behave same as this.method().

Isn't super the same thing with ((SuperClass)this)?

So no, super and ((SuperClass)this) are not the same.

Via super.method() you are moving flow of control to code of method() available for superclass, but via (SuperClass)this you are just casting reference.

Sign up to request clarification or add additional context in comments.

7 Comments

What do you mean by "casting reference"? I am trying to understand the use of a ((Class)this).method() syntax
We can think of this as special kind of reference. Just like in case of Cat cat = new Cat() cat is reference which can be cast to Animal like Animal animal = (Animal)cat which creates new reverence of type Animal. But even if we do it and we will invoke animal.eat() code of Cat#eat() method will be invoked because of late binding. Similar thing happens here, but instead of cat we have this.
I still can't understand how it works. I am sorry I am new to java. What I don't understand is how the reference part works. I have a method in a subclass that refers to it. If I put ((Class)this).method() the reference changes from the subclass to the class named Class? And what happens when the reference changes in terms of output?
I am not sure where should I start explaining. Did you read article about late binding I included in my answer? If no then short form is that: binding (choosing which body of method to use) for non private, static, nor final methods happens at runtime (it is late binding) not at compilation time (early binding). What this means is that for regerences Animal a1 = new Cat(); Animal a2 = new Dog(); when you call a1.eat(); a2.eat() it will invoke for a1 body of Cat#eat() and for a2 Dog#eat(). That is what polymorphism is about.
When you are doing ((SuperClass)this).method() you can think of it as doing SuperClass sc = (SuperClass)this; sc.method(), but as explained earlier type of reference is not important, and body of method will be decided based on actual type of instance which sc holds (instance type doesn't change, only reference which you used to store it did).
|
0

No, in short what (Class)this is doing is converting the type of the reference the subclass to the superclass but the class that is referenced remains the same. Now in general if you have a reference of type that points to a and you call a method that overrides, the expected behaviour is to get the overridden method. So (Class)this is therefore not different.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.