super is used to invoke parent class constructor

The super keyword can also be used to invoke the parent class constructor. Let's see a simple example:


\begin{lstlisting}
class Animal{
Animal(){System.out.println(''animal is creat...
...blic static void main(String args[]){
Dog d=new Dog();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
animal is created
dog is created
\end{lstlisting}

Let's see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.


\begin{lstlisting}
class Person{
int id;
String name;
Person(int id,String...
...){
Emp e1=new Emp(1,''ankit'',45000f);
e1.display();
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
1 ankit 45000
\end{lstlisting}