• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Calling a method  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
class Base {}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());
The correct answer is 4, why is not 1??
Thank in advance.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think 1) will compile. Since a is of type Base, the compiler will look in class Base for the method, but will not find it.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct.
a is an object of type Base and Base does not have a getFields() method.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain the difference between
a.getFields() and
( (Agg) a.getFields() )
In this programm, both method calls work, but whats the difference?
class Base
{
public String getFields(){ String name = "Base"; return name; }
}
class Agg extends Base
{
public String getFields(){ String name = "Agg"; return name; }
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
System.out.println( a.getFields());
//System.out.println( ((Agg) a).getFields()); Works, too
}
}
Result: "Agg" is printed
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thomas,
The code posted by Jordi will not compile as the there is no method named getFields() defined in the class Base.It will compile if we cast the reference variable a for the object of class Agg like ((Agg) a).getFields().

It will make you understand dynamic binding or polymorphism much much better.
If you remove getFields() from Base class, you actually take getFields() out of the polymorphism between Base and Agg class. Compiler only know b is refer to a Base class, which does not have the method getFields(), compile time error!!!
Remember Agg ISA Base. Compiler does not care which child/grandchild/grandgrandchild down the hierarchy b is actually referring to, and which getFields() it should call. However, they all have an getFields() (polymorphism). It is a runtime decision. Dynamic Binding.
Dynamic binding, or binding at runtime only for those methods defined in Base class, either inherited or overrided by the subclass. The override can be already happened now or will happen in the future, the compiler does not care.

I hope it will help.
Gurpreet Sachdeva
Please visit for all type of info. about SCJP (Including mock exams) at: http://www.go4java.20m.com
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Thomas' example, there is no difference between these two:
a.getFields() and
( (Agg) a.getFields() )
They will both run the Agg getFields() method.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!