-2

We know that the constructor of the parent class could be called from the constructor of the child class. It could be done by calling super and super should be the first statement of the constructor.

Is it just a feature kept for increasing the number of feature of OOP concept or there is any practical application?

If there exists any practical application, please describe.

Please take a look here, before answering:

I am asking for a practical application. That means a real life scenario where it would be very much useful to use this feature instead of doing the work other how.

Everyone is just posting the concept or code how it works. But I already know it. I just want one practical scenario where it would safe me from either writing a big piece of code or it is impossible to do other how.

Community
  • 1
  • 1
Fresher
  • 25
  • 5
  • This might be a duplicate, I initially voted to close it. I'm not sure though, so I reopened it again. – Keppil May 12 '16 at 11:11
  • @Keppil I think you have judged too quickly. – Fresher May 12 '16 at 11:12
  • 2
    Since you already know how it works, you also already now a practical scenario: everytime the parent constructors needs arguments. – Tom May 12 '16 at 11:14
  • @Tom Did you ever use it in practical? If yes, then why? what was the scenario? why you did not do any other how? Thats all I need. Clear? – Fresher May 12 '16 at 11:16
  • 1
    No it is not clear, why you still ask for the scenario. Everyone told you that the scenario is, when a parent class constructor expects an argument. There is no other meaning or magic behind that to describe the scenario anymore. *"why you did not do any other how?"* Why should I? – Tom May 12 '16 at 11:28
  • @Tom "when a parent class constructor expects an argument" can you clearly tell me when the 'when' in your statement occurs in real life? You are still in technical answer and telling why am I asking for scenario! Come to the real life instead of technical life. Thanks – Fresher May 12 '16 at 11:34
  • @Tom See the accepted answer to feel what is real life example. – Fresher May 12 '16 at 11:35
  • You really needed someone else to come up with such an example? A little bit of fantasy is more than enough. – Tom May 12 '16 at 11:43

6 Answers6

5

Suppose the parent class requires a constructor parameter, but the subclass doesn't because it will supply that parameter. You'd make the call explicitly in order to provide the parameter.

class Vehicle {
     private VehicleType type;

     Vehicle(VehicleType t) {
        this.type = t;
     }
}

class Car extends Vehicle {
    Car() {
        super(VehicleType.Car);
    }
}

That's just one example. Another might be if the parent class's constructor may throw an exception, and the child class wants to throw a more general exception (perhaps to hide internal details, maybe the parent class isn't public):

class Parent {
     Parent() throws InternalException {
        // ...
     }
}

public class Child extends Parent {
    Child() throws PublicException {
        try {
            super();
        } catch (InternalException e) {
            throw new PublicException();
        }
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This is known to me. I asked for a practical application, that means, in real life, where it could be use? please, provide a scenario where the purpose would not serve without it or at least it would need a lot of coding if we want to do it other how. – Fresher May 12 '16 at 11:03
  • @Fresher: The above are practical examples: Whenever you need to handle the call to the superclass constructor in a way other than the default. If you mean *real-world*, I figure the Vehicle/Car thing could easily exist in the real world. Or I was once involved in a board game website. We had a `Game` class which accepted the dimensions of the board (e.g., 8x8 for Chess), and then a `ChessGame` subclass that did `super(8, 8);` in its constructor. – T.J. Crowder May 12 '16 at 11:19
3

This feature is incredibly useful in practice, because it lets you reuse parent class initialization.

Here is one of the most basic examples:

abstract class Positionable {
    private int x, y;
    public int getX() {return x;}
    public int getY() {return y;}
    protected Positionable(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
class Circle extends Positionable {
    private int r;
    public int getR() {return r;}
    public Circle(int x, int y, int r) {
        super(x, y);
        this.r = r;
    }
}
class Rectangle extends Positionable {
    private int w, h;
    public int getH() {return h;}
    public int getW() {return w;}
    public Rectangle(int x, int y, int h, int w) {
        super(x, y);
        this.h = h;
        this.w = w;
    }
}

Note how Circle and Rectangle share the logic from the constructor of Positionable, instead of setting up x and y themselves (which would have to be protected, not private, if the call of super from the constructor were not supported).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

As a short answer if I understand you right - if you have some initialisation/setup code in the superclass constructor, which you also need in the subclass, then by calling super(..), you wouldn't need to duplicate that in the subclass constructor.

nsandersen
  • 896
  • 2
  • 16
  • 41
0

Here's an example

class Person {

    String name;

    Person(String name) {
        this.name = name;
    }
}

class Employee extends Person {

    Employee(String name) {
        super(name);
    }
}

Note that if you don't call super explicitly a super(); call is automatically added on the first line of the constructor by the compiler. This means you get a little bit strange compilation error if you remove the super(name); call in the example above because there is no default constructor in the super class.

If you then also remove the Person(String name) constructor it will compile again, because a default constructor is automatically added to the Person class if no constructors are explicitly specified.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
0

Of course there are practical applications.

I had a class called registers, so whenever a new register is created a mail was triggered to the user which was an argument of the constructor.

After some days there came new subregisters whenever they were created mail needs to be send to user as well as a db entry was supposed to be made.

Subregisters inherited registers and in its constructor we called super(user) and then made a method call to make db entry.

It's how you stack it. There can be different parent constructor or you can even call parent methods with super.

Nothing in java is there without reasons, even if there is, it won't be there for long.

Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29
0

Java supports Inheritance feature and super() keyword describes the default contructor of super class. You can also use super(Params... params) whenever you extend your subclass from a superclass. OOP conceptually needs inheritance of classes to keep the interaction relations more precise.

cihan adil seven
  • 541
  • 1
  • 4
  • 20