Method Overriding
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”
Example
public Class OverrideSuper
{
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
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);
}
}
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);
}
}
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.
Example
public Class OverrideSuper
{
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
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);
}
}
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);
}
}
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
Example
public Class OverrideSuper
{
int b;
void test( )
{
System.out.println(“No parameters”);
}
void test(int a)
{
System.out.println(“a: ”+a);
}
}
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);
}
}
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);
}
}
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.
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.
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”);
}
}
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”);
}
}
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 );
}
}
Lab Task 2
 Add call to the super class constructor in all the sub
class constructors.

Method overriding

  • 1.
  • 2.
    Method Overriding  Ifa 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”
  • 3.
    Example public Class OverrideSuper { voidtest( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 4.
    Example cont.. public ClassOverrideSub 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 classOverrideDriver { 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 classMembers  If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
  • 7.
    Example public Class OverrideSuper { voidtest( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 8.
    Example cont.. public ClassOverrideSub 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 classOverrideDriver { 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 classData 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
  • 11.
    Example public Class OverrideSuper { intb; void test( ) { System.out.println(“No parameters”); } void test(int a) { System.out.println(“a: ”+a); } }
  • 12.
    Example cont.. public ClassOverrideSub 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 classOverrideDriver { 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  Usethe 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 ofthe 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 { intb; 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 ClassOverrideSub 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 classOverrideDriver { 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.