0

what's the difference? a) Animal dog = new Dog(); b) Dog dog = new Dog();

28th Apr 2016, 5:18 PM
Vlad Turchenko
Vlad Turchenko - avatar
6 Answers
+ 4
The difference is that unlike object dog from Animal, object dog from Dog will be able to use the existing members from the Class Dog and Animal at the same time. The dog from the class Animal will only be able to use members from Animal since its the base class, any additions to the class Dog will be ignored by the dog created from the class Animal.
30th Apr 2016, 1:42 AM
Pablo Hernandez
+ 3
Edit: I now know why my comment is receiving down votes. Animal dog = new Dog(); Dog dog = new Dog(); They would both create a Dog object, which derives from the Animal class. But the object instantiated from Animal wouldn't be able to use the methods etc defined within the Dog class, only only those within the Animal class. Pablo is right, and I'm still learning :P
17th Jun 2016, 9:56 PM
Malachi Jones
Malachi Jones - avatar
+ 1
Hi! I'm just wondering, shouldn't ... Animal dog = new Animal(); and not Animal dog = new Dog(); ?
11th Jul 2016, 12:54 PM
Bruce William Opio
Bruce William Opio - avatar
0
what you are doing here is also termed as casting. In the a) step.
24th Jun 2016, 7:29 AM
Saud Ahmad
Saud Ahmad - avatar
0
The difference is the public interface used to access the Dog's properties, methods and behaviour. In the 'Animal dog = new Dog ()' example we are only intetested in treating the Dog as an Animal... meaning we are only interested in what Animal properties and behaviour the Dog has [ Eat(), .Drink(), .Legs] ... not those specific to a Dog derived class [.Bark(), .Fetch()]. đŸ€”
2nd Dec 2016, 3:06 AM
Green Square Dermatology
Green Square Dermatology - avatar
- 4
There is no difference. They both create a Dog object. syntax a) allows for easier understanding of the code; that Dog is a derived class of Animal. You could even write: var dog = new Dog();
15th Jun 2016, 12:51 AM
Malachi Jones
Malachi Jones - avatar