"super()" method :
super() method is used to call super class constructor from sub class constructor to create super class object when subclass object is created. Suppose we have a super class and sub class having a method with same name, then from subclass if we want to acess the super class method, we need to use the suer keyword for accesing the super class method. We can acess the super class members from subclass as super.variable, super.method(), super(values) ...
ex:
class SupCls {
sameMethod(){ }
}
class SubCls {
sameMethod() { }
// calling super class method
super.sameMethod();
}
We can call SuperClass Constructor From SubClass Constructor explicitly using super(); or super(parameter list);
- With super(), the SuperClass Default Constructor is called.
- With super(parameter list), the SuperClass Argumented Constructor with a matching parameter list is called.
- Invocation of a SuperClass Constructor must be the first line in the SubClass Constructor.
- If a SubClass Constructor does not explicitly invoke a SuperClass Constructor, the Java Compiler automatically call to the Default Constructor of the SuperClass. And if theSuperClass does not have a Default Constructor, we will get a compile-time error.
- Compiler places super() method call in all constructors at the time of compilation, if there is no super() or this() method call is placed explicitly.
Developer should place super() method call in below two cases:
- if super class does not contain no-arg constructor.
- if developer wants to create super class object with parameterized constructor.
In the above two cases developer should define constructor in sub class with explicit super() method class by passing agument of type same as super class constructor parameter type.
Rule: Inheritance can only be implemented if super class has a visible constructor. It means other than private constructor.
Super() method rules: Identify is inheritance possible in below cases?
Case1: Empty super class
class Example{}
class Sample extends Example{}
Case2: Super class with explicit no-arg constructor
class Example{
Example(){
Sopln("No-arg")
} }
class Sample extends Example{}
Case3: Super class with explicit parameterized constructor
class Example{
Example(int a){
Sopln("No-arg")
} }
class Sample extends Example{}
Case4: super class with private constructor
class Example{
private Example(){
Sopln("No-arg") ;
} }
class Sample extends Example{
Sample(){
Sopln("sample parm");
}
public static void main(String arg[]){
Sample s=new Sample();
}}
Case5: super class with private constructor and visible parameterized constructor
class Example{
private Example(){
Sopln("No-arg") ;
}
Example(){
Sopln("Pram");
} }
class Sample extends Example{
Sample(){
Sopln("sample parm");
}
public static void main(String arg[]){
Sample s=new Sample();
}}
- Super keyword is used to call immediate parent.
- Super keyword can be used with instance members i.e., instance variables and instance methods.
- Super keyword can be used within constructor to call the constructor of parent class.
Use of Super Keyword:
- Use of super keyword is to access the hidden data variables of the super class hidden by the sub class.
- super keyword most useful to handle situations where the local members of a subclass hide the members of a super class having the same name.
- Its a non-static variable used to store super class object reference through current sub class object.
A sample program about using this and super keywords:
public class Demo {
Demo(){
super();
System.out.println("in Demo Constructor");
}
public void m1(){
System.out.println("in m1 of Demo Class");
}
public void m2(){
System.out.println("in m2 of Demo Class");
this.m1();
}
public void m3(){
System.out.println("in m3 of Demo Class");
}
public static void main(String[] args) {
Demo d=new Demo();
d.m1();
d.m2();
d.m3();
Demo sd=new SubClassOfDemo();
sd.m1();
sd.m2();
sd.m3();
}
}
class SubClassOfDemo extends Demo
{
public SubClassOfDemo() {
super();
}
public void m1(){
System.out.println("in m1 of SubClasssDemo Class");
}
public void m2(){
super.m2();
System.out.println("in m2 of SubClasssDemo Class");
}}
2 comments:
Super keyword in java
Great, nice post.
Super keyword in java
Good, keep sharing
Post a Comment