Malwinder Singh
Malwinder Singh

Reputation: 7060

Object of child class cannot call its own methods

If c is a ChildClass object then why can't it call methods of ChildClass? Like:

ParentClass c=new ChildClass(); //Here ChildClass extends ParentClass

In particular:

Object s=new StringBuffer();

Here s is object of StringBuffer, but s.append(null) is compile time error. Why so?

Upvotes: 0

Views: 911

Answers (5)

sara Sodagari
sara Sodagari

Reputation: 423

you have to cast your object to stringbuffer type or you have to declare s object like this stringbuffer s =new stringbuffer() then you can call all method of this class

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79838

It's the compiler's job to make sure that you don't call any methods that don't match the variable that you're calling it on. To assist the compiler with this, we give all variables a "type". A type might be a primitive type like "int", or a class, an enum or an interface. If it's a class, an interface, or an enum; then the methods that are listed in that class, interface or enum are the ones that the compiler will let you use, on that variable.

The "type" of the variable isn't necessarily the same as the "class" of the object that the variable refers to. In your example, the type is Object, but the class is StringBuffer. But the type, not the class, is what the compiler uses to check that your method calls are OK; because the compiler can't be expected to know the class of every object that you're ever likely to use a variable to refer to.

If the language didn't work this way, there'd be no point in variables having types at all. That's the way Javascript works - everything is declared as var and there's no real type system. When we say Java is "statically typed", we mean that we have to give variables types, and that doing so helps the compiler to protect us from misusing our variables.

Upvotes: 2

NielsonRock
NielsonRock

Reputation: 1

you need to cast the object to use methods of the instance

Object s = new StringBuffer();
StringBuffer s2 = (StringBuffer) s;
s2.append(null);

despite being an instance of StringBuffer, it remains of type Object, and Object not have method append

Upvotes: 0

ajb
ajb

Reputation: 31699

c is declared to be a ParentClass. Therefore, all the compiler knows about it is that it is a ParentClass or some subclass of it. Suppose some other statement reassigned c somewhere:

c = new SomeOtherChildClass();

which is legal, because SomeOtherChildClass is also a subclass of ParentClass, and you've told the compiler that c can be any ParentClass. Now, if the compiler sees

c.methodOfChildClass();

how will it know that c is a ChildClass and not a SomeOtherChildClass that doesn't have that method?

P.S. If you're sure c is really a ChildClass, at some point, you can get it to call a ChildClass method by downcasting:

((ChildClass)c).methodOfChildClass();

The cast will cause the compiler to see c as a ChildClass; at runtime, the program will check to make sure c really is a ChildClass or else the program will throw ClassCastException.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240908

because compiler doesn't know that reference s is going to be referring to an instance of StringBuffer at runtime and Java is statically typed language

Upvotes: 1

Related Questions