Ben
Ben

Reputation: 11

"The type of a class variable determines which method names can be used with the variable". What does that mean?

I'm currently studying polymorphism but I don't understand the following statements:

"The type of a class variable determines which method names can be used with the variable. However, the object named by the variable determines which definition with the same method name is used."

I'm confused.

Upvotes: 0

Views: 445

Answers (4)

Marco Capo
Marco Capo

Reputation: 256

To put it simple, you will be able to execute methods specific to your class variable and not of the class instance.

In this way you are able to use interfaces as declared class variable and instantiate a new object.

Display display = new TV();

You will be able to execute whatever method Display has, but not the specific method that TV has.

Upvotes: 0

khachik
khachik

Reputation: 28693

Consider the following code (and explanations inside):

class A {
   public void methodA() {
       System.out.println("A -> A");
   }
   public void methodB() {
       System.out.println("A -> B");
   }
}

class B extends A {
   @Override
   public void methodB() {
       System.out.println("B -> B");
   }
   public void methodC() {
       System.out.println("B -> C");
   }
}

A a = new B(); 

// here, the type of variable a determines which methods can be called on that var.
// A declares two methods, methodA and methodB and only those can be called.
// Even a is actually referring to an instance of B which declares methodC
// as well, the call a.methodC() is not valid because a has type A.
a.methodA(); // prints A -> A

// Here, the actual implementation (the object that a refers to) determines 
// which implementation is being called and because the actual object 
// is an instance of B, we get B -> B printed.
a.methodB(); // prints B -> B
// a.methodC(); cannot be called

Upvotes: 1

Eran
Eran

Reputation: 393781

The type of a class variable determines which method names can be used with the variable.

This means that if you have a class Base and a variable of type Base:

Base base = ...

You can call a method

base.method()

only if method() is defined in class Base (or a super-class of Base).

However, the object named by the variable determines which definition with the same method name is used.

This means calling base.method() doesn't always execute method() of class Base.

If, for example, Derived is a class that extends class Base and overrides method() method, then if the actual type of the instance referenced by base is Derived:

Base base = new Derived();

then calling

base.method();

will execute Derived's class implementation of method().

The "type of a class variable" means the static (compile-time) type of a reference variable.

The "object named by the variable" means the dynamic (run-time) type of the instance (object) referenced by the variable.

Upvotes: 1

Ayrton
Ayrton

Reputation: 2303

It means that the methods you can call from an object are limited by the type of that object. For example, let's say you have these classes:

public class Animal {
    public void sayName() {
        System.out.println('animal');
    }
}

public class Cow extends Animal {
    @Override
    public void sayName() {
        System.out.println('cow');
    }

    public void sayMoo() {
        System.out.println('mooo');
    }
}

Now, you can declare your cows like this:

Animal cow = new Cow();

You can do that because Cow is a subclass of Animal. However, if you do that you'll be unable to say moo with your Cow, because you have created the variable as a simple Animal. Therefore, the method sayMoo is unaccessible from your variable.

Upvotes: 1

Related Questions