aspose file tools*
The moose likes Java in General and the fly likes some Java concepts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "some Java concepts" Watch "some Java concepts" New topic
Author

some Java concepts

Rayt Leeop
Greenhorn

Joined: Jun 09, 2011
Posts: 23

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

Joined: Jun 10, 2011
Posts: 207

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:


Piyush
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

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.


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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

Joined: Jun 18, 2011
Posts: 49



Do or die...?? No, Do before you die... || (SCJP .6)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: some Java concepts