I tried with below example, it is working fine.
I expected it to pick sub-class's value since object won't be created for super class (as it is abstract). But it is picking up super class's field value only.
Please help me understand what is the concepts behind this?
abstract class SuperAbstract { private int a = 2; public void funA() { System.out.println("In SuperAbstract: this.a " + a); } } class SubClass extends SuperAbstract { private int a = 34; }
I am calling new SubClass.funA();
I am expecting it to print 34, but it is printing 2.
P.S.: What I want to know is why using this in an abstract class not giving me an error?
As below text is emphasizing this
would work on an instance and abstract classes won't have an instance.
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. from: http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html
3 Answers. Show activity on this post. To answer the question in the title: Yes, this can be used in an abstract class. An abstract Animal is created at the same time as a Dog is created.
Abstract methods cannot be declared static. An abstract keyword cannot be used with variables or constructors.
Yes, of course you can define the static method in abstract class. you can call that static method by using abstract class,or by using child class who extends the abstract class. Also you can able to call static method through child class instance/object.
You can provide the keyword, though, but there won't be any difference. Within abstract classes, however, when you want to denote a method as abstract , you're required to type the abstract keyword. This actually makes the code more readable and easy to be understood.
To answer the question in the title: Yes, this
can be used in an abstract class. An abstract Animal
is created at the same time as a Dog
is created.
You can't override fields the way you have tried it. Fields "are not virtual" like methods.
From Java Quick Reference: Overloading, Overriding, Runtime Types and Object Orientation - Overriding Methods
- fields cannot be overridden but they can be hidden ie if you declare a field in a subclass with the same name as one in the superclass, the superclass field can only be accessed using super or the superclasses type
If you could, the field would probably have had to be at least protected :-)
since object won't be created for super class (as it is abstract)
It is actually instantiated.
The abstract
keyword only ensures that, when instantiated, it's instantiated in the form of a subclass. When instantiating a Dog
, you're at the same time instantiating an Animal
! The this
reference in the context of an Animal
will thus always refer to a Dog
or a Cat
or whatever, but in all cases it refers to some Animal
. :-)
As the example below illustrates, the this
reference makes sense even in an abstract class:
abstract class Animal { public String name; public Animal(String name) { System.out.println("Constructing an Animal"); this.name = name; } public abstract void speak(); } class Dog extends Animal { public Dog(String name) { super(name); System.out.println(" Constructing a Dog"); } public void speak() { System.out.println("Bark! My name is " + name); } } public class Test { public static void main(String... args) { new Dog("Woffy").speak(); } }
Prints:
Constructing an Animal Constructing a Dog Bark! My name is Woffy
Update: The this
reference refers to the same object in the super class as in the sub class.
You could try to add
public Animal getSuperThis() { return this; }
to the animal class, and do
System.out.println(this == getSuperThis());
in Dog.speak()
. You would see that it prints true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With