Basic inheritence question

• 03-17-2012, 01:25 AM
jubbiejub
Basic inheritence question
I'm having a hell of a time finishing this assignment.

"RK4 is a numerical integration technique that can be used to evaluate the integral of a given function over a given range. The function will be defined in your class as

double f(double t, double y)

The functions you will be integrating are the following:

log(x) / x5 from 1 to 2
log(x)3 from 0.001 to 5
x3ex2 from -2 to 2
x3 cos(x2) from 0 to pi

Using one of your classes from Assignment 4 as a base class, write a class for each of the above functions. You are only allowed to override f(), and you may not make changes to your base class."

Here is my base class:

Code:

```public class RK4{ public double f(double x, double y){ return 5 - 1/x + 3 * x * x; } public double rk(double x, double y, double step){ double k = 0.001;  while(k<=20){     double h = step;     double k0 = h * f(x, y);     double k1 = h * f(x + 0.5 * h, y + 0.5 * k0);     double k2 = h * f(x + 0.5 * h, y + 0.5 * k1);     double k3 = h * f(x + h, y + k2);     return y + 1.0/6.0 * (k0 + 2 * k1 + 2 * k2 + k3); }     return x; } }```
And here is my attempt at using override and inheritance:

Code:

```public class RK4one extends RK4{ public double f(double x, double y){     return (Math.log10(x))/(x*x*x*x*x);     }{ rk();         } }```
Can somebody please explain what I am supposed to be doing? I don't quite understand this(); or super ();.
• 03-17-2012, 03:33 AM
ozzyman
Re: Basic inheritence question
Did you have any compile errors or runtime errors?

What are you having trouble with?

'this' is a keyword used to reference the class from which the keyword 'this' is used.

'this()' refers to the constructor from 'this' class:
Code:

```int someField; //constructor 1: RK4() {   this(0); //calls constructor 2 with '0' as the parameter } //constructor 2: RK4(int aNumber) {   someField = aNumber; }```
'super' refers to the parent class and 'super()' refers to the parent class constructor, example:
Code:

```class BorderPanel extends JPanel {     super(); //JPanel constructor     setBorder(BorderFactory.createLineBorder(Color.black)); }```
Hope this helps...

p.s. Please post Java code within CODE tags.
• 03-17-2012, 03:56 AM
noobplus
Re: Basic inheritence question
Quote:

Originally Posted by ozzyman
calls constructor 2 with '0' as the parameter
'super' refers to the parent class and 'super()' refers to the parent class constructor,

thx I was havin doubts in those cases, and got clarified

thx
dhilip
• 03-17-2012, 04:11 AM
jubbiejub
Re: Basic inheritence question
Basically, I have no idea how to actually use inheritance to integrate the functions. I'm not even sure if my base class is correct at the moment.
• 03-17-2012, 04:31 AM
ozzyman
Re: Basic inheritence question
To integrate the functions... It's probably a lot simpler than you think, and that's why you're confused.

Consider this Animal class:

Code:

```class Animal {     boolean gender; //male=true     int numLegs = 4; //4 legs by default     int numEyes = 2; //2 eyes by default     Animal (boolean isMale) {         gender = isMale;     }     public void eat() { System.out.println("Animals need to eat."); }     public void move() { System.out.println("Animals need to move."); }     //methods to obtain properties of Animal     public boolean isMale() { return gender; }     public int getNumLegs() { return numLegs; }     public int getNumEyes() { return numEyes; }     }```
It would be a pain the *** if you had to write out all those fields and methods for every animal you created (e.g. Lion, Tiger, etc).

Because they are properties and methods shared by all Animals, it is better to put them in the Animal class.

Any class that extends animal, will inherit those methods.

Now we want a Lion class. All we have to do is add the extra properties and methods that only Lions and its subclasses have, e.g. a Mane.
Code:

```class Lion extends Animal {         boolean hasMane;         Lion (boolean withMane) {             super(withMane); //only male lions have a Mane             hasMane = withMane;         }         public boolean hasMane() { return hasMane; } }```
Now if I were to instantiate a Lion, it would have all the methods and properties of Animal as well as Lion:
Code:

```Lion simba = new Lion(true); simba.eat(); //output: "Animals need to eat."; simba.move(); //output: "Animals need to move.";```
You can also hide an inherited method, if it's public, like this:
Code:

```class Lion extends Animal {     //as above     ...     @Override     public void move() {         System.out.println("Lions need to move.");     } }```
Then try this:
Code:

`simba.move(); //output: "Lions need to move";`
• 03-17-2012, 05:11 AM
jubbiejub
Re: Basic inheritence question
Could you possibly give me an example of what it would look like with my code? I'm still sort of confused about how to use my rk method in my extended class.
• 03-17-2012, 05:23 AM
ozzyman
Re: Basic inheritence question
So if you create an object RK4one, it will have all the methods of RK4 as well as the added method

Code:

```RK4one example = new RK4one(); example.f(1.5, 2.5); //refers to the method in RK4one (since it overrides the method 'f(double, double)' in RK4) example.rk(1.0, 2.0, 1.0); //refers to method in RK4 (not the one in RK4one because they have different method signatures) example.rk(); //refers to method in RK4one```
That's something I forgot to mention, if you want to Override an inherited method, your method signature has to be the same in the subclass.
• 03-17-2012, 05:28 AM
jubbiejub
Re: Basic inheritence question
Ok that makes a lot more sense now. For some reason I wasn't getting the fact that to use the methods I had to actually initialize a new RK4 object. I think I'll be able to do what the assignment wants me to now.

Except for the fact I think my code doesn't do exactly what it should. It is supposed to integrate different functions on different intervals, which I could easily do when I was creating a separate class for each function. However now I think it's all screwed up since I have the same rk method for each interval. Any opinions on that?
• 03-17-2012, 05:35 AM
ozzyman
Re: Basic inheritence question
I'm sorry I didn't understand any of that lol...

Why doesn't it do what it should?