• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

conceptual problem !!!!!!

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi !
can any one explain
there are 2 class
1. Base class
2. Child class
child class extends base class and in child class if we write
class Child extends Base
{
Child obj1 = new Child(); // no problem
Base obj2 = new Base(); // no problem
Base obj3 = new Child(); // please explain this in all possible situations
// i.e it's working when a method is called from
// base class or from child class
can we use instanceof operator here to check to which the obj belongs to ?
please reply
regards
sachin
delhi
------------------
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sachin,
You can always assign a subclass (Child) object to a superclass (Base) reference as a subclass inherits the public part of it's superclass ie it can do the same things.
However, you can't assign a superclass object to a subclass reference without a cast.
Child instanceof Base -> true
Base instanceof Child -> false
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Sachin,


Child obj1 = new Child(); // no problem
Base obj2 = new Base(); // no problem
Base obj3 = new Child(); // please explain this in all possible situations
// i.e it's working when a method is called from
// base class or from child class
can we use instanceof operator here to check to which the obj belongs to ?


Further to what Jane said,


Hope this clears your doubt..
Ravindra Mohan.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic