Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance and overloading in Java

I'm studying for an exam and I need some help to understand what is going on in the following snipped of code.

class A {
    public void method1(A X) {
        System.out.println("A");
    }
}
class B extends A {
    public void method2() {
        System.out.println("B");
    }
}
class C extends B {
    public void method1(A x) {
        System.out.println("C");
    }
}
class D extends C {
    public void method1(D x) {
        System.out.println("D");
    }
}
public class test {
    public static void main(String[] args) {
        C c = new D();
        B b = c;
        c.method1(c);  // prints C
        b.method1(b);  // prints C
    }
}

Ok this is what I think: c.method1(c) invokes method1 in C and not method1 in D because c is decleard as a C therefore method1 in D is not visible. But b.method1(b) is more difficult. B does not have method1 and I assumed that method1 in the superclass will be used, but it is not. Why is it using the method in C? I put a new D in b but nothing of the specialization of D is visable, because b is from the type B.

like image 324
Asker Avatar asked Sep 28 '15 11:09

Asker


People also ask

What is inheritance in overloading?

Overloading allows several function definitions for the same name, distinguished primarily through different argument types; it is typically resolved at compile-time. Inheritance allows subclasses to define more special versions of the same function; it is typically resolved at run-time.

Can we overload inherited method?

To override an inherited method, the method in the child class must have the same name, parameter list, and return type (or a subclass of the return type) as the parent method. Overloading a method is when several methods have the same name but the parameter types, order, or number are different.

What is the difference between overriding and inheritance in Java?

Inheritance is a mechanism in which one object acquires all the properties and behaviours of parent object. Inheritance represents the IS-A relationship. If subclass (child class) has the same method as declared in the parent class, it is known as method overriding.

What is the inheritance of Java?

In Java, inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.


1 Answers

In summary, here's the visibility of each method at each inheritance level:

class D:

public void method1(D x) { System.out.println("D"); }
public void method1(A x) { System.out.println("C"); }
public void method2()    { System.out.println("B"); }

class C:

public void method1(A x) { System.out.println("C"); }
public void method2()    { System.out.println("B"); }

class B:

public void method1(A x) { System.out.println("A"); }
public void method2()    { System.out.println("B"); }

class A:

public void method1(A x) { System.out.println("A"); }

It's also important to point out that both variables 'c' and 'b' in your example, are the same object, and is a instance of D.

So... If you call c.method1(c);, it prints "C" because D is an instance of A (it's actually a D, but it's also an A by inheritence), so you can call method1(A), which for D, prints "C". (that's a mouth full). Why doesn't it print "D" you'll ask? Because the variable is declared as a C, which the compiler can only link to method1(A).

If you call b.method1(b);, it prints "C" because your variable b is actually an instance of a D, since you created it as new D(). Actually c and b point to the same object of type D.

When a method is called, the JVM looks at the actual type of the object D in this case, and not what it's declared as B.

A good way to remember is when you have something like

B b = new D()

The left part of the equation is mostly used by the compiler. Remember, method1(A) and method1(D) are two different methods (because not the same exact signature, different argument types).

The right part of the equation is used by the JVM at runtime. It defines the actual type, at runtime, of that variable b.

like image 55
mprivat Avatar answered Oct 25 '22 14:10

mprivat