• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

some Java concepts

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 62
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 49
Java ME PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic