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: I have a problem with class inheritance.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I have a problem with class inheritance.

    Hi Everybody! I'm very new to Java. I started learning Java about 5 months ago in my high school computer science class, so I'm just a beginner. Earlier in class we were learning about inheritance and the keyword "extends". For example: public class subClass extends superClass. I'm very new to programming, especially object oriented programming, so I have no idea what's wrong with my code.
    I have a superclass called Animal and two subclasses, Dog and Ant, that both extend Animal. Here's my code and then I'll explain my problem:

    Animal superclass:

    public class Animal
    {
    	public Animal(){}
     
       public void isAnAnimal(){
        	System.out.println("This is an animal.");}
     
       public void has4Legs(){
        	if(getNumberOfLegs() == 4)
        		System.out.println("The animal has 4 legs.");
        	else
        		System.out.println("The animal does not have 4 legs.");}
     
       public int getNumberOfLegs(){
       	return Animal.numberOfLegs;}
     
       public static int numberOfLegs;
    }


    Dog subclass:

    public class Dog extends Animal
    {
        public Dog(String b, String n){
        	breed = b;
        	name = n;
        	Dog.numberOfLegs = 4;}
     
        public int getNumberOfLegs(){
       	return Dog.numberOfLegs;}
     
      	public String breed;
      	public String name;
      	public static int numberOfLegs;
    }


    Ant subclass:

    public class Ant extends Animal
    {
       public Ant(){
       	Ant.numberOfLegs = 6;}
     
       public int getNumberOfLegs(){
       	return Ant.numberOfLegs;}
     
    	public static int numberOfLegs;
    }


    Main:

    public static void main(String args[])
    {
    	Dog Onyx = new Dog("Poodle","Onyx");
    	Dog Fido = new Dog("Dalmation","Fido");
    	System.out.println("Onyx:");
    	System.out.println("Onyx.numberOfLegs: " + Onyx.numberOfLegs);
    	Onyx.has4Legs();
    	System.out.println("Fido:");
    	System.out.println("Fido.numberOfLegs: " + Fido.numberOfLegs);
    	Fido.has4Legs();
    	Fido.numberOfLegs = 3;
    	System.out.println("Fido.numberOfLegs: " + Fido.numberOfLegs);
    	Fido.has4Legs();
     
    	Ant Jeff = new Ant();
    	System.out.println("Jeff:");
    	Jeff.isAnAnimal();
    	System.out.println("Jeff.numberOfLegs: " + Jeff.numberOfLegs);
    	Jeff.has4Legs();
    	System.out.println("Fido.numberOfLegs: " + Fido.numberOfLegs);
    	Fido.has4Legs();
     
    	System.out.println("Onyx.getNumberOfLegs(): " + Onyx.getNumberOfLegs()); // 3??????
    }



    Here's my output when I compile and run the program:

    Onyx:
    Onyx.numberOfLegs: 4 // These lines are red to show how the logic of the program doesn't make sense.
    The animal has 4 legs.
    Fido:
    Fido.numberOfLegs: 4
    The animal has 4 legs.
    Fido.numberOfLegs: 3
    The animal does not have 4 legs.
    Jeff:
    This is an animal
    Jess.numberOfLegs: 6
    The animal does not have 4 legs.
    Fido.numberOfLegs: 3
    The animal does not have 4 legs.
    Onyx.getNumberOfLegs(): 3 // These lines are red to show how the logic of the program doesn't make sense.
    Press any key to continue...


    Here's my problem:
    I noticed that this line of code in my main class ------> Fido.numberOfLegs =3; also changes the value of Onyx.numberOfLegs to 3. Is this because they're both a Dog object? How do I make it so that Onyx and Fido can have a different value of numberOfLegs? I'm pretty sure that if I made another Ant object called Billy and I used Jeff.numberOfLegs = 100; than the value of Billy.numberOfLegs would also be 100. I'm completely lost and have no idea what to do. This isn't for a school project or anything. I decided to do this on my own so I could better understand inheritance.

    Thanks for all the help! I really appreciate it!


    --- Update ---

    If you have any questions, please ask me. I'll try to explain my best!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a problem with class inheritance.

    This in Animal:

    public static int numberOfLegs;

    should not be static. Making it static means that all subclasses "share" the variable numberOfLegs. This is also called a "class variable," indicating that it belongs to THE CLASS Animal and not its instances. Removing 'static' makes the variable an "instance variable."

    You'll notice that numberOfLegs starts out as 4 for Onyx but changes last to 3 for Fido, so when the number of legs for Onyx is requested after Fido, 3 is given as the answer, because that was the last value set for Fido.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Za Voucha (February 1st, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with class inheritance.

    Thanks so much for taking the time to help! I'm not by my code right now to see if that works, but you seem right to me. I'll let you know how it goes.

  5. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: I have a problem with class inheritance.

    Hi Greg,

    So you are saying no need to create static classes in subclasses right?? i think SuperClass static variable is enough tat we can use in subclass. Is it right please explain. I studied about static variable i asked so many members but i didn't get clear idea. Please help me to get clarity on static usage.

  6. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: I have a problem with class inheritance.

    just want to suggest you that first try to be familir with small inheritance programs then try a large one.
    this is just a suggest .... if you have done all your small inheritance programs correctly and throughly the you would have not faced this problem..

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a problem with class inheritance.

    Quote Originally Posted by Ganeprog View Post
    So you are saying no need to create static classes in subclasses right?? i think SuperClass static variable is enough tat we can use in subclass. Is it right please explain. I studied about static variable i asked so many members but i didn't get clear idea. Please help me to get clarity on static usage.
    No, I didn't say that. I said that the specific variable, numberOfLegs in the class Animal should not be static. Static nested classes is an entirely different subject, related to this topic only by the word 'static.' If your question about that topic persists, please review this discussion and then start your own topic with a specific question about the topic of nested classes, or point me to one you already started.

  8. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with class inheritance.

    Quote Originally Posted by GregBrannon View Post
    This in Animal:

    public static int numberOfLegs;

    should not be static.
    Ok so right now I'm taking off the word static in public static int numberOfLegs; I just got this error "error: non-static variable numberOfLegs cannot be referenced from a static context
    return Animal.numberOfLegs;}"
    I tried removing the word static everywhere in all of my classes, but I got the error again... 3 times.

    --- Update ---

    Quote Originally Posted by Praful Chougale View Post
    just want to suggest you that first try to be familir with small inheritance programs then try a large one.
    this is just a suggest .... if you have done all your small inheritance programs correctly and throughly the you would have not faced this problem..
    Yea, you're absolutely right, but my Java textbook has a lot of examples of small inheritance programs (ex: only one superclass and one subclass). I wanted to try and see what would happen if I went ahead and extended multiple classes to the same superclass.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a problem with class inheritance.

    Instead of returning Animal.numberOfLegs, try:

    return numberOfLegs;

    As for removing the word 'static' everywhere, I'm all for it, but I hadn't analyzed what problems that may cause. Let us know if errors persist after trying the above suggestion.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    Za Voucha (February 1st, 2014)

  11. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Relaxed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with class inheritance.

    Thanks so much! That did it! I just had to turn all of the
    Animal.numberOfLegs
    Dog.numberOfLegs

    and Ant.numberOfLegs to just non-static numberOfLegs. Thanks!

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I have a problem with class inheritance.

    Perfect. Glad to help.

Similar Threads

  1. Inheritance - Object class
    By prasanna1157 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 29th, 2012, 05:43 PM
  2. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 14th, 2012, 07:29 PM
  3. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 12th, 2012, 08:10 PM
  4. Inheritance; Problem with Test class
    By Charlie.beat in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 8th, 2012, 10:59 PM
  5. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM

Tags for this Thread