Like an instance method, a static method can be inherited. However, a static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

Overriding vs Overloading

  • Overloaded methods supplement each other while an overriding method replaces the method it overrides
  • Overloaded methods can exist in any number in the same class while each method in a base class can be overridden at the most once in any one class
  • Overloaded methods must have different parameter lists while overriding methods must have identical type and order of the parameter list
  • The return type of an overloaded method can be chosen freely while the return type of an overriding method must be identical to that of the method it overrides


Single method call behaves differently depending on the type of the object which the call references. We can store a reference to a derived class object in a variable of the derived class type AND we can also store it in a variable of any direct or indirect base class. To be able to support polymorphic behavior, there are a few requirements as given below:

  • The method call for the derived class object must be through a variable of a base class object
  • The method called must also be a member of the base class
  • The method being used must satisfy all requirements for overridden methods

Example 1:

Rectangle r = new Rectangle();
Ellipse e = new Ellipse();
Shape s = r;

Example 2:

public class PolymorphismDemo {
  public static void main(String[] args) {
    m(new GraduateStudent());
    m(new Student());
    m(new Person());
    m(new Object()); }
  public static void m(Object x) {
class GraduateStudent extends Student {

class Student extends Person {
  public String toString() {
    return "Student";  }

class Person extends Object {
  public String toString() {
    return "Person";  }

Casting Objects

You have already used the casting operator to convert variables of one primitive type to another. Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. In the preceding section, the statement

m(new Student());

assigns the object new Student() to a parameter of the Object type. This statement is equivalent to:

Object o = new Student(); // Implicit casting

Why Casting Is Necessary?

Suppose you want to assign the object reference o to a variable of the Student type using the following statement:

Student b = o;

A compilation error would occur. Why does the statement Object o = new Student() work and the statement Student b = o doesn’t? This is because a Student object is always an instance of Object, but an Object is not necessarily an instance of Student. Even though you can see that o is really a Student object, the compiler is not so clever to know it. To tell the compiler that o is a Student object, use an explicit casting. The syntax is similar to the one used for casting among primitive data types. Enclose the target object type in parentheses and place it before the object to be cast, as follows:

Student b = (Student)o; // Explicit casting
Tagged with: ClassesJAVAObject Oriented

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.