3 March 2010

The difference bewteen overloading and overriding in java: SCJP 6 exam revision

A key concept in object orientation and the java language. You should know this very basic stuff if you're a Java Developer. Here's a summary for newbie's:

Overriding only applies to inhertited methods when a subclass re-implements a superclass's method. An overriding method:

- MUST have the same parameters (arguments) as the method it is overriding
- MUST have the same return type as the method it is overriding, except for the case of co-varinat return in java 5 (class in same heirachy is allowed)
- MUST NOT restrict the access modifier i.e. public can not become private but private can become public
- MUST NOT throw new or checked broader exceptions
- CAN throw fewer or narrower exceptions or any unchecked exception

Overloading refers to methods that share the same name but different parameters. Nothing to do with inherentience. Overloaded methods:

- MUST have different parameters
- CAN have a different return type
- CAN have a different access modifier
- CAN throw different exceptions

No comments:

Post a Comment