530 likes | 1.48k Views
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 )
E N D
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 ) 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
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
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 ); } }
Overloading Constructors public class TopClass { public TopClass( String s, int i ) {} public TopClass( float f ) {} }
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
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.
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; }
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
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(); } }
Continued…. Public static void main( String args[] ) { OuterOne.InnerOne i = new OuterOne().new InnerOne(); }
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
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 } } }
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