Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 04-19-07, 06:51 AM   #1
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default 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
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 04-19-07, 12:06 PM   #2
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default 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
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 04-19-07, 10:36 PM   #3
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default 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();
rhink is offline   Reply With Quote
Old 04-19-07, 11:04 PM   #4
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default 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.
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 04-19-07, 11:15 PM   #5
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default 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?
rhink is offline   Reply With Quote
Old 04-20-07, 09:10 AM   #6
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default 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.
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 04-20-07, 06:18 PM   #7
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default 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).
rhink is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google (partially) loses suit to Oracle over use of Java API's ViN86 Mobile Devices And Smartphones 3 05-17-12 10:25 AM
Judge Declines To Rule That Google Pillaged Java News Archived News Items 0 05-10-12 07:00 AM
BAH! I'm being driven insane by Java Virtual Machine Kruno General Software 9 09-01-02 09:59 PM
My Windows XP Internet Explorer Blue Jay Microsoft Windows XP And Vista 8 08-11-02 10:20 PM

All times are GMT -5. The time now is 11:10 AM.


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