Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

some Java concepts

 
Rayt Leeop
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have several questions, can anyone help me some concepts? Many thanks.

1) All classes have inheritence because they inherit from some other class (we state it) or implicitly inherit from Java's standard root class "Object". is this right?

2) When assign a variable to an object between super and sub class, an object of subclass can be assigned to a variable of type Super class? and in reserve: an object of superclass can be assigned to a variable of type subclass?


 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rayt Leeop wrote:
1) All classes have inheritence because they inherit from some other class (we state it) or implicitly inherit from Java's standard root class "Object". is this right?

This is right. To add to this a class can extend from a single class, but not from multiple classes.
Rayt Leeop wrote:
2) When assign a variable to an object between super and sub class, an object of subclass can be assigned to a variable of type Super class? and in reserve: an object of superclass can be assigned to a variable of type subclass?

This is also right, but when we assign an object of superclass to a variable of type subclass then we need to explicitly cast the object:
 
joy b chakravarty
Ranch Hand
Posts: 62
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Child obj3 = (Child) new Base(); //we need to cast here

This is not a good thing to do.
Lets say you have a method defined in a sub class (not present in the super class), this this case.

calling obj3.childMethod() will result in java.lang.UnsupportedOperationException
This is because the object is actually of type Base, and doesnt have the method 'childMethod'.
So up casting is fine,

Before down casting, make sure the object passes the 'instance of' test, else you might end up with UnsupportedOperationException.

 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Child obj3 = (Child) new Base(); //we need to cast here

This is not a good thing to do.

Yes you are right. That was not at all a good thing to do.
I was just trying to demonstrate what is legal.
I should have come up with a better example.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joy b chakravarty wrote:
Child obj3 = (Child) new Base(); //we need to cast here

This is not a good thing to do.

Correct, but not because of the reason you gave. This cast will throw a ClassCastException since the Base instance simply isn't a Child.
 
Piyush Joshi
Ranch Hand
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
joy b chakravarty wrote:
Child obj3 = (Child) new Base(); //we need to cast here

This is not a good thing to do.

Correct, but not because of the reason you gave. This cast will throw a ClassCastException since the Base instance simply isn't a Child.

Thanks for correcting me. I posted that example to just show the basic legal assignment of variables and casting.
I forgot that it will throw ClassCastException at runtime.
Thanks again.
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic