3

I'm not sure why Android allows me to do this? Or is this even valid?

I have code like this:

    LinearLayout linearLayout = (LinearLayout) this.findViewById(R.id.mainid);

findViewById returns a View, but I'm casting it to a LinearLayout which extends a ViewGroup. Wouldn't this be a dangerous thing to do since, I'm getting none of the the LinearLayout properties that may need to be set? Or wouldn't calling one of the methods from this cast cause issues since we have not allocated memory to it yet?

mskw
  • 10,063
  • 9
  • 42
  • 64

3 Answers3

6

If you look at the class structure for LinearLayout, you will see that ViewGroup inherits View, hence you can do that.

Argyle
  • 3,324
  • 25
  • 44
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • But here I'm up casting, which means I'm casting to a extended class with extra methods and variables inside the LinearLayout, I thought that would not work. Here I'm converting a View into LinearLayout – mskw Jul 16 '14 at 19:22
  • I dont understand what you mean, if a class inherits another class you can cast down to any of the other classes it inherits meaning you could cast all the way down to `Object` if you wanted – tyczj Jul 16 '14 at 19:24
  • But you cannot Cast an Object into a LinearLayout just because it inherits from it right? – mskw Jul 16 '14 at 19:25
  • 1
    an object does not inherit from LinearLayout, object is the base class for everything, you can just go casting random objects to stuff if they dont inherit from that – tyczj Jul 16 '14 at 19:26
  • I'm asking since LinearLayout inherits from Object(not the other way around), does that mean I can cast an Object into a LinearLayout? – mskw Jul 16 '14 at 19:29
  • 1
    Object object = new Object(). LinearLayout layout = object; This wouldnt work because OBJECT is NOT a LinearLayout – committedandroider Jul 16 '14 at 19:30
  • @mskw no and that makes no sense – tyczj Jul 16 '14 at 19:30
  • @committedandroider, that would cause issues right? We did not allocate enough memory for 'layout' for it to cast like that. Or is my understanding of what cast does is wrong. – mskw Jul 16 '14 at 19:32
  • @committedandroider what if I do this, is this valid? Object object = new Object(). LinearLayout layout = (LinearLayout)object – mskw Jul 16 '14 at 19:33
  • 1
    @mskw no I just said object does not inherit LinearLayout http://stackoverflow.com/questions/5289393/casting-variables-in-java – tyczj Jul 16 '14 at 19:34
  • 1
    Cast is saying i want to treat this object as another specific object type. In this case you're saying you want to treat an Object as a LinearLayout, which doesn't make sense intuitivelyif you think about it. Because youre calling LinearLayout methods on the object which may not have attributes that a linearlayout has – committedandroider Jul 16 '14 at 19:35
  • @tyczj I know that. I'm saying LinearLayout Inherites from object. just like LinearLayout inherits from View. – mskw Jul 16 '14 at 19:35
  • @mskw no that is not what you are saying with that code, what you are saying is that object inherits from `LinearLayout` which it does not – tyczj Jul 16 '14 at 19:38
  • @tyczj I'm converting an Object into a LinearLayout, which requires LinearLayout to inherit from Object. Which is equal to Converting a View into a LinearLayout, which requires LinearLayout to inherit from View. Why is this wrong? And the code would be (LinearLayout)object which is also (LinearLayout)viewObject. What is wrong with that? – mskw Jul 16 '14 at 19:41
  • 1
    @mskw its impossible to just "convert" and object to LinearLayout, what avout all the other classes LinearLayout inherits from, how do they get created? – tyczj Jul 16 '14 at 19:46
  • Yes thats why original question, why did Android compiler allowed me to do that. As it turns out, that object is actually a LinearLayout thats why I can. – mskw Jul 16 '14 at 19:47
  • 2
    @mskw if you would have read the link tyczj posted, you would have seen that the casting is to be decided by the developer. The compiler simply sees that `LinearLayout` `isa` `View` and says "ok". The fact that it may not be is something *you* have to watch out for. – A--C Jul 16 '14 at 19:49
  • 2
    @mskw yes that is what the object is which is what we have been saying all along. If you try to cast that to domething its not just say TextView or something you will get ClassCastException – tyczj Jul 16 '14 at 19:50
4

LinearLayout is a ViewGroup, a View that contains other views. Hence you can cast a View to a ViewGroup This is what you're doing

(LinearLayout) this.findViewById(R.id.mainid);

Currently think the View before the cast is

View v = LinearLayout(R.id.mainid)

The actual object type is LinearLayout

You can cast down to LinearLayout because that is the actual type of the object

A--C
  • 36,351
  • 10
  • 106
  • 92
committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • This explains it. So you are saying if the actual object is not linearLayout, it would cause undefined behaviours. – mskw Jul 16 '14 at 19:24
  • yess, hence the class cast exception as mentioned below – committedandroider Jul 16 '14 at 19:25
  • I'm not sure that other answer has so many up votes when things can go seriously wrong if you mis-cast a View object into a LinearLayout when the View object is not actually a LinearLayout. – mskw Jul 16 '14 at 19:27
  • Thats why you should give ids that help you distinguish the different views – committedandroider Jul 16 '14 at 19:29
  • @mskw you cannot forward cast something (if thats even a word) to cast to something means a class must inherit from that class – tyczj Jul 16 '14 at 19:29
2

ViewGroup is a specialization of View, so the cast is actually valid: http://developer.android.com/reference/android/view/ViewGroup.html

Of course, as any cast, you should know what you are doing unless the dreaded ClassCastException occurs.

Shlublu
  • 10,917
  • 4
  • 51
  • 70