Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

when use default super()?

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Is there sometimes that you need to invoke the default super() explict?
I'm studying and I encoutered this:
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is optional to add super() call in constructor. Mentioning it gives a code clarity

Thanks
Abhay Agarwal

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use super keyword, for the same program as,


Also, sometime the Parents class method is robust and implemented the best logic which is applicable to every sub class. So such time, you really don't need to override that method, just calling the Parents method, is sufficient. One example I know is when you override javax.servlet.http.HttpServletclass to make servlet. overriding HttpServlet#service() method is always discouraged and even if you override it, make sure you call super() in it.
 
Ranch Hand
Posts: 32
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be help you to understand briefly:

http://leepoint.net/notes-java/oop/constructors/constructor-super.html
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tekchand Prasad wrote:This might be help you to understand briefly:
http://leepoint.net/notes-java/oop/constructors/constructor-super.html


Yes, but that's too specifically for Constructors. If its constructor, then poster don't even need to call explicitly the method, like


The point is, JVM automatically inserts, a call to no-arg constructor of its super class. But as I said previously, if the implementation details(method body) logic is care taken by the Parents class itself, then one just need to call super() while overriding that method.
 
Sheriff
Posts: 9686
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Rohankar wrote:You can use super keyword, for the same program as,


Also, sometime the Parents class method is robust and implemented the best logic which is applicable to every sub class. So such time, you really don't need to override that method, just calling the Parents method, is sufficient. One example I know is when you override java.lang.HttpServletclass to make servlet. overriding HttpServlet#service() method is always discouraged and even if you override it, make sure you call super() in it.



Sagar, I don't think your code will compile. Maybe you were trying to write super.eat(f);. Anyways, super call to a no-argument constructor is superfluous. But if the super class doesn't have any no-argument constructor, or if you want to invoke another constructor with arguments, then it is worth to use super

 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
Sagar, I don't think your code will compile. Maybe you were trying to write super.eat(f);


Yes, exactly. I'm just confused with the super in constructor and super in overridden method. Sorry, for the error.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic