Super Keyword:
The functionality of super keyword is only to point the immediate super class object of the current object.
super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
super keyword used to access the members of the super class object.
super.member;
It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.
We can call super class constructor in sub class using super() call.
We can access super class methods and variables in sub class using super.variable_name, super.method();
Uses of super :
1. By using super keyword we can access super class variables in sub class.
Using super keyword we can access super class variables from sub class.
super.variable_name.
package com.superkeywordinjava;
public Class SuperDemo{
int a,b;
}
package com.superkeywordinjava;
public Class Subdemo extends SuperDemo{
int a,b;
void disply(){
System.out.println(a);
System.out.println(b);
super.a=10;
super.b=20;
System.out.println(super.a);
System.out.println(super.b);
}
public static void main (String args[]) {
Subdemo obj= new Subdemo();
obj.a=1;
obj.b=2;
obj.disply();
}
}
Output:
1
2
10
20
2. By using super keyword we can access super class methods in sub class.
Using super keyword we can call super class methods from sub class.
super.method();.
package com.instanceofjavaforus;
public Class SuperDemo{
int a,b;
public void show() {
System.out.println(a);
System.out.println(b);
}
}
package com.instanceofjavaforus;
public Class Subdemo extends SuperDemo{
int a,b;
void disply(){
System.out.println(a);
System.out.println(b);
super.a=10;
super.b=20;
super.show();
}
public static void main (String args[]) {
Subdemo obj= new Subdemo();
obj.a=1;
obj.b=2;
obj.disply();
}
}
Output:
1
2
10
20
3. We can call super class constructor from class constructor:
By using super keyword we can able to call super class constructor from sub class constructor.
Using super();
For the super(); call must be first statement in sub class constructor.
package com.superinterviewprograms;
public Class SuperDemo{
int a,b;
SuperDemo(int x, int y){
a=x;
b=y
System.out.println("Super class constructor called ");
}
}
package com.superinterviewprograms;
public Class Subdemo extends SuperDemo{
int a,b;
SubDemo(int x, int y){
super(10,20);
a=x;
b=y
System.out.println("Sub class constructor called ");
}
public static void main (String args[]) {
Subdemo obj= new Subdemo(1,2);
}
}
Output:
Super class constructor called
Sub class constructor called
key points:
Super(); call must be first statement inside constructor.
By default in every class constructor JVM adds super(); call in side the constructor as first statement which calls default constructor of super class, if we are not not calling it.
If we want to call explicitly we can call at that time default call wont be there.
4.What will happen if we are calling super() in constructor but our class does not extending any class?
if our class not extending any class Yes still we can use super(); call in our class
Because in java every class will extend Object class by default this will be added by JVM.
But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.
package com.superinterviewprograms;
public Class Sample{
Sample(){
super();
System.out.println("Sample class constructor called ");
}
public static void main (String args[]) {
Sample obj= new Sample();
}
}
Output:
Sample class constructor called
5.What if there is a chain of extended classes and 'super' keyword is used
package com.superinterviewprograms;
public Class A{
A(){
System.out.println("A class constructor called ");
}
}
package com.superinterviewprograms;
public Class B extends A{
B(){
System.out.println("B class constructor called ");
}
}
package com.superinterviewprograms;
public Class C extends B{
C(){
System.out.println("C class constructor called ");
}
public static void main (String args[]) {
C obj= new C();
}
}
Output:
A class constructor called
B class constructor called
C class constructor called
6. Can we call super class methods from static methods of sub class?
No we can not use super in static methods of sub class Because super belongs to object level so we can not use super in static methods.
If we try to use in sub class static methods compile time error will come.
The functionality of super keyword is only to point the immediate super class object of the current object.
super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
super keyword used to access the members of the super class object.
super.member;
It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.
We can call super class constructor in sub class using super() call.
We can access super class methods and variables in sub class using super.variable_name, super.method();
Uses of super :
1. By using super keyword we can access super class variables in sub class.
Using super keyword we can access super class variables from sub class.
super.variable_name.
package com.superkeywordinjava;
public Class SuperDemo{
int a,b;
}
package com.superkeywordinjava;
public Class Subdemo extends SuperDemo{
int a,b;
void disply(){
System.out.println(a);
System.out.println(b);
super.a=10;
super.b=20;
System.out.println(super.a);
System.out.println(super.b);
}
public static void main (String args[]) {
Subdemo obj= new Subdemo();
obj.a=1;
obj.b=2;
obj.disply();
}
}
Output:
1
2
10
20
2. By using super keyword we can access super class methods in sub class.
Using super keyword we can call super class methods from sub class.
super.method();.
package com.instanceofjavaforus;
public Class SuperDemo{
int a,b;
public void show() {
System.out.println(a);
System.out.println(b);
}
}
package com.instanceofjavaforus;
public Class Subdemo extends SuperDemo{
int a,b;
void disply(){
System.out.println(a);
System.out.println(b);
super.a=10;
super.b=20;
super.show();
}
public static void main (String args[]) {
Subdemo obj= new Subdemo();
obj.a=1;
obj.b=2;
obj.disply();
}
}
Output:
1
2
10
20
3. We can call super class constructor from class constructor:
By using super keyword we can able to call super class constructor from sub class constructor.
Using super();
For the super(); call must be first statement in sub class constructor.
package com.superinterviewprograms;
public Class SuperDemo{
int a,b;
SuperDemo(int x, int y){
a=x;
b=y
System.out.println("Super class constructor called ");
}
}
package com.superinterviewprograms;
public Class Subdemo extends SuperDemo{
int a,b;
SubDemo(int x, int y){
super(10,20);
a=x;
b=y
System.out.println("Sub class constructor called ");
}
public static void main (String args[]) {
Subdemo obj= new Subdemo(1,2);
}
}
Output:
Super class constructor called
Sub class constructor called
key points:
Super(); call must be first statement inside constructor.
By default in every class constructor JVM adds super(); call in side the constructor as first statement which calls default constructor of super class, if we are not not calling it.
If we want to call explicitly we can call at that time default call wont be there.
4.What will happen if we are calling super() in constructor but our class does not extending any class?
if our class not extending any class Yes still we can use super(); call in our class
Because in java every class will extend Object class by default this will be added by JVM.
But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.
package com.superinterviewprograms;
public Class Sample{
Sample(){
super();
System.out.println("Sample class constructor called ");
}
public static void main (String args[]) {
Sample obj= new Sample();
}
}
Output:
Sample class constructor called
5.What if there is a chain of extended classes and 'super' keyword is used
package com.superinterviewprograms;
public Class A{
A(){
System.out.println("A class constructor called ");
}
}
package com.superinterviewprograms;
public Class B extends A{
B(){
System.out.println("B class constructor called ");
}
}
package com.superinterviewprograms;
public Class C extends B{
C(){
System.out.println("C class constructor called ");
}
public static void main (String args[]) {
C obj= new C();
}
}
Output:
A class constructor called
B class constructor called
C class constructor called
6. Can we call super class methods from static methods of sub class?
No we can not use super in static methods of sub class Because super belongs to object level so we can not use super in static methods.
If we try to use in sub class static methods compile time error will come.
No comments:
Post a Comment