• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance in java  RSS feed

 
Sachin Pratap
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{}
class B extends A{}
class C extends A{}
class Test{
public static void main(String args[]){
A a = new A();
B b = new B();
C c = new C();

// assignment here
}
}



Given the above code, which one of the following assignment is legal?
1) c=a
2) a=b
3) b=c
4) b=a
5) a=b=c



 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Pratap,first let us know your specific doubts regarding the question.


Do add code tags
 
salvin francis
Bartender
Posts: 1663
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be the answer according to you Sachin Pratap ?

Remember, when you say X extends Y, it means that X "is a" Y, like Tiger extends Animal so Tiger is an animal so we can have Animal a = new Tiger(); but the reverse isn't possible.
Tiger t = new Animal(); //-- will throw a compile error




 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I won't say any more: Sachin and Salvin have said it all already.
 
salvin francis
Bartender
Posts: 1663
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to code ranch
 
punyashloka mohapatra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to my understanding with your question you want to check compatibility.Then from your options only x=y is possible.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid your answer does not give enough explanation. Nor is it helpful to give a straight answer. Look at the title page of this forum and you will read this:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Please don't be annoyed, but I have pulled rank and changed your answer.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sachin,

2. a = b is legal assignment.

The reason is - here, A is a parent class i.e superclass of B and C (Inheritance concept)
There is a relationship between A-B and A-C i.e the Parent-child relationship.

class B and C are siblings but there is no relationship between them so you can't assign B class reference = C class reference.
that's why b = c OR c = b are incompatible.

and now, always remember one thing, We can always assign a child class reference to parent class reference but not vice versa.
That's why c = a is not valid. It's imcompatible. Because we can't assign parent class reference to child class reference.

