nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   Java Help (http://www.nvnews.net/vbulletin/showthread.php?t=90036)

jcrox 04-19-07 06:51 AM

Java Help
 
This is real simple intro to java stuff but hell If I can figure it out.
I can do this:

SuperClass
int ivar = 8

Sub Class
print ivar to console

TestDrive
instantiate sub class and run display method = 8

Here's where I have a problem if anyone can help:
SuperClass
int ivar

getIvar()

setIvar()
Sub Class
print ivar to console

TestDrive
instantiate sub class and run display method = 0

The program and the instructions are more complex than what's above, but the simple gist of it is that I can't get the sub class to inherit the value of ivar when using getter and setter methods :(

jcrox 04-19-07 12:06 PM

Re: Java Help
 
OK, so maybe I need to be more specific....

class A {
int ivar;

int getIvar() {
return ivar; }

void setIvar(int i) {
ivar = i; }
}
class B extends class A {
int ivar2;

int getIvar2() {
return ivar2; }

void setIvar2(int i2) {
ivar2 = i2;}

void d1() {
System.out.println(ivar + ", " + ivar2);
}
}

public class TestDrive {
public static void main(String[] args) {
A a = new A;
B b = new B;

a.setIvar(25);
b.setIvar2(35);

b.d1();
}
}

This is still a simplified version but again the display in class b puts out ivar as 0 :(

rhink 04-19-07 10:36 PM

Re: Java Help
 
That's because ivar is 0 in B. new A() and new B() create two different objects.... ivar is only being set in A.

Try:

B b = new B();
b.setIvar(25);
b.setIvar2(35);
b.d1();

jcrox 04-19-07 11:04 PM

Re: Java Help
 
So then I take it I can't set the instance variable for class A from the test drive class and have class B inherit that value.....the value would have to actually be declared in class A.....that's how I originally did it but the instructions for the project are rather ambiguous and resemble something a student would come up with rather than an instructor grrrrr....Dumb broad spends 4 weeks going over variables, another 4 weeks going over arrays and then gives us a "We've falling a bit behind" speech so we have 1 week to learn polymorphism, inheritance, interfaces, abstract classes and abstract methods.....and to think...I'm paying for this.

rhink 04-19-07 11:15 PM

Re: Java Help
 
heh that sucks, arrays are the easy stuff.

I don't think you're quite understanding inheritance. In your example "a" is its own unique variable or object. It points to a different spot in memory than "b". There's no way for "b" to know what's going on in "a".

Inheritance means that your class "B" 'inherits' all methods and attributes of class "A". So when you do a "new B()", you create a new object that contains all of the variables and methods in class "A", plus whatever else you've defined.

I'll go through your example step by step to show you what I mean...
Code:

A a = new A();
"a" now contains variable ivar, initialized to 0.

Code:

B b = new B();
"b" now has its own copy of ivar, and ivar2, both initialized to 0

Code:

a.setIvar(25);
Now "a.ivar" is 25, "b.ivar" is 0, "b.ivar2" is 0.

Code:

b.setIvar2(35);
Now "a.ivar" is 25, "b.ivar" is 0, "b.ivar2" is 35.

Code:

b.d1();
You print out b's copy of ivar and ivar2- so you print "0, 35".

Now do you see why that doesn't work?

jcrox 04-20-07 09:10 AM

Re: Java Help
 
Yeah, I understand it thanks. It's just the project instructions make it sound like we are to use the set and get methods in A to assign a value to ivar and then print it in B strictly through inheritance.....this is the 4th class I've taken designed by this chick and it's the same old crap.

rhink 04-20-07 06:18 PM

Re: Java Help
 
Quote:

Originally Posted by jcrox
Yeah, I understand it thanks. It's just the project instructions make it sound like we are to use the set and get methods in A to assign a value to ivar and then print it in B strictly through inheritance.....this is the 4th class I've taken designed by this chick and it's the same old crap.

Yeah that's definitely not going to work, they're two different objects.

Though you *can* use a reference of type A to refer to B and use A's methods on it...

B b = new B();
A a = (A)b; //"a" is now refers to the same object as "b", but 'looks like' something of type "A"

Now you can use A's methods on a, and print it via b... but it's kind of pointless, at least in this example (it does have uses though).


All times are GMT -5. The time now is 07:50 PM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.