Simple Java Objects

I have taken and given a lot of inteviews and worked with lot of developers. Here is one of the most common, simple but confusing question:

 

class A 
{ 
  public int addA()
  {
    //do something
  }
} 

class B extends A 
{ 
  public int addB()
  {
    //do something
  }
} 
 

1. A a = new B() is right?

or

2. B b = new A() right?

1 is right. B can be said to be specific implementation of A.

So an object of B will always have the characteristics of A but not vice versa.

 
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s