Win a copy of Terraform in Action this week in the Cloud 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic