Welcome to Dream.In.Code
Become a Java Expert!

Join 150,221 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,164 people online right now. Registration is fast and FREE... Join Now!




Help with inheritance

2 Pages V  1 2 >  
Reply to this topicStart new topic

Help with inheritance

sp_and5
20 Nov, 2007 - 12:12 PM
Post #1

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 3


My Contributions
Hi ,

I need some help with two classes im trying to inherit. Ive got class Person which is the base class and class student which needs to be inherited from person. Im at early stages on the student class but im having problems with super().

Heres my classes :

CODE
public class Person
{
   protected String name;
   protected String gender;
   protected Date dateOfBirth;
   protected String address;
   protected String natInsceNo;
   protected String phoneNo;
   protected int indexNumber;
   protected static int counter;

    public Person(Date dateOfBirth, String nme, String addr,String ins)
    {
        name = nme;
        address = addr;
        natInsceNo = ins;
        counter ++;
    }
  
  
    public Person(String nme, char sex, Date dob)
    {
     name = nme;
     setGender(sex);
     dateOfBirth = new Date(dob);
     counter ++;
    }
    
    
     public void setGender(char sex)
            {
                    if (sex == 'm' || sex == 'M')
                                     gender="male";
                    else if (sex == 'f' || sex == 'F')
                                     gender="female";
                    else gender = "unknown";
            }
            
            
    public void setName (String nme)
    {
    
        name = nme;
    
    }
    
    public void setAddress (String addr)
    {
        address = addr;
    }
    
    public void setNatInsecNo (String ins)
    {  
        natInsceNo = ins;
    }
    
    public void setPhone (String phone)
    {
        phoneNo = phone;
    }
    
    public boolean equals(Person other)
    {
     return name == other.name && dateOfBirth == other.dateOfBirth && natInsceNo == other.natInsceNo;
    }
    
    
    public static int count()
    {
        return counter;
    
    }
}


and student ...


CODE
public class Student extends Person
{
   protected Date dateEnrolled;
   protected String course;
   protected int year;
  
  
    public Student(Date dateEnrolled, String crs, int yr)
    {
     super();
     course = crs;
     year = yr;
     dateEnrolled = new Date (enrolled);
        
   }
    
    public Student (String nme,char sex, Date dob,String insceNumber,Date enrolled )
    {
        super(nme);
        super(sex);
        super(dob);
        super(insceNumber);
        dateEnrolled = enrolled;
  
    }
    
    public void setCourse(String s)
    {
        
        Course = s;
        
    }
  
    public void setYear(int yr)
    {
        year = yr;
    }
    
    
}


now super doesnt seem to work as an error comes up "cannot find symbol - constructor Person()"

what im i doing wrong?

Thank you in advance!


User is offlineProfile CardPM
+Quote Post

modsiw
RE: Help With Inheritance
20 Nov, 2007 - 09:57 PM
Post #2

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 1


My Contributions
You need to create and empty constructor for Person. That is, a constructor that does not accept any arguments.

OR

You need to pass arguments to super() that match the signature of an existing constructor of Person.
User is offlineProfile CardPM
+Quote Post

sp_and5
RE: Help With Inheritance
21 Nov, 2007 - 04:47 PM
Post #3

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 3


My Contributions
Thank you very much i worked my way through it!
User is offlineProfile CardPM
+Quote Post

sp_and5
RE: Help With Inheritance
21 Nov, 2007 - 04:56 PM
Post #4

New D.I.C Head
*

Joined: 20 Nov, 2007
Posts: 3


My Contributions
Hi,
I got three classes with toString method Person, Student and PartTime

PartTime is inherited from Student and Student is inherited from Person which is the base class.

Now im trying to inherit the toString methods in Student from Person and in PartTime from Student.

Heres my classes

CODE
public class Person
{
   protected String name;
   protected String gender;
   protected Date dateOfBirth;
   protected String address;
   protected String natInsceNo;
   protected String phoneNo;
   protected int indexNumber;
   protected static int counter;

    public Person()//Date dateOfBirth, String nme, String addr,String ins)
    {
        this.name = name;
        this.address = address;
        this.natInsceNo = natInsceNo;
        counter ++;
    }
  
  
    public Person(String nme, char sex, Date dob, String ins)
    {
     name = nme;
     setGender(sex);
     dateOfBirth = new Date(dob);
     natInsceNo=ins;
     //natInsceNo=ins;
     counter ++;
    }
    
    
     public void setGender(char sex)
            {
                    if (sex == 'm' || sex == 'M')
                                     gender="male";
                    else if (sex == 'f' || sex == 'F')
                                     gender="female";
                    else gender = "unknown";
            }
            
            
    public void setName (String nme)
    {
    
        name = nme;
    
    }
    
    public void setAddress (String addr)
    {
        address = addr;
    }
    
    public void setNatInsecNo (String ins)
    {  
        natInsceNo = ins;
    }
    
    public void setPhone (String phone)
    {
        phoneNo = phone;
    }
    
    public boolean equals(Person other)
    {
     return name == other.name && dateOfBirth == other.dateOfBirth && natInsceNo == other.natInsceNo;
    }
    
    
    public static int count()
    {
        return counter;
    
    }
    
    public String toString()
    {
      return "Name :" + name + "Gender :" + gender + "Date Of Birth : "
      + dateOfBirth + "Address :" + address +
      "National Insurance No : " + natInsceNo + "Phone No : " + phoneNo;  
    }
}




CODE
public class Student extends Person
{
   protected Date dateEnrolled;
   protected String course;
   protected int year;
  
