• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final Class cannot be instantiated

 
Alpesh Rathod
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{}
class B extends A{}
class C extends A{}

final class D extends B{
public void doMethod(){
A a=new B();
B b=new D();
C c=new C();
D d=null;
//Insert Here
}
}


d=(D)(B)a; //gives class cast exception.

Now in the above code how come an instance of D can be made since D is a final class...as far as i know that final class is never instantiated.

I m confused ...Help me..
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final keyword tell that it cnnot be extended by any other class.

Ex:-
class E extends C{} ///ERROR
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rathod, A final class can not be sub classed but it doesn't mean it can not be instantiated......
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i went wrong......my mistake....thanks
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Bad Post]Sorry[/Bad Post]

 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sunil langeh wrote:Dear Rathod, A final class can not be sub classed but it doesn't mean it can not be instantiated......



Ok, but why is this a compile time error?

thanks
<><
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a compile time error, this is runtime ClassCastException.
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Punit,

I saw it few seconds before your post...I was stupid..

thanks.
<><

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic