• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

a doubt with 'extends'

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 class A
2 {
3 void callme()
4 {
5 System.out.println("A");
6 }
7 int r=10;
8 }
9 class B extends A
10 {
11 public void callme()
12 {
13 System.out.println("B");
14 }
15 int r=20;
16 }
17 class Q16
18 {
19 public static void main(String args[])
20 {
21 A a = new B();
22 a.callme();
23 System.out.println(a.r);
24 }
25 }

here the ans is b, 10 as methods r bound at runtime and variables r bound at compile time....
but here
1 class Base {}
2 class Agg extends Base
3 {
4 public String getFields()

5 {
6 String name = "Agg";
7 return name;
8 }
9 }
10 public class Inheritence
11 {
12 public static void main(String argv[])
13 {
14 Base a = new Agg();
15 System.out.println(a.getFields());
16 }
17 }

it gives compiler error instead of returning the name?...
Why is that so?....AM i missing something?....
Thanks in adv
Priya
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compiler error are you getting?
-Barry
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler error occurs because the Base class has no getFields() method
 
priya selva
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compiler error is....
Method getFields() not found in class Base
why dont that call the subclass' method...
Am i confusing something?...
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How can the baseclass now about the subclass method?
We will sublcass only when we want the base class functionality and some additional features. If the base class can call subclass methods , what is the need for the subclass?

HTH
Praveen
 
priya selva
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but in the first eg, y the base class calls the subclass' method(callme method)?...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Zarina said correctly the base class does not have the method getFields().
If you want to call a method in the subclass through the reference to a base class the subclass must override the base class version, which in this case does not exist.
-Barry
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here the ans is b, 10 as methods r bound at runtime and variables r bound at compile time....


sorry for this stupid inquiry but i just want to know what did you mean when you stated that statement above? Im just starting to grasp the OOP concepts...thanks!
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also whats the difference in the following declaration, during compile time and run time...
Base a = new Agg();
Agg a = new Agg();
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO these are basic question that should be moved to the beginner forum.
Two marvellous resources that will speed up your learning, and that are are also free are The Java Tutorial and Thinking in Java
There go the answers:
Base b = new Derived();
b.method();
The compiler will check that a method with the given name (method) and given parameters (none) was declared in the class which is the declared type of b; that is Base. If no such method exist the compiler will complain whether the existance or not of a similar method in the Derived class. By the way the compiler also looks for in the supertypes of Base.
At execution time the class of the real object pointed by b is examined. That is if the Derived class declared an overriding method, that will be called. This is why we say that the methods are resolved at runtime.
However:
Base b = new Derived();
System.out.println(b.field);
The compiler again will check that a field of the given name (field) will exist either in Base or in one of its supertypes. If not, the compiler will complain and refuse to produce the class file.
At runtime the field that will be accessed is the defined in the declared type of b, regardless any possible declaration in Derived. That is the reason why we say that fields are resolved at compile time.
BTW useful abreviation:
Base b;
Base is the declared type of the variable b

hope helps
 
Catch Ernie! Catch the egg! And catch this tiny ad too:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic