12 Replies - 1535 Views - Last Post: 04 May 2013 - 06:50 AM Rate Topic: -----

#1 pawan27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-May 13

usage of "this" keyword in Inheritance

Posted 03 May 2013 - 07:24 AM

I saw this following question in a book.
Quetion:In derived class constructors, ‘super’ and ‘this’ cannot be used together to invoke the constructors of base and derived respectively
Answer:True.

My Doubt is : can we use “this” keyword to access base class constructor.
If yes then how?

This post has been edited by Atli: 03 May 2013 - 07:27 AM
Reason for edit:: The [code] tags are for code only, not normal text.

Is This A Good Question/Topic? 0
  • +

Replies To: usage of "this" keyword in Inheritance

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:02 AM

In java you use super() to call the base constructor. If your derrived class has further code that needs to be implmeneted, then you would proceed with that code right after. Example:
public class A
{
   public A(){

   }
}

public class B extends A
{
   public B()
   {
      super();
      foo(); //some set of further instructions
    }

}

Infact, if you put nothing in your B constructor, it will default to A's constructor. So no unless you start playing with some really ugly generic object methods. But I think even that would stray from the question.
Was This Post Helpful? 1
  • +
  • -

#3 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:04 AM

Are you asking whether or not the following is valid?
class Base {
	public Base() {
		
	}
}
class Derived extends Base {
	public Derived() {
		this.super();
	}
}



No, it is not.

This post has been edited by Gungnir: 03 May 2013 - 08:10 AM
Reason for edit:: code tags

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:35 AM

View Postpawan27, on 03 May 2013 - 09:24 AM, said:

My Doubt is : can we use “this” keyword to access base class constructor.
If yes then how?


Mischievous answer:
import java.lang.reflect.*;

public class Garbage extends Junk{

	public static void main (String[] args){
		Garbage g = new Garbage();
		g.sillyMethod();
	}
	
	public void sillyMethod() {

		try {
			this.getClass().getSuperclass().newInstance(); 
			System.out.println("calling superclass constructor");
		}
		catch (Exception e)
		{
			e.printStackTrace(); //don't do this, kids!
		}
	}
}



So I used "this" and I accessed the base class constructor... do I win the gold-plated bagel?

Depending what you mean by "access" you could also use this.getClass().getSuperclass().getConstructors(); to get all of the constructors, for what that's worth...

This post has been edited by jon.kiparsky: 03 May 2013 - 08:36 AM

Was This Post Helpful? 3
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:40 AM

What a trivial display of your scholarly powers.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:41 AM

I prefer to think of it as an example of my absurdist humor
Was This Post Helpful? 1
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 09:33 AM

*
POPULAR

No one has addressed the use of the this keyword that I thought the question was hinting at. this() can be used in any class to call its own default, no-argument constructor. I'm not sure what the part of the question "‘super’ and ‘this’ cannot be used together" is driving at, but here's a runnable example that uses both the super and this keywords to call the parent (base) and derived (child) class constructors. One might argue whether they are being used "together".
public class ChildOrDerivedClass extends ParentOrBaseClass
{
    String stringInstance;
    
    // default, no argument child class constructor
    public ChildOrDerivedClass()
    {
        // the following line happens by default but can be
        // explicitly stated
        super();
        
        // the rest of the constructor
        System.out.println( "In the derived class' no-argument constructor." );
    }
    
    // single argument child class constructor
    public ChildOrDerivedClass( String stringInstance )
    {
        // calls the current class' default constructor
        this();
        
        // the rest of the constructor
        this.stringInstance = stringInstance;
        System.out.println( "In the derived class' single-argument constructor." );
        System.out.println( this.stringInstance );
    }
    
    public static void main( String[] args )
    {
        System.out.println( "Creating a child class using the no-argument constructor:" );
        new ChildOrDerivedClass();

        System.out.println( "\nCreating a child class using the single-argument constructor:" );
        new ChildOrDerivedClass( "Exiting the single-argument constructor." );
    }
    
}

class ParentOrBaseClass
{
    // default parent class constructor
    public ParentOrBaseClass()
    {
        System.out.println( "In the base class constructor." );
    }
}

The use of the this keyword with arguments can also be used to call other current class constructors. For example, this( "another string" ), would be acceptable in my runnable example.
Was This Post Helpful? 5
  • +
  • -

#8 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 10:40 AM

View Postjon.kiparsky, on 03 May 2013 - 10:35 AM, said:

Mischievous answer:
import java.lang.reflect.*;

public class Garbage extends Junk{

	public static void main (String[] args){
		Garbage g = new Garbage();
		g.sillyMethod();
	}
	
	public void sillyMethod() {

		try {
			this.getClass().getSuperclass().newInstance(); 
			System.out.println("calling superclass constructor");
		}
		catch (Exception e)
		{
			e.printStackTrace(); //don't do this, kids!
		}
	}
}



So I used "this" and I accessed the base class constructor... do I win the gold-plated bagel?

Depending what you mean by "access" you could also use this.getClass().getSuperclass().getConstructors(); to get all of the constructors, for what that's worth...


I was about to type something like this out. But I felt there would be more repercussions of him trying to remember any of it than benefit to showing him.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 10:43 AM

Yeah, should have added the standard warning: if you think you need reflection, you're probably wrong.

The second warning, too: If you think this case is an exception, you're still probably wrong.
Was This Post Helpful? 1
  • +
  • -

#10 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 10:51 AM

GregBrannon, great point but I think the essay question was alluding to using them in the same constructor method.
public class BadFoo extends Foo
{
 public BadFoo()
 {
   super();
    this();
 }
}


Which cannot happen because this() and super() need to be the first call.
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 08:05 PM

View Postpawan27, on 03 May 2013 - 10:24 AM, said:

My Doubt is : can we use “this” keyword to access base class constructor.

I think that GregBrannon answered that :^:
Was This Post Helpful? 0
  • +
  • -

#12 pawan27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-May 13

Re: usage of "this" keyword in Inheritance

Posted 03 May 2013 - 09:22 PM

View PostGungnir, on 03 May 2013 - 08:04 AM, said:

Are you asking whether or not the following is valid?
class Base {
	public Base() {
		
	}
}
class Derived extends Base {
	public Derived() {
		this.super();
	}
}



No, it is not.


Thanks alot 
Gungnir


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: usage of "this" keyword in Inheritance

Posted 04 May 2013 - 06:50 AM

for the simple reason that


 public Derived() {
        this.super();
    }


before the super() is called, the object does not exist yet so this points to nothing yet
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1