• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Final Class Problem

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out Henry's explanation for this other question towards the bottom of this post: https://coderanch.com/t/417922/java-programmer-SCJP/certification/Casting-Interface

It should explain things better than I can.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kenneth Lomvey
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
     
    Henry Wong
    author
    Posts: 23958
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

  • 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
    Posts: 598
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 23958
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 598
    Android Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 23958
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Sheriff
    Posts: 7345
    1404
    IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks to Kenneth for his explanation about this.
    [ November 21, 2008: Message edited by: Devaka Cooray ]
     
    Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic