规则:
每当我们在派生类构造函数中使用Super()或者超级(...)时,它们必须用作第一个语句。
1.每当我们想要从派生类中的默认Class of派生类的默认构造函数调用默认构造函数,派生类的默认构造函数是可选的。
例如:
class Bc { Bc () { System.out.println ("I AM FROM BASE CLASS..."); } }; class Ibc extends Bc { Ibc () { System.out.println ("I AM FROM INTERMEDIATE BASE CLASS..."); } }; class Dc extends Ibc { Dc () { super (); //optional System.out.println ("I AM FROM DERIVED CLASS..."); } }; class InDemo3 { public static void main (String k []) { Dc o1=new Dc (); } };
2.每当我们想在派生类中使用Super(...)的派生类中的参数化类调用Super类参数化类是必需的。
例如:
class C1 { int a; C1 (int a) { System.out.println ("PARAMETERIZED CONSTRUCTOR - C1"); this.a=a; System.out.println ("VALUE OF a = "+a); } }; class C2 extends C1 { int b; C2 (int a, int b) { super (a); System.out.println ("PARAMETERIZED CONSTRUCTOR - C2"); this.b=b; System.out.println ("VALUE OF b = "+b); } }; class InDemo4 { public static void main (String k []) { C2 o2=new C2 (10, 20); } };
3.每当我们想要从参数化的派生类中拨出的基类的默认构造函数,使用super()在派生类中的派生类中是可选的。
例如:
class C1 { int a; C1 () { System.out.println ("PARAMETERIZED CONSTRUCTOR - C1"); this.a=a; System.out.println ("VALUE OF a = "+a); } }; class C2 extends C1 { int b; C2 (int b) { super (); //optional System.out.println ("PARAMETERIZED CONSTRUCTOR - C2"); this.b=b; System.out.println ("VALUE OF b = "+b); } }; class InDemo5 { public static void main (String k []) { C2 o2=new C2 (20); } };
4.每当我们想要从派生类的默认Class of派生类的默认构造函数调用参数化的基类,在派生类的默认构造函数中是必需的。
例如:
class C1 { int a; C1 (int a) { System.out.println ("PARAMETERIZED CONSTRUCTOR - C1"); this.a=a; System.out.println ("VALUE OF a = "+a); } }; class C2 extends C1 { int b; C2 () { super (10); System.out.println ("DEFAULT CONSTRUCTOR - C2"); this.b=20; System.out.println ("VALUE OF b = "+b); } }; class InDemo6 { public static void main (String k []) { C2 o2=new C2 (); } };
Super关键字用于区分具有派生类功能的基类功能。
超级关键字在三个地方处于重要作用。
它们处于可变级别,在方法级别和构造函数级别。
变量级别的Super
每当我们将基类成员继承到派生类中时,基类成员都有可能与派生类成员类似。
为了将基类成员与派生类中的派生类成员区分开来,基类成员将在关键字super之后。
变量级别的Super的语法:
super. base class member name
例如:
class Bc { int a; }; class Dc extends Bc { int a; void set (int x, int y) { super.a=x; a=y; //by default 'a' is preceded with 'this.' since 'this.' represents current class } void sum () { System.out.println ("SUM = "+(super.a+a)); } }; class InDemo { public static void main (String k []) { int n1=Integer.parseInt (k[0]); int n2=Integer.parseInt (k[1]); Dc do1=new Dc (); do1.set (n1, n2); do1.sum (); } };
方法级别的Super
每当我们将基类方法继承到派生类中时,都有可能是基类方法与派生方法类似。
要在派生类中区分派生类方法 和 基类方法,基类方法前面必须有关键字super。
Super在方法级别的语法:
super. base class method name
例如:
class Bc { void display () { System.out.println ("BASE CLASS - DISPLAY..."); } }; class Dc extends Bc { void display () { super.display (); //refers to base class display method System.out.println ("DERIVED CLASS - DISPLAY..."); } }; class InDemo { public static void main (String k []) { Dc do1=new Dc (); do1.display (); } };
在构造函数中的super关键字
每当我们开发任何继承应用程序时,我们都用来创建始终是最突出的类的对象。
当我们创建底部最派生类的对象时,它又调用了其立即超级类默认构造函数,而且轮到它调用其顶级最超级级默认构造函数。
因此,在Java环境中,将始终从右下角调用构造函数,并且执行从上到下开始。
考虑以下多级继承:
super()用于从默认构造函数或者派生类的参数化构造函数调用超级类默认构造函数。
它是可选的。
super(...)用于从默认构造函数或者派生类的参数化构造函数调用超级类参数化构造函数。
它始终是强制性的。
上述规则的示例:
class Bc { Bc () { System.out.println ("BASE CLASS - DEFAULT CONSTRUCTOR"); } Bc (int x) { this (); System.out.println ("BASE CLASS - PARAMETERIZED CONSTRUCTOR"); } }; class Ibc extends Bc { Ibc () { super (100); System.out.println ("INTERMEDIATE BASE CLASS - DEFAULT CONSTRUCTOR"); } Ibc (int x) { this (); System.out.println ("INTERMEDIATE BASE CLASS - PARAMETERIZED CONSTRUCTOR"); } }; class Dc extends Ibc { Dc () { this (10); System.out.println ("DERIVED CLASS - DEFAULT CONSTRUCTOR"); } Dc (int x) { super (10); System.out.println ("DERIVED CLASS - PARAMETERIZED CONSTRUCTOR"); } }; class StDemo { public static void main (String k []) { Dc do1=new Dc (); } };