File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Final Class Problem 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Final Class Problem" Watch "Final Class Problem" New topic
Author

Final Class Problem

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Source: http://www.examlab.tk/ Practice Exam: 3 Question# 30


If I uncomment the line in the main, code does not compile. Could someone please explain the reason. Explanation in the test says since it is final class and it does not implement interfae I, that's why it does not compile. I know that final class can not be overridden but I am clear about this explanation.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Check out Henry's explanation for this other question towards the bottom of this post: http://www.coderanch.com/t/417922/java-programmer-SCJP/certification/Casting-Interface

It should explain things better than I can.


SCJA
~Currently preparing for SCJP6
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Very good question.

First at all, you can see that the following statement doesn't generate any compilation error, even though A is not an implementer of I.

new A() instanceof I ;


But the following statement gives an error:

b = new E() instanceof I ;


Because E is a final class. Keep in mind that, you can NEVER perform an instanceof comparation between a final class with an interface, unless that the class is an implementer of that implementer.

This is same as to the following code:

I ob1 = new A(); //Compile time ok
I ob2 = new E(); //Compile time error.

This is another important point on final classes.
Jayakumar Ramalingam
Greenhorn

Joined: Nov 20, 2008
Posts: 1
I am getting compile time error on both the places

interface I{}
class A{}
class B implements I{}
class C extends A{}
class D{}
final class E{}

class test{
public static void main(String args[])
{
I ob1 = new A(); //Compile time error
I ob2 = new E(); //Compile time error.
I ob3 = new B();
}
}
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

You are getting this error because A and E both are not implementing interface I. At the compile time the compiler checks for IS A relationship.


My Blog SCJP 5 SCWCD 5
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Originally posted by Jayakumar Ramalingam:
I am getting compile time error on both the places

interface I{}
class A{}
class B implements I{}
class C extends A{}
class D{}
final class E{}

class test{
public static void main(String args[])
{
I ob1 = new A(); //Compile time error
I ob2 = new E(); //Compile time error.
I ob3 = new B();
}
}


Oh! I'm sorry, I made a small mistake :roll: . It should be changed as following

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I will try to point out the logic which i got from the above discussion:

[*]We can cast user defined class to any user defined interface. (Explicit casting necessary. There will be no compiler error but will through ClassCastException if it fails to pass 'IS A' test.)

[*]We cannot cast already present(Java API) class to already given Interfaces as their relationship is already defined.



------------------------------------------------------
Please correct if there is any mistake.
[ November 21, 2008: Message edited by: Himanshu Gupta ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Himanshu Gupta:
I will try to point out the logic which i got from the above discussion:
  • We can cast user defined class to any user defined interface. (Explicit casting necessary. There will be no compiler error but will through ClassCastException if it fails to pass 'IS A' test.)
  • We cannot cast already present(Java API) class to already given Interfaces as their relationship is already defined.




  • There is no special magic between the core Java classes and user defined classes in regard to this. The compiler does not make special exceptions -- it checks for legallity of casting, in the same way, in regards to both.

    How did you draw the conclusion?

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

  • We can cast user defined class to any user defined interface. (Explicit casting necessary. There will be no compiler error but will through ClassCastException if it fails to pass 'IS A' test.)


  • To prove this as not true, just take a look at the post previous to yours. Casting E to I caused a compile time error.

    Henry
    Himanshu Gupta
    Ranch Hand

    Joined: Aug 18, 2008
    Posts: 598

    I think i misunderstood the concept.

    Now it means that casting can be done anywhere and anyhow.Its upon the developer to check that it will adhere to the 'IS A' relation or not. It is applicable when programming with interfaces.(Extra care has to be given for final classes).

    Moreover if we code by using classes references then we do have to take care of the 'IS A' relation even at the compile time.


    Am i right this time? Please help.

    Thanks Henry for correcting me.
    [ November 21, 2008: Message edited by: Himanshu Gupta ]
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

    Am i right this time? Please help.


    To be blunt, I don't know what you said. I don't know what you mean by "code using class references" (isn't this always true?) or by "extra care".

    Henry
    [ November 21, 2008: Message edited by: Henry Wong ]
    Himanshu Gupta
    Ranch Hand

    Joined: Aug 18, 2008
    Posts: 598

    interface Y{}
    interface I{}
    class A{}
    class B implements I{}
    class C extends A{}
    class D{}
    final class E implements Y{}

    public class test{
    public static void main(String args[]){

    A aObj = new C();// class reference
    I iRef = new B(); // Interface reference
    }


    }

    I think this will make my point a bit clear.
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18896
        
      40

    I think this will make my point a bit clear.



    Hmmmm.... I have no idea what you said. Giving me an example doesn't help here -- as examples are use to prove a point. I don't know what point you are trying to make.

    Are you trying to make a distinction between implicit and explicit casts?

    Henry
    Devaka Cooray
    ExamLab Creator
    Saloon Keeper

    Joined: Jul 29, 2008
    Posts: 3164
        
      47

    Thanks to Kenneth for his explanation about this.
    [ November 21, 2008: Message edited by: Devaka Cooray ]

    Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
    What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Final Class Problem