• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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: 7111
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: 1204
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: 73244
332
  • 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: 73244
332
  • 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: 157
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic