Abstract Class or Method

Can we make use of "this" keyword reference inside a abstract method or abstract class? Explain

Questions by sagar salunkhe

Showing Answers 1 - 0 of 0 Answers

javafuns

  • Nov 5th, 2009
 

Yes we can. For e.g:


public abstract class TestEngine {
private String engineId;
private String engineName;
public TestEngine(String engineId , String engineName)
{
this.engineId = engineId;
this.engineName = engineName;
}
//public gettors and settors
public abstract void scheduleTest();
}


public class JavaTestEngine extends TestEngine
{
private String typeName;
public JavaTestEngine(String engineId , String engineName , String typeName)
{
super(engineId , engineName);
this.typeName = typeName;
}


public void scheduleTest()
{
//do Stuff
}
}


A class has a constructor so that when an instance of the class is created,
the fields of the class can be setup to a initial valid state.


Abstract classes define partial implementation of a public contract. Which
means that it may implement some of the methods and contains partially
implemented methods that are marked abstract.


Since abstract classes can have partial implementation and such partial
implementation can include fields of the class, a constructor becomes necessary
so that those fields are initialized to a valid default state when they are
created thru the constructor of their concrete subclasses.


All abstract class are not pure abstract, partially they are.  So if you
need a pure abstract class go for an Interface.

We can't use this with abstract class or method because this is use for current object while in case of abstract class or method we can't make object therefore there is no way of using this with abstract class or method.

  Was this answer useful?  Yes

Not true, abstract methods don't include the implementation but just the method prototype (method name and the arguments order and type included) and the return types as follows:

protected abstract void displayString();

So there is no question of this when you have all abstract methods. However you could always have non-abstract methods which can use this to refer to the current instance of the class.

whereas in the case of an abstract class you can always create one without having to define abstract methods.

Another usage of an abstract class is the follows example where you have static methods and obviously you cannot use this as this refers to an instance and cannot be used in static context.

Example above usage of abstract class besides implementation inheritance.

public abstract class ContextHolder {
  
   private static final ThreadLocal context = new ThreadLocal();

   // static getters and setters (need not be abstract)
}

This is different from singletons though but much simpler, you can never have multiple instances of this class and the variables being static there is only one for the class or for all instances.

  Was this answer useful?  Yes

dh_maan

  • Dec 21st, 2009
 

In abstract class, we cant use 'this' inside abstract method, but for concrete method we can use this. For example 


public abstract class Test {
public int i = 10;
public abstract void myAbsMethod();
public void myNonAbsMethod(){
System.out.println(this.i);
}
}

Abstract class is a class that is partly abstract. It means it does not fully abstract.
In abstract method u mujst  used abstract key world
for example abstract void show();
In abstract class  you used both method abstract and non abstract.
abstract class cannot create instrance.

  Was this answer useful?  Yes

kish39

  • Jan 13th, 2010
 

You canno write `this` keyword in abstract method but in abstact class non-abstact method you can use `this`.

Look at the bellow code

abstract class Test
 {
   
    public int i ;
    public Test()
    {
        this.i=20;
    }   
    
    public abstract void myAbsMethod();
   
    public void myNonAbsMethod()
   {
        System.out.println(this.i);
   }

}

public class ThisTest extends Test
{
    public static void main(String args[])
    {
        ThisTest obj= new ThisTest();
        obj.myNonAbsMethod();
        obj.myAbsMethod();
   
    }
   
    public void myAbsMethod()
    {
            this.i=40;
           
            System.out.println(this.i);
                   
      }
  
}

yogesh249

  • Jan 24th, 2010
 

Yes that is true that we cannot create object of an abstract class. But the question is "Can we use this keyword in an abstract class."


The answer is YES we can..
Have a look at the following code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package testapp;
abstract class XX
{
    private String name;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class YY extends XX
{

}
/**
 *
 * @author Yogi
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        YY y = new YY();
        y.setName("yogesh");
System.out.println(y.getName());
    }
}

java indian

  • Mar 10th, 2010
 

Yes we can use "this" key word in Abstract class or methods,
Here we have simple sample used of this key in abstract.

abstract class ab
{
int j;
void  meth()
  {
 this. j=10;
   System.out.println("inside"+j);
  };

  public abstract void scheduleTest();

}

class ABC  extends ab
{
    int j=20;
public  void scheduleTest()
    {
   System.out.println("in interfia");
    }


  public static void main(String a[])
  {
      ABC a1= new ABC();
      a1.meth();
   System.out.println("in interfia");
  }
 }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions