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!