Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please somebody explain

 
Nasir_Desi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Mohnish and Harry for pointing me the error in my program.I under stand the concept now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic