1 / 14

Overloading and Overriding

Overloading and Overriding. Overloading reusing the same method name with different arguments or a different return type Overriding using the same method name with the same number and type of arguments. Overloading Method Names. Examples: 1. public void exampleMethod( int a, int b )

bailey
Télécharger la présentation

Overloading and Overriding

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overloading and Overriding • Overloading • reusing the same method name with different arguments or a different return type • Overriding • using the same method name with the same number and type of arguments

  2. Overloading Method Names • Examples: 1. public void exampleMethod( int a, int b ) 2. public int exampleMethod( int a, int b ) 3. public float exampleMethod( float a, float b ) 4. public void exampleMethod() 5. Public void exampleMethod( int i, int j ) // same as #1 • Compiler figures out which method to call by looking at the arguments

  3. Method Overriding • Relates only to subclassing • Replaces the method that it overwrites • Each method can be overridden once in any subclass • Must have identical name, arguments, and return type

  4. Invoking Overridden Methods and Constructors and Subclassing • Use of super.xxx() • Example: public class BaseObject { public BaseObject( int arg 1 ) {} } public class Object1 extends BaseObject { public Object1( int arg2 ) { super( arg2 ); } }

  5. Overloading Constructors public class TopClass { public TopClass( String s, int i ) {} public TopClass( float f ) {} }

  6. Constructor Rules • Can only create an object if the argument list matches one that is defined in the class itself • If you provide no constructor then the compiler provides a default one, if you define even one then there is no default provided by the compiler • Body is delayed until super classes have been initialized therefore calls to super() must be on the first line of the constructor

  7. this • this is a final variable that holds reference to the object in which it exists (i.e. this points to the current object) • The type of this is the reference type of the object • It is sometimes necessary to pass a reference to the current object as a parameter to another method.

  8. Example public class StackNode { private Object data; private StackNode next, prev; public StackNode( Object o ) { this( o, null ); } public StackNode( Object o, StackNode n ) { data = o; next = n; } public StackNode getNext() { return next; }

  9. Inner Classes • Inner, or Nested, classes are standard classes declared within the scope of a standard top-level class. • There are different kinds of inner class • nested top-level class • member class • local class • anonymous class

  10. Enclosing this reference and Construction of Inner Classes public class OuterOne { int x; public class InnerOne { int y; public void innerMethod() {} } public void OuterMethod{} public void makeInner{ InnerOne anInner = new InnerOne(); } }

  11. Continued…. Public static void main( String args[] ) { OuterOne.InnerOne i = new OuterOne().new InnerOne(); }

  12. Static Inner Classes • Does not have a reference to an enclosing instance • Therefore they cannot access instance variables of the enclosing class • They can only access static variables

  13. Classes Defined Inside Methods public void aMethod( final String s, int i ) { int a = 1; final int b = 2; class InsideMethod { public void method() { // can access s and b // cannot access i and a } } }

  14. Anonymous Classes • Class that does not have a name • Used mostly when working with the AWT public void MethodOne() { theButton.addActionListener( new ActionListener { public void actionPerformed( ActionEvent e ) { // code goes here • class is instantiated and declared in the same place • Cannot have constructors

More Related