aspose file tools*
The moose likes Beginning Java and the fly likes Need Help! How can I...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need Help! How can I......" Watch "Need Help! How can I......" New topic
Author

Need Help! How can I......

Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
I have created these two classes, but now I find out I cant use inheritance, how can i do this without inheritance, like how do i pass the Student class to the Undergraduate class?

class Student
{
String lastName,firstName;
double GPA;

Student(String lname, String fname)
{
lastName = lname;
firstName =fname;
}

Student(String lname, String fname,double gpa)
{
this(lname,fname);
GPA = gpa;
}

String getLname()
{
return lastName;
}

String getFname()
{
return firstName;
}

double getGpa()
{
return GPA;
}


}

class Undergraduate extends Student
{
Undergraduate(String lname, String fname)
{
super(lname,fname);
}

Undergraduate(String lname, String fname, double GPA)
{
super(lname, fname, GPA);
}

double getGPA()
{
return super.getGpa();
}

String getFName()
{
return super.getFname();
}

String getLname()
{
return super.getLname();
}
} :roll:
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
why cant you use inheritance?? is it a requirement for the assignment?
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
yeah, its a requirment we have to use composition i guess, he said to pass Student as an object in the constructor for Undergraduate but thats what I dont know how to do, can someone please help, thats all i need to finish the program. thanks
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
anybody.......
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
you simply write the constructor of the undergrad class so that it accepts a Student object. eg.
Undergraduate( Student student ){
s = student; //s is a data member of your Undergraduate class
}

now you can reference to Student by using
s.getLName() or whatever.

it doesnt make sense not to use inheritance here, though...
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6



------------------


GenRocket - Experts at Building Test Data
Elvis Ve
Greenhorn

Joined: May 17, 2004
Posts: 19
thanks a lot I understand better now, I know it dosent make sense not to use inheritance but I got one of those teachers that make u go back to class and object basics
 
wood burning stoves
 
subject: Need Help! How can I......