The Artima Developer Community
Sponsored Link

Java Answers Forum
Inheritance upcasting Polymorphism - help need to understand the logic

1 reply on 1 page. Most recent reply: Jan 15, 2003 4:20 AM by Sarma K T R B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
sivanantham kandan

Posts: 3
Nickname: sivaparam
Registered: Dec, 2002

Inheritance upcasting Polymorphism - help need to understand the logic Posted: Dec 28, 2002 11:54 AM
Reply to this message Reply
Advertisement
I have appended simplified example with an output. The very only difference is, method tune in WindA is static. ( static void tune(int x ) { )

The question is,
Pl. refer the example" WindA.java" , How come " i1.tune(3);" is calling base class method, but it is not in "WindB.java".

I am struggling to follow this logic, please help.

Thanks in advance.

thanks
siva


WindA.java:=
-----------------
//: c06:Wind.java

// Inheritance & upcasting.



class Instrument {
static int y = 100;

Instrument(){
System.out.println("Constructor Instrument...");
}

static void tune(int x ) {
System.out.println("...called tune from Instrument with no arg - "+x+","+y );
}

}




// Wind objects are instruments
// because they have the same interface:

class WindA extends Instrument {
static int y = 200;

WindA(){
System.out.println("Constructor Winda...");
}

static void tune(int x ) {
System.out.println("called tune from Winda with no arg...: "+x );
}


public static void main(String[] args) {


WindA w1 = new WindA();
Instrument i1 = new Instrument(), i2 = new WindA();



w1.tune(1);
i1.tune(2);


i1=w1;
i1.tune(3);


}

} ///:~





Output:-
=========
D:\siva\reading\java>java WindA
Constructor Instrument...
Constructor Winda...
Constructor Instrument...
Constructor Instrument...
Constructor Winda...
called tune from Winda with no arg...: 1
...called tune from Instrument with no arg - 2,100
...called tune from Instrument with no arg - 3,100

D:\siva\reading\java>




WindB.java
==============

//: c06:Wind.java

// Inheritance & upcasting.



class Instrument {
static int y = 100;

Instrument(){
System.out.println("Constructor Instrument...");
}

void tune(int x ) {
System.out.println("...called tune from Instrument with no arg - "+x+","+y );
}

}




// Wind objects are instruments
// because they have the same interface:

class WindB extends Instrument {
static int y = 200;

WindB(){
System.out.println("Constructor Winda...");
}

void tune(int x ) {
System.out.println("called tune from Winda with no arg...: "+x );
}


public static void main(String[] args) {


WindB w1 = new WindB();
Instrument i1 = new Instrument(), i2 = new WindB();



w1.tune(1);
i1.tune(2);


i1=w1;
i1.tune(3);


}

} ///:~




Output:-
==========

D:\siva\reading\java>java WindB
Constructor Instrument...
Constructor Winda...
Constructor Instrument...
Constructor Instrument...
Constructor Winda...
called tune from Winda with no arg...: 1
...called tune from Instrument with no arg - 2,100
called tune from Winda with no arg...: 3

D:\siva\reading\java>


Sarma K T R B

Posts: 5
Nickname: sarma2312
Registered: Jan, 2003

Re: Inheritance upcasting Polymorphism - help need to understand the logic Posted: Jan 15, 2003 4:20 AM
Reply to this message Reply
hi,
Here the problem is

When you declare a method as static all references to it will be resolved at compile time rather than at runtime so when the compiler encounters the statemet

i1=w1; it will look for i1 as i1 is the reference of Instrument class the base class method is executing because i1 is actually the reference of Instrument class and late binding is applicable only to non static methods.

Hope this is clear...

Sarma

Flat View: This topic has 1 reply on 1 page
Topic: How can i use java  Applet to make Notepad..... Previous Topic   Next Topic Topic: LetterCount

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use