wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final class" Watch "final class" New topic

final class

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
this concept i took from devaka's examlab.
why can't final class be applied to instanceof test with the interface which it does not implement?

for eg:

class A{}
class B extends A implements inter{}
final class C{}

class Testex
public static void main(String args[])
boolean b=new A() instanceof inter;
boolean b1=new B() instanceof inter;
boolean b2=new C() instanceof inter; // compiler error ie. inconvertible type
System.out.println(b+ "," +b1+ "," + b2);

could someone explain this?

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Simple Arun.
Now your C class is final

final class C{}

So you cannot do

class D extends C implements inter{}

Why? As final class cannot be subclassed.
So It will never be possible to do

C c=new D();

So it is not possible to make subclass of C means now no possibility to make a reference of C will point to an object that implements inter interface. Compiler can detect these things. So it will not allow final class reference to be checked as instanceof any interface that final class is not implementing.
[ December 16, 2008: Message edited by: punit.singh ]

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Perfect answer, Punit. I am removing my erroneous answer.
[ December 16, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
I agree. Here's the link: http://aspose.com/file-tools
subject: final class
It's not a secret anymore!