    /**
     * Constructor for objects of class Student
     */
      public Student()//Date enrolled, String crs, int yr)
      {
      super();
      this.course = course;
      this.year = year;
      this.dateEnrolled = dateEnrolled;
        
      }
    
    public Student (String nme, char sex, Date dob,String ins, Date enrolled )  
    {
        super(nme,sex,dob,ins);
        dateEnrolled = new Date (enrolled);
        
  
    }
    
    public void setCourse(String s)
    {
        
        course = s;
        
    }
  
    public void setYear(int yr)
    {
        year = yr;
    }
    
    public String toString()
    {
        
    }
    
}


and PartTime..

CODE
public class PartTime extends Student
{
    protected String employer;
    protected String workAddress;
    protected String workPhoneNo;
    
    
    public PartTime()
    {
     super();
     this.employer = employer;
     this.workAddress = workAddress;
     this.workPhoneNo = workPhoneNo;
    
    }

    public PartTime(String nme, char sex, Date dob, String ins, Date enrolled)
  
    {
      super(nme, sex, dob, ins, enrolled);
      this.dateOfBirth = dateOfBirth;
      this.dateEnrolled = dateEnrolled;
    }
    
    public void setEmployer(String emp)
    {
        employer = emp;
    }
    
    public void setWorkAddress (String wAddr)
    {
        workAddress = wAddr;
        
    }

    public void setWorkPhone (String wPhone)
    {
        workPhoneNo = wPhone;
        
    }
    public String toString
    {
        
    }
}



Do i need an extra line of code in toString method to inherit the other methods drom the other classes or just define the variables in the method ex return "Name" + name etc......
?


Thank you in advance!!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help With Inheritance
21 Nov, 2007 - 05:15 PM
Post #5

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Merged topics. Please do not create duplicate topics.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Help With Inheritance
21 Nov, 2007 - 05:34 PM
Post #6

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
if the classes are inheriting properly they can access public and protected variables directly.
User is offlineProfile CardPM
+Quote Post

Mayan
RE: Help With Inheritance
1 Jan, 2008 - 06:50 PM
Post #7

New D.I.C Head
*

Joined: 1 Jan, 2008
Posts: 6

Does anyone know how to create constructor for those three classes.

User is offlineProfile CardPM
+Quote Post

Mayan
RE: Help With Inheritance
3 Jan, 2008 - 03:41 AM
Post #8

New D.I.C Head
*

Joined: 1 Jan, 2008
Posts: 6

how to create constructor for those three classes(Person, Student and PartTime) pls i need help.....
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Help With Inheritance
3 Jan, 2008 - 09:35 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,285



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Here are the problems:

CODE

// Person constructors...
public Person(Date dateOfBirth, String nme, String addr,String ins);
public Person(String nme, char sex, Date dob);


//Student extends Person

public Student(Date dateEnrolled, String crs, int yr){
    super(); // constructor does not exist in Person.
    //...
}

public Student (String nme,char sex, Date dob,String insceNumber,Date enrolled ) {
    super(nme); // constructor does not exist in Person.
    super(sex); // constructor does not exist in Person.
    // never mind, only on super call per constructor
    //
}

// PartTime extends Student
public PartTime() {
    super();  // constructor does not exist in Student.
    //...
}

public PartTime(String nme, char sex, Date dob, String ins, Date enrolled) {
    super(nme, sex, dob, ins, enrolled); // this one should actually work!
    //...
}


I think what's being missed here is the intent of the parent contructor. You want all that data!

What you're constructors say is, basically, is for Person the two most important bits of info are (String name, Date dateOfBirth). So, we'll keep those.

Here are some options:
CODE

// Person constructors...
public Person(String name, Date dateOfBirth) {
    this.name = name;
    this.dateOfBirth = dateOfBirth;
    counter++;
}
public Person(String name, Date dateOfBirth, char gender) {
    this(name, dateOfBirth);
    this.gender = gender;
}
public Person(String name, Date dateOfBirth, String address, String natInsceNo) {
    this(name, dateOfBirth);
    this.address = address;
    this.natInsceNo = natInsceNo;
}

public Student(String name, Date dateOfBirth, Date dateEnrolled, String course, int year){
    super(name, dateOfBirth);
    this.dateEnrolled = dateEnrolled;
    this.course = course;
    this.year = year;
}

public Student (String name, Date dateOfBirth, String natInsceNo, Date dateEnrolled, String course, int year) {
    this(name, dateOfBirth, dateEnrolled course, year);
    this.natInsceNo = natInsceNo;
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

Mayan
RE: Help With Inheritance
4 Jan, 2008 - 07:33 AM
Post #10

New D.I.C Head
*

Joined: 1 Jan, 2008
Posts: 6

Thanks alot.....


User is offlineProfile CardPM
+Quote Post

Mayan
RE: Help With Inheritance
10 Jan, 2008 - 08:16 AM
Post #11

New D.I.C Head
*

Joined: 1 Jan, 2008
Posts: 6

how to create controllers for those three classes(Person, Student and PartTime.

Thanks in advance
User is offlineProfile CardPM
+Quote Post

danspub
RE: Help With Inheritance
30 Jan, 2008 - 04:04 PM
Post #12

New D.I.C Head
*

Joined: 30 Jan, 2008
Posts: 2

Hi, I just tried running the program by using run java application in the tools toolbar, but I get the error "Exception in thread "main" java.lang.NoSuchMethodError: main." I'm new to java and trying to learn it before I take a course. How do you get the program to run? Can anyone help?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:43AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month