Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the keyword 'this' be used in an abstract class in Java?

Tags:

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

like image 586
Reddy Avatar asked Jun 09 '10 07:06

Reddy


People also ask

Can we use this keyword in abstract class in Java?

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.

Can we use abstract keyword with variable?

Abstract methods cannot be declared static. An abstract keyword cannot be used with variables or constructors.

Can we use static keyword with abstract class in Java?

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.

Is abstract keyword necessary in Java?

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.


1 Answers

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.

Overriding fields

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 :-)

Creation of objects of abstract classes

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.

like image 109
aioobe Avatar answered Oct 28 '22 22:10

aioobe