• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java class Super()??

 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying out some code and came across unexpected behavior as and referenced my user defined class to 'new Super()'. Is there any predefined class in Java API by the name Super()?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
super() always refers to the super class, if none other, it is Object class itself in java.
 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshvardhan thanks for your response

Oops... big goof up In elaborating my question properly I answered my own question
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super != super.

I'm not aware of any pre-defined classes with that name (and it would be a poor choice of name so I'd be surprised if there was). And unless it was in java.lang you wouldn't have a clash anyway unless you imported it.

So if the capitalization is as you say, the cause of the strange behaviour is probably something else.
 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew, thanks for your response

Very true, it would be a poor choice of a class name. I searched for 'Super()' in google and Java API but could not find any class by this name
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Matthew has already hinted, spellings are important in programming. In a case‑sensitive language like Java, the compiler will distinguish
  • super, which is a keyword, from
  • Super, which is a poorly‑named class, and both of those from
  • Super() which is (probably) a constructor call.
  • You should use whichever spelling is correct in your posts. You probably sought Super in the index not Super().
     
    Vishnu Khera
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell thanks for the clarification

    It was indeed a poor choice of naming my class as Super, but the reason for naming it so was the length of the program I wanted to compile. It was a small 10 line program.

    Yes, its a good tip because I was getting a strange compile time error in the same program

    I understand if there were any, it would be class Super and not class Super()

    Probably waaay too excited to post questions on this forum and get suuuper quick replies


    BIG THANKS TO ALL OF YOU
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50289
    80
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You’re welcome
     
    vinay chaturvedi
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can use super as a keyword to call the super class methods in a sub-class.

    For Ex : super.superClassMethod();

    Or you can use super() to call the super class constructor.

    For Ex :



     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic