• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anybody.......
 
Ben Buchli
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


------------------
 
Elvis Ve
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic