This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Variable and Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variable and Instance" Watch "Variable and Instance" New topic
Author

Variable and Instance

Chuan Chung Tsao
Greenhorn

Joined: Sep 27, 2003
Posts: 2
public class A{
int i;
public void m(){}
}
public class B extends A{
int i;
public void m(){}
public static void main(String[] arg){}
}
if we in main method add:
a) A a = new A();
b) B a = new A();
C) A a = new B();
d) B a = new B();
and add respectively:
a.i;
a.m();
how do we to understand its result, and if we announce m() and i as static in the different class, what is its result? And how could we remember it easily? Thank you.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I don't understand your question, or are you asking multiple questions?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Hi Chuan Chung Tsao
Following is a modified version of your program so as to make u understand logic well.......
class A
{int i = 10;
public void m()
{System.out.println("In m() of Super Class");}
}
public class B extends A
{int i = 20;
public void m()
{System.out.println("In m() of Sub Class");}
public static void main(String[] args)
{ A a = new A();
System.out.println("value of i :: "+a.i);
a.m();
}
}

1>
A a = new A();
value of i :: 10
In m() of Super Class
Logic :: Instance of Superclass A is created and thus variable and method of superclass are accessed.
*********************************************
2>
A a = new B();
value of i :: 10
In m() of Sub Class
Logic ::
Dynamic Method dispatch is what is happening here.
Instance created is of type Class A but is pointing towards Class B. As a result of which method of class B ie subclass is accessed.
Variable accessed however of superclass as variables are not overrided in case Dynamic Method dispatch.
*********************************************
3>
B a = new B();
value of i :: 20
In m() of Sub Class
Logic :: Instance of Subclass B is created and thus variable and method of subclass are accessed.
*********************************************
4>
B a = new A();
Compile Time Error :: Incompatible types
B a = new A();
Logic :: Casting is required.
Solution -> B a = (B) new A();
This however will again cause ClassCast Excetion as instance of Superclass can not be casted to instance of Subclass. Converse however is correct.
Hope this clarifies your doubt ... In case u need more information just let me know....


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199

In case u need more info on Dynamic Dispatch
Dynamic Inheritance in Java!
Have a look at
http://www.cyberdyne-object-sys.com/oofaq2/DynInh.htm
 
Don't get me started about those stupid light bulbs.
 
subject: Variable and Instance
 
Similar Threads
Invoking methods from constructors
Polymorphism
threads
Memory Allocation and Benifit of an Interface Referance Variable over class referance
Use Of Interface