In Java, you have an interface IFlyable with a method Fly(). You have a class Airplane that implements this interface. Which of the following is the correct way to declare the Fly() method in the Airplane class?a.public void Fly() { /*...*/ }b.internal void Fly() { /*...*/ }c.private void Fly() { /*...*/ }d.protected void Fly() { /*...*/ }
Question
In Java, you have an interface IFlyable with a method Fly(). You have a class Airplane that implements this interface. Which of the following is the correct way to declare the Fly() method in the Airplane class?a.public void Fly() { /.../ }b.internal void Fly() { /.../ }c.private void Fly() { /.../ }d.protected void Fly() { /.../ }
Solution
The correct way to declare the Fly() method in the Airplane class that implements the IFlyable interface in Java is:
a. public void Fly() { /.../ }
Explanation: In Java, when a class implements an interface, it needs to provide the implementation for all the methods declared in the interface. These methods must be declared as public. This is because all methods in an interface are implicitly public and abstract. So, the implementing class cannot reduce the visibility of the methods. Therefore, options b, c, and d are incorrect.
Similar Questions
Choose the correct statement about the following code: 1: public interface CanFly { 2: void fly(); 3: } 4: interface HasWings { 5: public abstract Object getWindSpan(); 6: } 7: abstract class Falcon implements CanFly, HasWings { 8: } *1 pointA. It compiles without issue.B. The code will not compile because of line 2.C. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of lines 2 and 5.F. The code will not compile because the class Falcon doesn’t implement the interface methods.
Which of the following is the correct way to declare a method in Java?Question 20Answera.int myMethod( ) { }b.void myMethod { }c.myMethod( ) { }d.int myMethod;
Fill the appropriate code to create an object for Flight.public class Flight { public static void main(String a[]) { Flight flightObj; blank ; }}flightObj=new Flight(); Flight flightObj=new Flight();
What is the output of the following application?package flying;class Rotorcraft {protected final int height = 5;abstract int fly();}public class Helicopter extends Rotorcraft {private int height = 10;protected int fly() {return super.height;}public static void main(String[] unused) {Helicopter h = (Helicopter)new Rotorcraft();System.out.print(h.fly());}}A. 5B. 10C. The code does not compile. D. The code compiles but produces a ClassCastException at runtime.Question 3AnswerBCAD
What is the result of the following code? 1: public abstract class Bird { 2: private void fly() { System.out.println("Bird is flying"); } 3: public static void main(String[] args) { 4: Bird bird = new Pelican(); 5: bird.fly(); 6: } 7: } 8: class Pelican extends Bird { 9: protected void fly() { System.out.println("Pelican is flying"); } 10: } *1 pointA. Bird is flyingB. Pelican is flyingC. The code will not compile because of line 4.D. The code will not compile because of line 5.E. The code will not compile because of line 9.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.