2

Given:

class TestA {
    public void start() { System.out.println(”TestA”); }
}
public class TestB extends TestA {
    public void start() { System.out.println(”TestB”); }
    public static void main(String[] args) {
        ((TestA)new TestB()).start();
    }
}

What is the result?

A. TestA

B. TestB

C. Compilation fails.

D. An exception is thrown at runtime.

The answer I gave to this question was B

What is the advantage of typecasting that is done at line 7 as we know that Method to be called depends on the actual object type.can anyone please give an example where the typecasting comes into play???????

Community
  • 1
  • 1
shirsendu shome
  • 385
  • 1
  • 5
  • 4

5 Answers5

7

There is no "advantage" here: it just seems like the casting was introduced to test one's knowledge of OOP concepts and inheritance, particularly that which you have already answered, i.e. the method to be called depends on the actual subtype of an object.

casablanca
  • 69,683
  • 7
  • 133
  • 150
3

rewriting the code so that it is more similar, and the casting removed:

class TestA
{
    public void start()
    {
        System.out.println("TestA");
    }
}

class TestB
    extends TestA
{
    public void start()
    {
        System.out.println("TestB");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        TestA a;
        TestB b;

        a = new TestA();
        a.start();  // TestA

        a = new TestB();
        a.start();  // TestB

        b = new TestB();
        b.start();  // TestB
    }
}

Putting the casts in here would give you something like:

public static void main(String[] args)
{
    TestA a;
    TestB b;

    a = new TestA();
    ((TestA)a).start();  // TestA

    a = new TestB();
    ((TestA)a).start();  // TestB

    b = new TestB();
    ((TestA)b).start();  // TestB
}

The casts do nothing to alter the runtime behaviour. They are only useful at compile time.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
1

It will print a TestB.

And that is why:

Firstly, we create a TestA class with it's own start() method. Then, we create a TestB, which extends TestA (TestB is a subversion("child") of TestA), that overrides his parent's method start().

Every TestB object is also an instance of TestA, but not vice versa. So when we create TestA obj = (TestA)new TestB(), obj will be unable to use TestB-specific method, but it still will use overrided TestB methods instead of native TestA's.

That's it.

Here the typecasting comes into play:

you may use ((TestB)obj).testBMethod(...), to call TestB-specific method. Better use of this will be:

if(obj instanceof TestB)
{
    ((TestB)obj).testBMethod(...);
}
TofuBeer
  • 60,850
  • 18
  • 118
  • 163
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
  • 1
    Not down voting either... but it the is the formatting (or lack of it), not the language used that is the issue. – TofuBeer Oct 22 '10 at 05:55
0

Please note that method to be called depends on the actual object type, not on the return type. Here new TestB() creates the object of TestB and this object is of return type TestA. But the object is still of TestB.

Here you have overridden the start() method in the subclass i.e. superclass and subclass start() method have the same signature(name, parameter list). Then you have instantiated an object of the subclass and type casted it to the super class type. Then using the type casted reference variable(which is now of super class type) you have invoked the start() method. In such a scenario it is the object that the reference variable is pointing to (which in this case is the subclass object) and not the type of the reference variable that decides the which version of the start() is invoked. Therefore the subclass start() is being called. If you want to invoke the superclass start() method you would have instantiate a superclass object and use it to call the start() method.

san1deep2set3hi
  • 4,904
  • 1
  • 23
  • 23
0

Typecasting comes into play during overloading. Method overloading in java is done at compile-time and overriding at run-time.

public class Overloading {
static class A {
    public void start(){
        System.out.println("Overloading$A.start");
    }
}

static class B extends A {
    @Override
    public void start() {
        System.out.println("Overloading$B.start");
    }
}


private static void test(A a) {
    System.out.println("A");
}

private static void test(B b) {
    System.out.println("B");
}

public static void main(String[] args) {
    A b = new B();
    b.start();
    test(b);
}

}

PS: instead of test(b) you could use test((A)b) if b reference was of type B.

Alex Nikolaenkov
  • 2,505
  • 20
  • 27