Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Subclass and Superclass in Java OCA 8 Programmer I Study Guide (Sybex)

 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. This is my first post, and I need to say thanks to all of you for this great forum.
I'm studing the OCA java 8 programmer I study guid from Jeanny Boryarsky and Scott Selikoff.
As I understand in the chapter 5 "Class Design", subclass is the child and superclass is the parent. But at page 282, at the and of the page are 4 rules for casting objects.


1. Casting an object from a subclass to a superclass doesn't require an explicit cast.
2. Castina an object from a superclass to a subclass requires an explicit cast.
3.
4.


The first 2 rules are not correct, it is the oposite.

Casting an object from a child (subclass) to a parent (superclass) require an explicit cast.
Please correct me if I'm wrong .
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claudiu Stroe,

First of all, a warm welcome to CodeRanch!

Claudiu Stroe wrote:Casting an object from a child (subclass) to a parent (superclass) require an explicit cast.
Please correct me if I'm wrong .

You are incorrect! The study guide is spot-on.

Let's use an example from the real world as it's probably easier to understand. You have animals (superclass) and cats (subclass). All cats (subclass) are animals (superclass), so no explicit cast is required. But not all animals (superclass) are cats (subclass), because you have dogs, cows, lions,... So you need an explicit cast to assign an animal (superclass) to a cat (subclass). And if the animal is something different than a cat, you'll get an exception (ClassCastException).

Hope it helps!
Kind regards,
Roel
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer Roel.

From cat(subclass) to animal(superclass) does not compile, and needs a cast
From animal to cat it does compile

Cat cat = new Animal() // does not compile
Animal animal = new Cat() // it's ok

I think the problem is the way i understand English. English is not my primary language

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:From cat(subclass) to animal(superclass) does not compile, and needs a cast
From animal to cat it does compile

Cat cat = new Animal() // does not compile
Animal animal = new Cat() // it's ok

Based on your explanation and the code examples, I think I know the cause of your misunderstanding. Because your code examples don't match your explanation And that might indeed be related to your understanding of the English.

Let's have a look at this castIn this statement you are casting from a Cat to an Animal. Here's another example from a castOn line1 you create a new Animal object/instance. And on line2 you are casting from an Animal to a Cat. And here is a third exampleHere you are casting from a Cat to an Animal. A cast from a subclass to a superclass happens implicitly, so you can write an explicit cast but it's not required. Both these statement will compile and are equivalentBut a cast from a superclass to a subclass never happens implicitly, it must always be mentioned explicitly. In the following code snippet, the second statement will fail to compile (no explicit cast) and the third statement will successfully compile because it uses an explicit cast.Although the third statement compiles successfully, the third statement will throw a ClassCastException at runtime because the object to which the reference variable a is referring to, IS-NOT-A Cat (it's an Animal).

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:I think the problem is the way i understand English. English is not my primary language

Luckily for you, the "casting (and instanceof)" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "casting (and instanceof)". These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about casting (and instanceof):
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class

  • I think you know what to do today

    Hope it helps!
    Kind regards,
    Roel
     
    Claudiu Stroe
    Greenhorn
    Posts: 29
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot! Now I understand what was wrong in my readings.

    A reference to an object Animal is equal to a cast from Cat to an Animal and you don't need an explicit cast



    A reference to an object Cat is equal to a cast from Animal to a Cat and you need a cast. It will compile but it will throw a ClassCastException at runtime if Animal is not instanceof Cat


     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Claudiu Stroe wrote:A reference to an object Animal is equal to a cast from Cat to an Animal and you don't need an explicit cast

    Spot-on! An object of a subclass (e.g. a Cat object) can be assigned to a reference variable of a superclass (e.g. an Animal or Object reference variable) without specifying an explicit cast.

    Claudiu Stroe wrote:A reference to an object Cat is equal to a cast from Animal to a Cat and you need a cast. It will compile but it will throw a ClassCastException at runtime if Animal is not instanceof Cat

    Although the gist of your thoughts is understandable and correct, you probably need to be a little bit more careful about wording and phrasing.

    Let's consider your code snippetBecause a Cat IS-A Animal, you can assign a Cat instance to an Animal reference variable (without the need of an explicit cast). If you want to assign this Animal reference variable to a Cat reference variable, you need an explicit cast. Because Cat IS-A Animal, the compiler allows you to cast the Animal reference variable to Cat. But if the object denoted by the Animal reference variable IS-NOT-A Cat, a ClassCastException will be thrown at runtime. To avoid this exception from being thrown at runtime, you can add a check using the instanceof operator. It returns true if the object is of the specified type; false otherwise.

    Hope it helps!
    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic