Method overriding
Upcoming SlideShare
Loading in...5
×
 

Method overriding

on

  • 61 views

Method Overriding

Method Overriding

Statistics

Views

Total Views
61
Views on SlideShare
61
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Method overriding Method overriding Presentation Transcript

  • 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.