• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we declare Abstract Class Object or if not then how can IDE do it?  RSS feed

 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

i created a class in Netbeans 7.0 IDE with JDK 6



then i created object of this class



it says yohu is abstract and can't instantiated and when i clicked resolve on side of netbeans

it displayed like



and i can access the methods in obj too

i am unable to get what is this -> new yohu() {};

in searches i get anonymous class concept, is this related to that? if yes then if i write new azrael(){} , it does not work

please show the path
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are creating an instance of an anonymous subclass of yohu.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azrael Noor wrote:H
it says yohu is abstract and can't instantiated and when i clicked resolve on side of netbeans

it displayed like



and i can access the methods in obj too

i am unable to get what is this -> new yohu() {};

in searches i get anonymous class concept, is this related to that


Yes. When you have the syntax new Foo() { /* body */ } that is an anonymous inner class. If Foo is a class, the anonymous class is extending Foo. If Foo is an interface, then the anonymous class is implementing it.

The { /* body */} part is what makes is an anonymous subtype of Foo, instead of just an instantiation of Foo. If Foo has any abstract methods, then they must be implemented in { /* body */ }. In your example, there were no abstract methods in the parent class, so it was okay to leave the body empty.

? if yes then if i write new azrael(){} , it does not work


Hard to say without more details. Maybe you haven't defined the parent class azrael. Maybe azrael has abstract methods and you didn't implement them.

ItDoesntWorkIsUseless.(⇐click) You need to TellTheDetails(⇐click) about what you tried and what the exact, complete error message was.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write:
You are almost doing the equivalent of this:

I say "almost": it's effectively the same, except that you can't later refer to the same class by name in your code. That's why it's called an "anonymous" class. It will have a name, autogenerated by the compiler, but you don't need to know what it is.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also suggest that you come up with better names for your classes. Things like 'yohu' and 'azrael' may have a meaning to you, but not to others (and certainly not me).

Winston
 
Azrael Noor
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for details


? if yes then if i write new azrael(){} , it does not work

Hard to say without more details. Maybe you haven't defined the parent class azrael. Maybe azrael has abstract methods and you didn't implement them.


Class azrael is not defined anywhere. I used it, if it is anonymous , it will take any name then, isn't it correct?





Winston - Yhoa may not appropriate as it is word from dialect, but azrael is my name Ok i will not create classes with name.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azrael Noor wrote:

? if yes then if i write new azrael(){} , it does not work

Hard to say without more details. Maybe you haven't defined the parent class azrael. Maybe azrael has abstract methods and you didn't implement them.


Class azrael is not defined anywhere. I used it, if it is anonymous , it will take any name then, isn't it correct?



Well, no. I think you need to brush up on anonymous classes. Anonymous classes means that the class itself (its name) is anonymous -- it doesn't mean that the super class doesn't exist.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azrael Noor wrote:Thank you all for details


? if yes then if i write new azrael(){} , it does not work

Hard to say without more details. Maybe you haven't defined the parent class azrael. Maybe azrael has abstract methods and you didn't implement them.


Class azrael is not defined anywhere. I used it, if it is anonymous , it will take any name then, isn't it correct?


That is an assumption you are making. That assumption may be correct or it may be wrong.

Evidence that the assumption is correct:
Do you have any?

Evidence that the assumption is wrong:
  • The compiler is telling you that you can't do that.
  • I already answered your question and told you that you can't do that.


  • Your potential courses of action:
  • You can cling to the belief that the assumption must be correct because it's what you assume and you want it to be correct.
  • You can look for further evidence to support or refute the assumption.
  • You can conclude that you now have enough evidence to let go of the assumption.


  • Merely asking the same question again, however, is not likely to be productive.


    Winston - Yhoa may not appropriate as it is word from dialect, but azrael is my name Ok i will not create classes with name.


    It's entirely up to you what names you use for your classes. However, a couple of points to consider:

    1. Using your own name for a class is meaningless, unless you are defining, "This is what an Azrael is, and what it can do."

    2. When you're asking somebody for help, if you use words that they don't understand, it will make it harder for them to help you, even if you understand those words perfectly.
     
    Azrael Noor
    Ranch Hand
    Posts: 385
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff : ok


    Henry Wong : Thank you for clarifying....
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!