• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final class

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?

Thanks
Preetha
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect answer, Punit. I am removing my erroneous answer.
[ December 16, 2008: Message edited by: Ruben Soto ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic