-1

According to this answer i tried to implement it like this

Abstract class (Pilot):

public abstract class Pilot{
    abstract void fly();
}

Interface (Safety Officer):

public interface ISafetyOfficer {
        void controlSafety();
}

Child class:

public class C141 extends Pilot implements ISafetyOfficer{
    void fly(){ 
        System.out.println("I'm flying C-141.");
    }

    public void controlSafety(){
        System.out.println("I'm control safety.");
    }

and in main() of Main class that cannot call controlSafety() method

public class Main {
    public static void main(String[] args) {
        Pilot pilot = new C141();
        pilot.fly();
        pilot.controlSafety(); // Cannot call controlSafty here.
    }
}

The problem occurred in function main(). Can anyone tell me why i can't call controlSafety() method in OOP way.

Community
  • 1
  • 1
KKKKK
  • 13
  • 1
  • 5
  • If you want to use the `controlSafety()` method using a Pilot reference, the Pilot class has to implement the `ISafetyOfficer` interface or define the method itself. As the Pilot class is abstract, you don't need to provide and implementation on the Pilot class but every non-abstract subclass of Pilot will require to provide an implementation for the method. – David SN Jan 28 '15 at 11:45

5 Answers5

1

Basically, because the controlSafety is not one of the characteristics of the the Pilot class. so when you trying to call the controlSafety from a Pilot declared object, the compiler checks if its declared inside the Pilot class, if not it not allow you to call it.

So C141 is a Pilot also is a ISafetyOfficer.

But the Pilot is not ISafetyOfficer.

Salah
  • 8,567
  • 3
  • 26
  • 43
0

You can cast the C141 to an ISafetyOfficer and then call it. Java is not a duck typed language.

Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37
0

Because you upcast the C141 class to the Pilot abstract class. But Pilot class does not have controlSafety() method.

Softengilker
  • 173
  • 1
  • 3
  • 11
0

Pilot class don't have method controlSafety() that's why compiler don't find that method.

Pilot pilot = new C141() It is polymorphism but only method present in Pilot class will be call not from method interface ISafetyOfficer get called.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
0

The left hand/reference variable of the statement Pilot pilot = new C141(); determines what methods can be called on the object assigned to it. In this case its of type Pilot, and since Pilot class does not have method controlSafety() it cannot be invoked using reference of type Pilot. You can do a workaround in such cases by type casting:-

  Pilot pilot = new C141();
  pilot.fly();
  C141 c=(C141)pilot;
  c.controlSafety();
  //or
  ISafetyOfficer isafe=(ISafetyOfficer)pilot;
  isafe.controlSafety();

Also from OOP pov , the pilot is meant to fly and not to control safety , thats what Isafety officer is for, hence you cannot tell pilot to do controlSafety. The ideal way should be:-

  Pilot pilot = new C141();
  pilot.fly();
  ISafetyOfficer isafe=new C141();
  isafe.controlSafety();
Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28