• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

a strane doubt over method calls .

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have asuper class sup & a subclass sub.
sup is empty.
sub has a method meth()
in a third class i write
sup s = new sub();
s.meth();
this gives me a compile error saying "method meth() not foundin superclass.
why do i needto havethat method in superclass.
please clarify.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like it's because of late binding - at compile time, it's not clear that s is actually an instance of the sub-class because you've said "sup s = . . .". It's only at run-time that the JVM works out that s is actually a instance of sub. So it falls over at compile time because it's treating s as a sup and there is no equivalent method in the superclass.
Hope this helps,
Kathy
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think your program is like this:
class sup{
}
class sub extends sup{
void meth(){}
}
public class supsub{
public static void main( String argv[] ){
sup s=new sub();
s.meth();
}
}

I do agree with rogers. And also if we say in other way,
you are making instance of sub class but type is sup i.e.
super class and your meth() method didn't find any equavalent method which it would override in super class. So meth() method is being created as a new method in your subclass not overridden
method in super class. So how come type super class works.
- Golam Newaz

- Golam Newaz

------------------
 
shilpa gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesit mean that evtime i assign a subclass to superclass and need to call a method from subclass that methd shold be present in superclass also.
can you please explain lte binding concept in little depth
thanx
shilpa
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shilpa!
please refer to Chapter6 Object_Oreintedprogramming in khalid mughal's book.He has clearly explained it.
 
shabbir zakir
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi shilpa!
please refer to Chapter6 Object_Oreintedprogramming in khalid mughal's book.He has clearly explained it.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shilpa,
This is a simple case of late-binding.Here you are overriding
the method in the base class.This means there should be already
a method in the base class which you are going to modify (or)
overridde.So during compile time checking is done whether there
is same method definition in the base class.Since it is not there in your program it shows compile time error.
Here is an example for your understanding
#########################################
class supercl{
void meth()
{
System.out.println();
}
}
class subcl extends supercl{
void meth()
{
System.out.println("Hello Javaranch");
}
}
public class exoverride{
public static void main( String argv[] ){
supercl s=new subcl();
s.meth();
}
}
Hope this helps
Cheers
Lala
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pooja,
java has two checks one at compile time and one at runtime
now at compile time it tries to make sure that the method called is there in the object.
Late binding is used for polymorphism
the method which has to be called is not decided at compile time but at runtime depending upuon the object stored in the variable.
that is if the method is overridden.
But if u call a final method of a class then compiler will resolve it at runtime and because of this final method calls are faster since late binding takes some time in methos lookup and in case of final methods late binding is not needed.

Cherry
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let me try to catch u all blind side:
a super class has no way of knowing the methods of the subclass...
thus the s.meth() will obviously try to look for the method in the super class, since s is a sup reference
am i right??
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code shows that the actual object is of type sub but is being casted to sup. So the compiler assumes it to be of sup and looks for the method meth() in super class.
sup s = new sub();
The only time s will invoke a function of sub class if that method of sup is overidden by sub.

------------------
http://www.mantrotech.com
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic