2. Method Overriding
If a method of the subclass has the same
signature(name and parameter list) and return type
as that of the method in super class then we say that
the methods are overrided.
The process is known as “Method Overriding”
The ability of a subclass to override a method allows
a class to modify behavior as needed.
It is also a method of implementing
“polymorphism”
4. Example cont..
public Class OverrideSub extends OverrideSuper
{
void test( )
{
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
a=a+10;
System.out.println(“a: ”+a);
}
}
5. Example Cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
6. Accessing Super class Members
If your method overrides one of its superclass's
methods, you can invoke the overridden method
through the use of the keyword super.
8. Example cont..
public Class OverrideSub extends OverrideSuper
{
void test( )
{
super.test();
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
super.test(20);
a=a+10;
System.out.println(“a: ”+a);
}
}
9. Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
10. Accessing Super class Data Members
Like methods we can have data members in the
subclass having the same name as that of the super
class.
Use super keyword to access the data members as
well.
Example: super.datamember_name
12. Example cont..
public Class OverrideSub extends OverrideSuper
{
int b;
void test( )
{
super.test();
int x,y,z;
x=10;
y=20;
z=x+y;
System.out.println(z);
}
void test(int a)
{
super.test(20);
super.b=30;
a=a+10;
System.out.println(“a: ”+a);
System.out.println(“b: ”+super.b);
}
}
13. Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
o.test( );
o.test(10);
OverrideSub s=new OverrideSub( );
s.test( );
s.test(10);
}
}
14. Lab Task
Use the following class hierarchy:
Shape
Circle Square Triangle
• All of the classes contain a method known as printValues.
• The printValues method of the sub class should call the method of the super
class.
• Note: Use the classes that you have coded in the previous lab.
15. Calling constructor of the super class
The syntax for calling a superclass constructor is
super();
super(parameter list);
With super(), the superclass no-argument
constructor is called. With super(parameter list),
the superclass constructor with a matching
parameter list is called.
Note: Invocation of a superclass constructor
must be the first line in the subclass
constructor.
16. Example
public Class OverrideSuper
{
int b;
OverrideSuper( )
{
System.out.println(“super class”);
}
OverrideSuper(int a)
{
b=a;
System.out.println(“b: ”+b);
System.out.println(“super class”);
}
}
17. Example cont..
public Class OverrideSub extends OverrideSuper
{
int c;
OverrideSub( )
{
super();
c=20;
System.out.println(“sub class”);
}
OverrideSub (int a)
{
super(20);
c=a;
System.out.println(“sub class”);
}
}
18. Example cont..
public class OverrideDriver
{
public static void main(String args[])
{
OverrideSuper o=new OverrideSuper( );
OverrideSuper o=new OverrideSuper(30 );
OverrideSub s=new OverrideSub( );
OverrideSub s=new OverrideSub(40 );
}
}
19. Lab Task 2
Add call to the super class constructor in all the sub
class constructors.