PREVIOUS
SUPER:
super keyword refers to the base
class variable or method.
Rules:
********
1. super keyword is used only within a subclass
2. super() must be the
first statement executed inside a subclass
constructor
3. The argument in the super
() must match the argument in the constructor defined in the superclass.
Types of super:
1. super. Variable à eg: super.a
2. Super (argument-list) à eg: super(a,b)
3. super .method à eg: super.fun ( )
/* super variable */
class s
{
String x="java";
}
class p extends s
{
String x="linux";
void dis()
{
System.out.println("this
is base string : " + super.x);
System.out.println("
this is derived : " +x);
}
}
class super_var
{
public static void
main(String ar[])
{
p p1=new p();
p1.dis();
}
}
// super
function name
class f
{
void dis(String s)
{
System.out.println("this
is f class string :"+s);
}
}
class g extends f
{
void dis(String s)
{
super.dis(s);
System.out.println("this
one is derived class string :"+s);
}
}
class super_fun
{
public static void
main(String ar[])
{
g g1=new g();
g1.dis("hello");
}
}
//super
feature(constructor)
class mano
{
int i;
mano(int p)
{
this.i=p;
}
}
class vino extends mano
{
int j;
vino(int p,int j)
{
super(p);
this.j=j;
}
}
class super_fea
{
public static void
main(String s[])
{
vino v=new vino(10,20);
System.out.println("this
is constructor val i :"+v.i);
System.out.println("this
is constructor val j :"+v.j);
}
}
No comments:
Post a Comment