Click to See Complete Forum and Search --> : How can access super class instance variable in a sub class method


ravibachwala
09-04-2007, 01:14 PM
i am using two classes in which the super class consists of instance variable and i want that instance variable to be access in the subclass method.....

example:

class Figure
{
double length;
double breadth;
}

class Rectangle extends Figure
{
Rectangle()
{
super();
}
double area() // i want to acess this method.......
{
return(length * breadth);
}
}

class FigureDemo
{
public static void main(String[] args)
{
Figure f = new Rectangle();
f.length = 10.0;
f.breadth = 10.0;
f.area();
System.out.println("Area of Rectanle is"+ f.area());
}
}


Note: I want to excute the same code return in the Main method.
How to can i access super class instance variable in sub class method.. please help out on this........


Thanks & regards,

Ravi

Khalid Ali
09-04-2007, 06:47 PM
by looking at your code, I can not see a method area defined in the parent class (which is Figure from ur code).
Any public methods in the parent class should be accessible in the child class.
If you change your code like this

Rectangle f = new Rectangle();
now you will be able to access methods in the Rectangle as well as Figure class..

ravibachwala
09-05-2007, 06:43 AM
Hi ,

I dnot want my main method to be change...... i need to run same code from the main entry point.....


// This is super class

class Figure
{
double length;
double breadth;
}

// This is my Sub class

class Rectangle extends Figure
{

// Here's is my method to be acessed.......i.e area()

double area()
{
return(length * breadth);
}

}


/// Main Method starts here and I doesn't want to change any code in this main method


class FigureDemo
{
public static void main(String[] args)
{
Figure f = new Rectangle();
f.length = 10.0;
f.breadth = 10.0;
f.area();
System.out.println("Area of Rectanle is"+ f.area());
}
}


Thanks,
Ravi

shimms
09-05-2007, 07:08 AM
You can't call a method declared in the sub class from an instance of the super class. The super class doesn't know anything about area().

If you want to call a method that belongs solely to the subclass, you must instantiate a type of that subclass, ie:

Rectangle f = new Rectangle();

// can now call area():

System.out.println(f.area());

If you really need to declare f as a type of Figure you must define area() in Figure. There are a number of ways to do this, you can either define Figure as an abstract base class, and declare the area method as abstract:

abstract class Figure
{
double length;
double breadth;

abstract double area();
}

Then the compiler will know that anything that inherits from Figure will implement the area method. This will have the side affect of not being able to instantiate objects of type Figure though - this may be an issue, I don't know the rest of your code base.

At run time the JVM will look at the dynamic type to resolve which implementation to call, at compile time the compiler will look at the static type to ensure the method exists.

Based on your code above, you aren't guaranteeing the compiler than all instances of Figure will have an Area method - the compiler thus wont let you call an method that it can't confirm exists.

Hope this helps.