• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why cant you use inheritance?? is it a requirement for the assignment?
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anybody.......
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


------------------
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic