+ 1

Making an object type from an object? Java

Hello fellow coders! I was wondering whether it is possible to make an object type from an object? For example, I have a class called 'Animal' and using that I want to make an object dog. However since there are many types of dogs, I want to be able to say something like DogBreed1, DogBreed2, DogBreed3. so that way I can set the attributes of each individual dogbreed from that set rather than affect all of my dogs. Sorry if this sounds confusing. I'm just a new experimental guy trying to understand. public class Animal { //color of Animal int age; String origin; String color; //let's make some animals from our Animal class Animal dog = new Animal(); Animal cat = new Animal(); Animal bear = new Animal(); /*let's make different breeds types from our dog object so that later we can apply changes to all of the objects of that breed rather than all of our dog objects./* dog dogbreed1 = new dog(); }

29th Jul 2017, 12:28 PM
YodaCoda
YodaCoda - avatar
5 Answers
+ 1
You can try to create some subclasses inside the class Animals. In that way you will have some common attributes from the superclass Animals but also some unique attributes in each subclass about every breed. Another way would be to create different classes which "extend" the class Animals. Again you will have some common attributes from class Animals but you can add any other attribute you want for every breed-class. Hope I helped!! Oh and keep experimenting...some of the greatest achievements have poped up that way!!!
29th Jul 2017, 1:53 PM
Maria204
Maria204 - avatar
+ 1
Yeah thank you for clarifying. I actually had a eureka moment when I realised that I should have nested classes instead since I want to be grouping.So this is the basic initial idea that I have public class Animal { public class Dog{ public class DogBreed1{ } public class DogBreed2{ } } }
29th Jul 2017, 3:00 PM
YodaCoda
YodaCoda - avatar
+ 1
May I ask, what do you think of the logic behind this? Does that technically make sense? I was wondering what would be the difference if we simply made the objects out of the Dog class instead or the Dog breed class instead. Because later on I would like to add more types of animals and animal breeds then alter these specific groups rather than the entire group itself. What do you think behind my logic, am I on the right track? public class Animal { public class Dog{ public class DogBreed1{ //our individual dogs Animal Buddy = new Animal(); Animal Chip = new Animal(); } public class DogBreed2{ //our individual dogs Animal Ralph = new Animal(); Animal Buster = new Animal(); } } }
29th Jul 2017, 3:17 PM
YodaCoda
YodaCoda - avatar
+ 1
To be honest with you I am not an expert with java, I merely have completed one or two school projects in which I had to deal with different classes. So in my opinion, for starters I think that in order to create an object from a subclass you will have to create an object of every superclass that it is nested into. So there is no difference because you will have to make the object dog and animal anyway. As far as your logic I believe that you are looking in the right direction and technically your code above seems to be very ok. If it is ok I would like to suggest something. Instead of creating one class animals and fill it with subclasses and inner subclasses, I think it will work better as a whole if for each animal you create a different class and only then create subclasses of every breed. In that case when you make an object of any breed of any animal in your main method, the program won't have to read the whole class animal and execute only one line of code of a subclass in a subclass. I hope I explained that in an understandable way! In my personal experience it is better for the whole program to create more classes and less subclasses so it will be easier to execute and at the same time it will be more flexible and manageable for you to handle as you make the development.
29th Jul 2017, 3:42 PM
Maria204
Maria204 - avatar
+ 1
Oh thank you very much
29th Jul 2017, 4:12 PM
YodaCoda
YodaCoda - avatar