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 12 of 12

Thread: Inheritance constructor help?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Inheritance constructor help?

    Here is the question:

    The Person class has two attributes, name and age. We want to create a subclass of Person, called Oldie, that represents someone who is getting on in age, say about thirty years old or older. Note the additional boolean-valued attribute called perryComoFan in Oldie. If you are an Oldie, you are either a fan of Perry Como or you are not.


    For this exercise, write a complete constructor for the Oldie class. Your constructor should have three parameters: a String parameter called n, an int parameter called a, and a boolean parameter called p. Your code should initialize the name and age attributes to the value of n and a respectively. Your constructor must also assign the value of p to the perryComoFan attribute of the class.

    Here is my code that I tried but it says it's wrong! I even tried used super(n, a); instead of the this. statements:

    public class Oldie extends Person
     {
     private boolean perryComoFan;
     
    Public Oldie(String n, int a, boolean p) {
     this.name=n;
     this.age=a;
     perryComoFan=p;
     }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    it says it's wrong
    Please copy the full text of the error messages and paste it here.
    Also post the definition for the Person class.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance constructor help?

    Here is the error message:

    compilation error (line 2, column 42) cannot find symbol
    symbol : constructor Person()
    compilation error (line 3, column 7) name has private access in inheritanceI.Person
    compilation error (line 4, column 7) age has private access in inheritanceI.Person

    (line 1 refers to the blank line above Public Oldie(String n....
    line 2 refers to the line: Public Oldie(String n, int a...
    line 3 refers to this.name=n; and so on)

    Here is the Person class:
     
     public class Person
     {
      private String name;
      private int age;
     
      public Person(String n, int a)
      {
        this.name = n;
        this.age = a;
      }
     
      public String getName()
      {
        return name;
      }
     
      public int getAge()
      {
        return age;
      }
     
      public void setName(String n)
      {
        name = n;
      }
     
     public void setAge(int a)
     {
       age = a;
     }
     
      public void print( )
      {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
      }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    cannot find symbol
    symbol : constructor Person()
    The compiler can not find a constructor in the Person class that takes no args.

    name has private access
    The extending class can not access the base class's private variables.
    Use the super class's constructor to pass the values.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance constructor help?

    So I changed the code to :

    public Person(String n, int a, boolean p) {
     this.name=n;
     this.age=a;
     perryComoFan=p;
     }

    and it's saying:
    compilation error (line 2, column 8) invalid method declaration, return type required. Why is it saying this?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    public is all lowercase letters. The compiler could think Public is the name of a class that the Person() method is returning.

    You need to post the full text of the error messages. The short messages you post don't show all that needs to be shown.

    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance constructor help?

    That was my fault, I typed it here with a capital P but in my code it is lowercase.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    That makes it a lot harder if the posted code is not what is causing the error.
    Post it again and please post the full text of the error message See the post #6
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance constructor help?

    The compiler we use for our class is called OWL, it is a part of our online textbook created by UMass Amherst I believe. If I enter the correct code, it marks my question correct and I can move on. The error messages I have been posting are all the information that is given to me, so the compiler is pretty useless.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    You need to learn how to use the javac compiler. It gives good error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance constructor help?

    Well I figured it out. I had to call super to get the name and age from the super class. Then since boolean perryComoFan is private, I couldn't refer to it directly.

    Public Oldie(String n, int a, boolean p) {
     super(n, a);
     this.perryComoFan=p;
     }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inheritance constructor help?

    Glad you got it. Mark this thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with inheritance
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2012, 12:07 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. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. Inheritance
    By lewzax in forum Object Oriented Programming
    Replies: 4
    Last Post: July 8th, 2011, 01:51 PM
  5. inheritance
    By b109 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 30th, 2010, 09:23 PM

Tags for this Thread