Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with inheritance

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help with inheritance

    Hey guys. I am practicing with inheritance and was trying a simple exercise. It consists of writing a super class Person with subclass Student. The person has a name and year of birth, while the student has a major he is studying. When I write the Student class I get an error in the constructor and don't know what it is. It is probably very simple. Could anyone help?

    package Problem11Persons;
     
    public class Person
    {
    	private String name;
    	private int year;
     
    	public Person(String aName, int aYear)
    	{
    		name = aName;
    		year = aYear;
    	}
     
    	public String toString()
    	{
    		//"Person is:" + name + "Date of birth is:" + birth;
    		return "Person is:" + name + "Date of birth is:" + year;
    	}
     
    }

    package Problem11Persons;
     
    class Student extends Person
    {
    	private String major;
     
    	public Student(String aMajor) // error here
    	{
    		major = aMajor;
    	}
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help with inheritance

    A subclass's constructor will *always* call the super class's constructor either explicitly where you write a call to super(...) at the start of the child's constructor, or implicitly. If it is done implicitly, it will call the super class's default or parameterless constructor. Your Student class's constructor doesn't call the super's constructor, and fails when it tries to implicitly call the default parameterless constructor since it doesn't exist.

    I sugges that you pass *three* parameters into Student, a String for name, an int for year, and a String for major, and that you on the first line of this constructor explicitly call the super's constructor passing in the two required parameters.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,296
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Help with inheritance

    The error says tons about what's going on, but you did not post it. You might benefit from posting it, as this will not only remove the necessity of relying on unpaid volunteers to copy, paste, and compile your code as well as give you some time to contemplate why the error says something like constructor Person() does not exist - which should be pretty informative in and of itself.

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with inheritance

    Person class --> When we write a constructor explicitly(in this case name,year) we are telling the users that if they want to create a Person object then name and year is mandatory.
    Now another class Student extends Person....here we are adding the attribute major. As Student inherits attributes from Person it makes sense to say that if anyone wants to create a Student object then name,year and major is mandatory.
    So with view to inheritance, it does not makes sense to say that the base class Person should be constructed with name and year but the subclass Student should be constructed with major.

Similar Threads

  1. Triple Inheritance
    By Samaras in forum Object Oriented Programming
    Replies: 8
    Last Post: August 26th, 2012, 03:11 AM
  2. hi need help with inheritance
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 2
    Last Post: July 19th, 2012, 04:01 PM
  3. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  4. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM