• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Inheritance - Creating object with super class and subclass

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

There is a animal class



We can create objects like this.




My question is, why should we create new Dog() with Animal reference? and what is the use?
In what conditions i should create



and in what conditions i should use



Please kindly explain.
Thanks
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mahesh Kumar!

Think about a program that just deals with what animals eat, not the specific animal.  You might have a method like this:
How would you be able to do this without an interface or a supertype?
 
Bartender
Posts: 1205
22
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, sometimes you don't know ahead of time what kind of Animal you need.

 
Mahesh Kumar ...
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I understand now.
 
Marshal
Posts: 79978
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan McGuire wrote:. . .

...but, if there is any possibility of its being a cat, it cannot show love.

Because the showLove() method isn't declared in Animal, it cannot be invoked on the pet, not even if it is a dog.
 
Mahesh Kumar ...
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



The animal class has some default methods for all the animals.
But Dog class has one specific method "showLove"


Now



Is not working. Animal class does not know anything about Dog class.
How do we handle this situation?

 
Campbell Ritchie
Marshal
Posts: 79978
397
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only say, “default method,” when referring to interfaces. You have concrete methods in the Animal class (which class should probably be declared abstract) and you are overriding them in the subclasses. Whenever you are overriding methods, tag them with @Override. Look at the three Java® Language Specification (=JLS) links in that page, and this Java™ Tutorials page for more information..
You can access showLove() like this:-Not only is that code awkward to read, but also I think any use of instanceof is a sign of bad design in your inheritance.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to expand further on the response of the first question, I suggest you take a look at https://en.wikipedia.org/wiki/Liskov_substitution_principle
 
grapes are vegan food pellets. Eat this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic