Resolved What is Animal in Animal cat = new Cat();

bondra

Well-known member
Joined
Oct 24, 2020
Messages
77
Programming Experience
Beginner
Simple question, but I'm currently commenting my code and started wondering. Is this correct?

C#:
// Creates a new instance of the object Cat, inherited from the class Animal
Animal cat = new Cat();
 
How do you know that Animal is a class and not an interface?
 
I realized the code might not be right regardless.
Cat cat = new Cat(); work just as fine. Don't know why I put Animal in there in first place. Thought it was the way to write when something was inheriting. Brainfart mixed with newbieness :)
 
Animal is one of the ancestor classes of Cat, assuming that Animal is even a class an not an interface. In general you use an ancestor class or interface when you don't care about the specific class that has been instantiated because all you want to do is use the methods/properties exposed by the ancestor or interface.

In general, it is actually a poor practice to comment every line of code. This is primarily because comments fall out of date, and because it adds cognitive load to to future readers of the code. You should only comment lines of code that are non-obvious or when you need to explain why something is being done. Usually the reason for explaining why something is being done is because you doing something against convention or something unexpected. (Imagine a poet or song writer explained every line of verse they wrote with the explanations between the lines of the poem/song. It completely disrupts the flow. The same is true when reading code. Experience readers expect a certain level of flow in the code.)
 
Thank you!

Indeed I agree with the comments! I remember pointing it out to my teacher that it was horrible to overview the code with too much comments.
But yeah I understand that those comments where needed as it was a beginners course. Now however it's a course with the main focus of OOP so commenting basic stuff can be left out, true!
 
Simple question, but I'm currently commenting my code and started wondering. Is this correct?

C#:
// Creates a new instance of the object Cat, inherited from the class Animal
Animal cat = new Cat();
There's no such thing as "an instance of an object". The object is the instance and it is an instance of a type. In this case, the type is a class.
 
I realized the code might not be right regardless.
Cat cat = new Cat(); work just as fine. Don't know why I put Animal in there in first place. Thought it was the way to write when something was inheriting. Brainfart mixed with newbieness :)
It really depends on what you want to do with that variable. If you want to treat it as a Cat, i.e. access those members that are specific to the Cat type, then you need to declare it as type Cat. If you intend to treat it as an Animal, i.e. only access those members common to all types derived from Animal, then declaring it as type Animal is OK and probably preferable, because it is an explicit indication that that is how it should be treated, e.g.
C#:
Animal cat = new Cat();
Animal dog = new Dog();
Animal parrot = new Parrot();

var pets = new[] {cat, dog, parrot};
 
Back
Top Bottom