2

I have this two classes:

public class Superclass 
{
  public Superclass();

  ...

}

and

public class ChildClass extends Superclass
{
  public ChildClass();

  public setname(String name)
  {
   ...
  }

}

if I do this:

Superclass a;
a = new ChildClass();
a.setname("Roger");

i get this warning: The method setname(String) is undefined for the type Superclass. How to resolve? Thanks

3
  • It's telling you that it knows that the object is of type ChildClass so it will perform the function but in other cases this would break. I would suggest using interfaces to understand inheritance more completely. Commented Aug 27, 2013 at 13:09
  • @ars265 - On the contrary, the compiler is telling you that the method cannot be called on the reference of type Superclass. It won't perform the function -- it won't even compile. Commented Aug 27, 2013 at 13:16
  • @AndyThomas, That's is what I thought but he said it was only throwing a Warning, not an Error so I made the assumption that it still compiled. My bad. Commented Aug 27, 2013 at 13:18

7 Answers 7

6

When you call a method through a reference of the Superclass type, you're limited to the Superclass API. The Superclass API does not include the method setname().

You could fix this by declaring the method in the superclass. In this example, it's declared as abstract so that the superclass does not need to define it.

public abstract class Superclass 
{
  public Superclass();

  public abstract void setname(String name);
}

Or you could change the type of the reference a to the subclass:

ChildClass a;
a = new ChildClass();
a.setname("Roger");
Sign up to request clarification or add additional context in comments.

4 Comments

.. or this works as well, related to my comment on the question.
the problem is that i need a to be an object of type Superclass as i have to pass it to a function of the tipe method(Superclass) but at the same time i have to set its attributes with methods of the ChildClass..
Your subclass can provide an implementation for a method that's declared in the superclass. At runtime, the actual method called will be the one in the subclass.
@beworker - Thanks for catching the missing "abstract" in a parallel edit.
2

Create a variable 'a' as

Childclass a = new Childclass();
a.setName("Roger");

Comments

1

Declare the variable as:

ChildClass a = new ChildClass();

You can still pass it into a method that takes an argument of type SuperClass. The idea is that a subclass should be substitutable for any of its superclasses.

Comments

1

a is a reference of Superclass type so it can only access the things present in Superclass. If you want access setname using the parent reference, then you need to have that method in parent class also.

Attributes and method accessible to a reference are only those which are present int reference class/interface and not in the containing object.

Comments

0

You are creating object of SuperClass type by using ChildClass only you can access the member and methods of SuperClass and subclass the reverse is not possible. ChildClass a=new ChildClass(); this will help you.

Comments

0

In parent child relationship you can take the reference of parent and can create the object of child as you have done.

Superclass a;
a = new ChildClass();

but there is one condition that is the object a follows the blueprint of its reference class, which means that in your case a has a reference of Superclass so a has the access of all the methods which are defined in Superclass not in ChildClass.

So your call to a.setname("Roger"); is not valid because a is of type Superclass which doesn't have setname(String) method but if you do something likeChildClass a = new ChildClass()then your call toa.setname("Roger");will be valid becauseais of type ChildClass which hassetname(String) method.`

One more thing, lets suppose you have a structure as :

class SuperClass
{
    public SuperClass();
    public void setName(String name)
    {
        System.out.println("setName() of SuperClass with name "+name);
    }
}

class ChildClass extends SuperClass
{
    public ChildClass();
    public void setName(String name)
    {
        System.out.println("setName() of ChildClass with name "+name);
    }
}

and you call it in main like :

    ChildClass a;
a = new ChildClass();
a.setname("Roger");

so it will compile and run fine with an output

setName() of ChildClass with name Roger.

and I am sure you know the real reason of this output now. :)

Comments

-1

Declare method setname as abstract in super class.

public abstract class Superclass 
{
  public Superclass(){
   }
  public abstract setname(String name);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.