But we can assign a = b.
we can also assign a = c. (this is not given in your question. But it's also perfectly valid)
because a is what type? a is A type i.e a is parent class reference. so we can assign child class refences b and c to a.


 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid using parent and child for super and sub classes,because they have no resemblance to parent or child


The rule about which you are talking about:
Instance(object) of subclass can be assigned to reference variable of its super class (direct or indirect)
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Avoid using parent and child for super and sub classes,because they have no resemblance to parent or child


The rule about which you are talking about:
Instance(object) of subclass can be assigned to reference variable of its super class (direct or indirect)


LMAO
The very first thing, parent class for superclass and child class for subclass I've taken for better understanding. And by the way I've already used A is superclass of B and C.

and the Second thing, You gotta read the question carefully.
According to the question, references are assigned.
if it was
still it would be incompatible if you take b = a;. You need to keep this in mind.
what you saying about object of subclass can be assigned to reference variable of super class is right.
But when you assign subclass reference to superclass reference, then and then only superclass reference holds the subclass object (instance).
You gotta always look at the reference types not objects.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I know this is late into the topic, so would appreciate it if anyone can...

Please QuoteYourSources

Henry
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all ,we expect everyone to not to use abbreviations (LMAO,gotta)
Reason:we are here to simplify things,using such acronyms, abbreviation, slang (whatever they are)won't help our cause

Anything which you feel to be helpful won't be helpful until that is right
So you are not advised to use parent and child


A-superclass
B-subclass

A a=new B();

What will this do
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sankalp Bhagat wrote:LMAO
The very first thing, parent class for superclass and child class for subclass I've taken for better understanding. And by the way I've already used A is superclass of B and C.

Please remember that the first rule we have here on the Ranch is Be Nice - don't be rude.

Sachin is right. Inheritance in object oriented programming is not the same as inheritance in biology. In OO programming, inheritance means specialization, and there is an "IS A" relationship between the subclass and its superclass. That is not the case with biological inheritance. Having class Child extends Parent is strange, because that means you are saying: a Child is a (special kind of) Parent, which is incorrect.

So, using the words "parent" and "child" doesn't help to understand what inheritance means in object oriented programming; it confuses the biological meaning of inheritance with what it means in OO programming.
 
Sankalp Bhagat
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:First of all ,we expect everyone to not to use abbreviations (LMAO,gotta)
Reason:we are here to simplify things,using such acronyms, abbreviation, slang (whatever they are)won't help our cause

Anything which you feel to be helpful won't be helpful until that is right
So you are not advised to use parent and child


A-superclass
B-subclass

A a=new B();

What will this do


of course you can write

because A is superclass. so superclass reference can always hold subclass object.
If you can't see, I would rather better tell you once again.
In the question posted, references are assigned.

A a = new B();
B b = new B();

Okay, you tell me is this legal?

b = a;

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi,
Your post was moved to a new topic.
(This informational message will self destruct in two days)

Okay, let's take this side discussion to another topic.

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I remind people that there are three ways those assignments might fail:-
  • 1: Failure to compile.
  • 2: Compile successfully but fail to execute.
  • 3: Execute successfully but fail with exception at runtime.
  • It is possible that a declared type of an object will allow a statement to be compiled, but it may fail at runtime because the runtime types are different from those declared.
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    1-I don't know what they mean,and don't have enough time to learn them
    They are useless for me.We strictly don't use them here to maintain that simplicity and decency thing,in the forum.

    2-Noone has got any right ,to say whether something is relevant or not.Moderators are here,to decide
    To prove my post was very much relevant to the op's question:
    In op's question he has clearly assigned objects to the reference variables:


    So a,b,c are reference variable holding object,not just reference variable holding nothing, so my post do make sense in my view

    3)No need to talk on parent-child and sub-super class,had already explained it well enough
     
    Karthik Shiraly
    Bartender
    Posts: 1210
    25
    Android C++ Java Linux PHP Python
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sankalp,

    Your technical arguments are valid.
    But the way in which you express yourself comes across as condescending and belittling towards others, and distracts readers.
    The "LMAO"s and "you need to..."s kind of talk in bold red can be done away with, with absolutely no damage done to the strength of your arguments.
    Please moderate your tone if you want your opinions to be respected.
     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    According to the question asked,
    2. a = b

    is correct answer. Because a subclass reference can always be assigned to superclass reference but not vice versa.

    But sachin tripathi pointed out that and said it's object that can be assigned and the rule is applicable for that..

    But that's partially correct.
    And I said yes we can also write
    A a = new B();

    But it's not that the rule is applied to only objects but not references.

    If Sachin tripathi is having doubt about that he can compile it and check it..

    So I asked him question because he said the rule is applicable for objects

    I said if we write

    A a = new B();
    B b = new B();

    And if we take,
    b = a; still this is incompatible.

     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For my explanation, Sachin tripathi pointed out that those are objects that can be assigned. But yes it's partially incorrect because we can assign references too.
    And in the main question, it was reference that was assigned. So I explained in that way. But you pointed it wrong that it's objects not references for that I said your explanation is irrelevant. Not the answer you gave to OP's question. I replied when you pointed out in my explanation that it's object for which the rule is applied which is partially correct..
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I said:
    The rule about which you are talking about:
    Instance(object) of subclass can be assigned to reference variable of its super class (direct or indirect)

    Now here direct means
    A a=new B();
    Indirect means:
    B b =new B();
    A a=new A();
    a=b;

    Now do I need to clarify more?
     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Was my explanation incorrect?

    Then how could you say which rule I was talking about?
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your explaination was limited to just reference variable of sub class assigned to reference variable of super class
    It was not implying anything about object of sub class assigned to reference variable of super class(which is indeed the rule)
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have already been told your explanation is correct.
     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My explanation was not limited to references only.
    My explanation was to the question.
    In question references were assigned.
    In exam do you write answer to the particular question or whole book you write on the answer sheet??
    If you write answer for the particular question doesn't mean it's limited.
    According to the context you have to explain...
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes you are right ,we should be specific and give explaination to the point,but in being specific one should try not to miss an important point
    Like:
    You said2. a = b is legal assignment.

    The reason is - here, A is a parent class i.e superclass of B and C (Inheritance concept)
    There is a relationship between A-B and A-C i.e the Parent-child relationship.

    class B and C are siblings but there is no relationship between them so you can't assign B class reference = C class reference.
    that's why b = c OR c = b are incompatible.

    You are talking about just references:
    Your explaination would have been apt,if the question would have been:


    Since op has clearly mentioned in his post

    So rule in terms of object is needed to be discussed
     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you had to reply to his post you should have replied to his not mine because I was replying to the original post.
     
    Sachin Tripathi
    Ranch Hand
    Posts: 368
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought a little addition in your answer would have helped op to get the answer more clearly,and it does.

    We are here to help,not to just prove our knowledge

    I have learned a lot from here.Still I am learning one thing or other from here

    So just due to that feeling, I will do everything possible to help the forum


     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So am I... I'm as well learning and helping a little bit if I can... No one is proving anyone's knowledge
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!