Free SCJP ebook!

Free SCJP Mock exams!( Registration required)
The free SCJP ebook
Witscale Test Center

Previous Page Next Page Contents Index

Valid XHTML 1.0!

Method Overloading and Overriding

Overloading
  • If two (or more) methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but different signatures, then the method name is said to be overloaded.
  • The signature of a method consists of the name of the method and the number and types of formal parameters in particular order. A class must not declare two methods with the same signature, or a compile-time error occurs.
  • Only the method name is reused in overloading, so method overloading is actually method name overloading.
  • Overloaded methods may have arguments with different types and order of the arguments may be different.
  • Overloaded methods are not required to have the same return type or the list of thrown exceptions.
  • Overloading is particularly used while implementing several methods that implement similar behavior but for different data types.
  • Overloaded methods are independent methods of a class and can call each other just like any other method.
  • Constructor Overloading
    • Constructors can also be overloaded in similar fashion, as they are also methods.
    • Java has some classes with overloaded constructors. For example, wrapper class for int has two overloaded constructors. One constructor takes int as argument and another takes string argument.

      Integer aNumber = new Integer(2);
      Integer anotherNumber = new Integer("2005");
      
    • One constructor can call another overloaded constructor of the same class by using this keyword.
    • One constructor can call constructor of its super class by using the super keyword.

[More in ebook]

Overriding
  • When a class defines a method with same method name, argument types, argument order and return type as a method in its super class, its called method overriding.
  • The method in the super class is said to be overridden by the method in the subclass.
  • Overriding method actually replaces the behavior in super class for a subclass.
  • Rules for overriding
    • Methods overriding cannot be declared more private than the super class method.
    • Any exceptions declared in overriding method must be of the same type as those thrown by the super class, or a subclass of that type.
    • Methods declared as final cannot be overridden.
    • An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden.
    • Methods declared as private cannot be overridden as they are not visible outside the class.
  • Overriding method can call the overridden method (just like any other method) in its super class with keyword super.
    The call super.method() will invoke the method of immediate super class.
  • Though keyword super is used to refer super class, method call super.super.method() is invalid.
[More in ebook]


Difference between overloading and overriding.

Overloading methods. Overriding method.
  • Overloaded methods supplement each other.
  • Overloading methods may have different argument lists of different types.
  • The return type may be different for overloaded methods
  • Since overloading methods are essentially different methods, there is no restriction on exceptions they can throw.
  • An overriding method replaces the method it overrides.
  • An overriding method must have argument lists of identical type and order.
  • The return type must be same as that of overridden method.
  • The overriding method must not throw checked exceptions which cannot be thrown by the original method.




'Is a', 'has a' relationship

The Generalization (Inheritance) and 'is-a' relationship
Generalization is the result of abstracting the common attributes and behavior from a group of closely related classes and moving them into a common super class. Specialization is the inverse of generalization.
A common test for generalization is the IS-A test. Generalization may also look for following phrases in problem definition to identify the inheritance.
  • "is a"
  • "is a type of"
  • "is a kind of"

For example, if part of problem definition says 'Dog IS AN animal', implementation will more likely have Animal class and Dog class will be the subclass of the Animal class.

The Composition and 'has-a' relationship
Composition is the relationship between a class and its constituent parts.
  • Composition passes the “HAS-A” test.
    Example: An Automobile 'has-a' Engine.
  • Composition also passes the 'life and death' test. The parts have the same life as the whole. Example: A Polygon has points, and the points live and die with the Polygon object.
[More in ebook]
Variables are shadowed and methods are overridden.

When a class member variable is accessed through an object reference, the variable to be selected depends on the declared class of the object reference variable.
However, when a method is called on through an object reference, the actual method invoked depends on the type of an object reference.
example
  • A subclass can have a variable with the same name as a variable in the parent class. This is called as shadowing the parent class variable. It is not overriding of variable.
  • Late Binding

    In any method call, actual method to invoke is determined by the 'Class' (or type) of an object on which the method is called and not on the type of variable holding the object reference. In addition, this is decided at runtime.

[More in ebook]

Related SCJP Objective

Section 6 : Overloading, Overriding, runtime type and object orientation
  1. State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a".
  2. Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods.
  3. Write code to construct instances of any concrete class including normal top-level classes and nested classes.
[More in ebook]

Previous Page Next Page Contents Index Top of this page

Valid CSS!