[Java] Method call error when inheritance and interface implementation are performed at the same time

A reminder about the errors that occurred during Java training.

Code and compilation errors

Sample.java


abstract class A{
    abstract void aMethod();
} 

interface B{
    void bMethod();
}

//A inheritance
class OnlyA extends A{
    public void aMethod(){};
}

//A inheritance / B implementation
class AB extends A implements B{
    public void aMethod(){};
    
    public void bMethod(){};
}

public class Main {
    public static void main(String[] args){
        A[] a = new A[]{new OnlyA(), new AB()};

        //Abstract class method call
        a[0].aMethod();
        a[1].aMethod();

        //Interface method call
        a[1].bMethod();
    }
}

/*
Main.java:26: error: cannot find symbol
        a[1].bMethod();
            ^
  symbol:   method bMethod()
  location: class A
*/

Could not call.

Solution

Main.java


public class Main {
    public static void main(String[] args){
        A var1  = new AB();
        B var2  = new AB();
        AB var3 = new AB();
        
        var1.bMethod(); //This is no good
        var2.bMethod(); //This is okay
        var3.bMethod(); //This is also okay
    }
}

In this case, it seems that you have to declare it with the interface type or the type that implements it.

I thought I was accessing the method of the instance class AB extends A implements B directly, but since I declared it with class A, I wonder if I'm accessing that first?

In the case of inheritance only / implementation only, both parent and child are defined, so it is unlikely that cannot find symbol will appear, but if inheritance and implementation are performed at the same time like this, you have to be careful. (I haven't used the interface so much, but now it's a good idea)

I felt that I needed to study more.

Recommended Posts

[Java] Method call error when inheritance and interface implementation are performed at the same time
Implementation method of linking multiple images to one post and posting at the same time
[ev3 × Java] Interface, implementation and inheritance (event handling)
Behavior noticed when adding RadioButton and initial check at the same time in code
Form and process file and String data at the same time with Spring Boot + Java
Call the super method in Java
[Spring Boot] Post files and other data at the same time [Axios]
Draw a bar graph and a line graph at the same time with MPAndroidChart
Java unit test Same day judgment ArDate, Date, Calendar, long ignore the time and judge whether they are the same day
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
How to delete the tweet associated with the user when you delete it at the same time
[Spring Boot] POST file array / list and other data at the same time [Axios]
Environment construction method and troubleshooter at the time of joint development (rails, docker and github)
[Java] [javaSliver] The interface and abstract classes are complicated, so I will summarize them.