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

Thread: extend a simple class Contacts to have a new parameter called favorites

  1. #1
    Junior Member
    Join Date
    Aug 2023
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default extend a simple class Contacts to have a new parameter called favorites

    Hi, I am a very beginner in Java. I have a simple class called contacts which is a class in a simple phonebook application. I would like to extend a class method to include an extra parameter in another class favorites. My attempt with my contact class below is giving errors. Can please someone help ?

    ************************************************** *****************************
    /** *
     * 
     */
    // Contacts Class
     
     
     
    public class Contacts {
     
        public String name;
        public String phoneNumber;
        public String nickname;
     
        public Contacts(String name, String phoneNumber) {
            this.name = name;
            this.phoneNumber = phoneNumber;
        }
     
         class Favorites extends Contacts 
          (String name, String phoneNumber,String nickname) {
            name = name;
            phoneNumber = phoneNumber;
            nickname = nickname;
         //
        }
     
        public String getName() {
            return name;
        }
     
        public String getPhoneNumber() {
            return phoneNumber;
        }
        public String getnickName() {
            return nickname;
        }
    }
    Last edited by Norm; August 28th, 2023 at 07:14 AM. Reason: Added / in ending code tag

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

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    giving errors
    Please copy the full text of the compiler's error message and paste it here.

    See ending tag for proper coding:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2023
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    Sorry, Im using BlueJ. How do I get the error messages?

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

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    Sorry, I do not use BlueJ.
    To use the compiler From the command line, Enter: javac Contacts.java
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2023
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    Please bear with me, I'm a very beginner. Here are the error I get

    class Favorites extends Contacts ----> I get - { expected

    name = name; here I get identifier expected
    phoneNumber = phoneNumber;
    nickname = nickname;


    thank you so much.

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

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    { expected
    The body of a class needs to be enclosed in {}s. Look at the Contacts class. It starts with:
    public class Contacts {
    and ends with a }

    When that is fixed, the other errors will change because the compiler got lost when it did not find the { for the class's body.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2023
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    I fixed the } issue but I still get errors now on the
    class favourites which are basically the same as previous. Can you help please?

    *********************************************

     
    public class Contacts {
     
        public String name;
        public String phoneNumber;
        public String nickname;
     
        public Contacts(String name, String phoneNumber) {
            this.name = name;
            this.phoneNumber = phoneNumber;
       }
     
        public String getName() {
            return name;
        }
     
        public String getPhoneNumber() {
            return phoneNumber;
        }
        public String getnickName() {
            return nickname;
        }
     
          public class Favorites extends Contacts 
          (String name, String phoneNumber,String nickname) {
            name = name;
            phoneNumber = phoneNumber;
            nickname = nickname;
     
        }
    }
    Last edited by Norm; August 28th, 2023 at 02:40 PM. Reason: Added missing / in ending code tag

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

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    The { goes after the class name. See the code for declaring the Contacts class:
    public class Contacts {
    What is the purpose of this line:
    (String name, String phoneNumber,String nickname)
    It looks like code for a constructor's or method's parameters.
    Again look at the code in Contacts:
    public Contacts(String name, String phoneNumber) {
    See how the name of the class is before (...)

    Did you write the code for the Contacts class or was it given to you to modify?

    Please look at this: https://docs.oracle.com/javase/tutor...classdecl.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2023
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    it was given to me to modify.

    I need to use it and show how I can use inheritance with the extends

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

    Default Re: extend a simple class Contacts to have a new parameter called favorites

    Copy this link so you can use it to find java topics:
    http://docs.oracle.com/javase/tutori...ybigindex.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Get static method to know which class it was called on
    By paalg in forum Object Oriented Programming
    Replies: 4
    Last Post: October 10th, 2019, 07:58 AM
  2. Replies: 6
    Last Post: February 9th, 2019, 12:48 PM
  3. Replies: 10
    Last Post: October 20th, 2013, 06:35 AM
  4. Class loader wont let me extend classes...?
    By sci4me in forum Java Theory & Questions
    Replies: 9
    Last Post: May 19th, 2013, 12:47 PM
  5. [SOLVED] Class throws FileNotFoundException when called from another class
    By Aurevir in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 2nd, 2012, 08:42 AM