Trying to print out the attributes of the object Problem - Java | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Trying to print out the attributes of the object Problem - Java

Hi everyone. Currently, I have one super class called "Animal" which holds the classes "Dog" and "Dog breeds1" and "DogBreed2". I created individual objects within each Dogbreed class and I'm trying to figure out how to print out the attributes of of the objects from these differing classes. The error I get is "<identifer expected>". Any ideas? Still experimenting at the moment. public class Animal { //setting attributes that every Animal will have int legs; //how many legs the animal will have String colour; public class Dog{ //setting the Animal attributes to our dogs Dog.legs = 4; //each and every dog will have 4 legs public class DogBreed1{ //our individual dogs of dogbreed 1 Animal Buddy = new Animal(); Animal Chip = new Animal(); //Buddy will be Brown Animal.Dog.Buddy = "Brown"; } public class DogBreed2{ //our individual dogs of dogbreed 2 Animal Ralph = new Animal(); Animal Buster = new Animal(); } } } public class Pr{ public static void main(String [] args) { System.out.print(Animal.Dog.DogBreed1.Buddy.colour); //print out the colour of buddy System.out.print(Animal.Dog.DogBreed2.Buster.legs); //print out how many legs buster has } }

29th Jul 2017, 4:45 PM
YodaCoda
YodaCoda - avatar
5 Antworten
+ 3
With regards to your first question, the big difference is that you must create an instance of the inner class with the outer class. In your updated code, if it were done with inner classes; that would mean every attribute the kangaroos have, all animals have as well. Same goes for the dogs, the Animal class contains everything. This can be a huge waist since when you have a kangaroo, you don't care about the dog class. (Cause a kangaroo is not a dog!) From StackOverflow: "inner classes have the methods they want, whereas subclasses have the methods of their parent class. Subclasses can of course define additional methods, but they'll always have those of their parent." In this case, you want to have methods from the parent. Since the Animal class is basically a blueprint for all animals. So the inheritance approach makes more sense. It is also far easier to read the code (IMO). As you only need to look at each class individually rather than a class in a class. Separate classes can also be in separate files, which can make things more maintainable / easily changeable. I'm not sure if I fully understand your second question though. Do you want to modify the Attributes in the animal class, rather than the dog/kangaroo class? Anywho, I recommend you look into Constructors. They can be very helpful for initializing attributes. That way you can have 2 dogs with different attributes. For example, one dog with white colour, and one dog with brown colour.
30th Jul 2017, 4:06 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
I really recommend you just use Inheritiance. Not only does it help with maintainability, but this sort of breaks the concept of OOP. IE: Not all animals are dogs that have 2 breads, but according to your classes they are. "Dogs.legs = 4" This is an issue for 3 reasons. 1) legs isn't in the dog class 2) legs isn't static 3) Since this isn't a declaration (it's a statement) it needs to be inside a block (IE: In a method). Don't make it static though, instead have a constructor initialize it. "Animal.Dog.Buddy = "Brown";" Buddy is again, not in the dog class. In a method you can just type Buddy.colour = "Brown"; Like this: { Animal.legs = 4; // Only works if legs are static // Again don't make it static, this is just an example of what is required for you to call it like that } { Buddy.colour = "Brown"; } But even on these fixes your printing no longer works. So, instead of how you've set up your code: Create the objects in the MAIN method. Do not create them in the classes themselves. Not all dog breads have 2 Animals.
29th Jul 2017, 6:10 PM
Rrestoring faith
Rrestoring faith - avatar
0
Hi thank you for your response and explanation. Essentially, the reason why I originally created the animal class was because in the future I intended to create many different types of animals that I wanted to inherit from that class. Moreover, I did completely change the approach based on your advice and this time I created one base class called 'animal' and to child classes (Dog and Kangaroo) which I wish to inherit from the base class animal. I had a much easier time creating the objects and such and I definitely understand that it makes the code clearer and easier to change through this approach. I just have two question. My first one is what is the main difference/benefit of using the approach of "class1 extends class2" as opposed to the simply having nested public classes? That's what confused me. Here is what I have so far, I hope I'm on the right track.
30th Jul 2017, 3:22 PM
YodaCoda
YodaCoda - avatar
0
public class Animal { //attributes that all animals will have int colour; boolean wild; //is it a wild animal or not? int legs; } public class test { public static void main(String[] args){ //creating dogs Dog Buster = new Dog(); Buster.colour = "Black"; System.out.println("Buster: "); Buster.DogAttributes(); System.out.println(" "); //space System.out.println("Chip: "); Dog Chip = new Dog(); Chip.DogAttributes(); //creating KangaroosKangaroo Kangaroo Jack = new Kangaroo(); //? } } //make a subclass which inherits from Animal class Dog extends Animal { //assigning attributes that all dogs will have String colour = "Brown"; boolean wild = false; int legs = 4; //output the attributes of the dog void DogAttributes() { System.out.println("Wild: " + wild); System.out.println("Colour: " + colour); System.out.println("Legs: " + legs); } class Kangaroo extends Animal { //attributes that all kangaroos will have int legs = 2; boolean wild = true; String colour = "Beige"; //output the attributes of the kangaroo void KangarooAttributes() { System.out.println("Wild: " + wild); System.out.println("Colour: " + colour); System.out.println("Legs: " + legs); } } } The second question was why is that I cannot use my two child classes to inherit from the base class?
30th Jul 2017, 3:23 PM
YodaCoda
YodaCoda - avatar
0
Wow, thanks! I get it now, so subclasses and inner classes are a different thing. and in my code, it's better to use inheritance. I solved the issue though as it was a small indentation problem. My question was regarding modifying the attributes of individual objects created from a class. For example, if I set all dogs to be brown in my dogs class but only wanted one dog to be black instead while keeping the rest of its default attributes, whether I could do that by overriding it in the main method. I used something like this in the main method: Buster.colour = "Black"; //not sure if this is ok? So that individual object would no longer inherit the 'brown' colour attribute from its class. However, as you've mentioned, I will definitely look at constructors to improve my code. Thank you very much for your time and help. Much appreciated. I have learned much.
30th Jul 2017, 7:49 PM
YodaCoda
YodaCoda - avatar