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

Final Class cannot be instantiated

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final keyword tell that it cnnot be extended by any other class.

Ex:-
class E extends C{} ///ERROR
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i went wrong......my mistake....thanks
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Bad Post]Sorry[/Bad Post]

 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
<><
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Punit,

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

thanks.
<><

reply
    Bookmark Topic Watch Topic
  • New Topic