Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic