GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please somebody explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please somebody explain" Watch "Please somebody explain" New topic
Author

Please somebody explain

Nasir_Desi
Greenhorn

Joined: Oct 03, 2000
Posts: 1
I have this simple code from one of the sample tests. I am getting confused in line 14. I would appreicte if any Java Guru can explain that what is the value of b. Is it of Derived1 object or Base object.
Here is the code.
1: class Base{
2: // legal code
3:}
4: class Derived1 extends Base{
5: // legal code
6:}
7: class Derived2 extends Base{
8: // legal code
9:}
10 ublic class Test
11:{
12: static public void main(String [] args)
13: {
14: Base b = new Derived1 ();
15: Derived1 d1 = new Derived1();
16: Derived2 d2 = new Derived2();
17: b = (Base) d1;
18: d1 = (Derived1) b;
19: }
20:}
Harry Singh
Ranch Hand

Joined: Sep 22, 2000
Posts: 53
From one green horn to another ....
At line 14 the b has a reference type as Base but at runtime it denotes an object of type Derived1. This is ok because it is the superclass and can hold an object of any of its subclasses.
The last 2 statements ...
b = (Base) d1; // cast not really needed
d1 = (Derived1) b;
simply copy d1 to b and back to d1 again. They are absolutely redundant. The cast is not really needed but if you do put it, then you can call only methods of the base class using the reference d1.
DS_Malik
Greenhorn

Joined: Oct 02, 2000
Posts: 5
As per reply
(then you can call only methods of the base class using the reference d1.)
I have changed the code accordigly for methods in all classes and tried to compile it.I got following errors
:variable method not found in class
:not an expression statement
Can you please Explain ???
class Base{
public void method(){
System.out.println("Base 1 Method");
}
}

class Derived1 extends Base{
public void method(){
System.out.println("Derived 1 Method");
}
}
class Derived2 extends Base{
public void method(){
System.out.println("Derived2 Method");
}
}
public class Test
{
static public void main(String [] args)
{
Base b = new Derived1 ();
Derived1 d1 = new Derived1();
Derived2 d2 = new Derived2();
b = d1;
b.method;
d1 = (Derived1) b;
d1.method;
}
}

Please clear the java concept here ?
Thanks in advance
Dharam
Harry Singh
Ranch Hand

Joined: Sep 22, 2000
Posts: 53
OK. You got the error because you did not put the brackets after method.
it should be b.method(); // since its a function.

The concept here is as follows ...
Base b = new Derived1 ();
Derived1 d1 = new Derived1();
Derived2 d2 = new Derived2();
b = d1;
b.method();
d1 = (Derived1) b;
d1.method();
Both Base and Derived1 have a method called Method. Hence you can declare a reference of type Base but assign an object of type Derived1.
Base b = new Derived1();
b --- type Base
pointing to object of type Derived1.
If Derived1 had a method called DerivedMethod you cannot use
b.DerivedMethod() since DerivedMethod is not present in b.
Here is some code for that....

class Base{
public void method(){
System.out.println("Base 1 Method");
}
public void BaseMethod()
{
System.out.println("BaseBMethod Method");
}
}
class Derived1 extends Base{
public void method(){
System.out.println("Derived 1 Method");
}
public void Derived1Method()
{
System.out.println("Derived1 Method");
}
}
class Derived2 extends Base{
public void method(){
System.out.println("Derived2 Method");
}
public void Derived2Method()
{
System.out.println("Derived2 Method");
}
}
public class ClassTest
{
static public void main(String [] args)
{
Base b = new Derived1 ();
Derived1 d1 = new Derived1();
Derived2 d2 = new Derived2();
b = d1;
b.method();
d1 = (Derived1) b;
d1.method();
b.BaseMethod();
//b.Derived1Method();// Will give error
}
}

Mohnish Sharma
Greenhorn

Joined: Sep 05, 2000
Posts: 7
DS_Malik
I think what is happening is that the compiler is thinking that
b.method;
is part of an expression (like x = b.method) and is complaining that the expression is incomplete and the variable "method" is not defined in class.
Try changing the code to
b.method();
and
d.method();
I made the change and got the following output.
Derived 1 Method
Derived 1 Method

DS_Malik
Greenhorn

Joined: Oct 02, 2000
Posts: 5
Thanks to Mohnish and Harry for pointing me the error in my program.I under stand the concept now.
 
GeeCON Prague 2014
 
subject: Please somebody explain