10. class FourWheeler {11. blank void display() { }12. }13. class Car extends FourWheeler {14. blank void display() { }15. }Which method at line 14, will correctly complete class Car?protected private
Question
- class FourWheeler {11. blank void display() { }12. }13. class Car extends FourWheeler {14. blank void display() { }15. }Which method at line 14, will correctly complete class Car?protected private
Solution
The method at line 14 can be completed correctly using either "protected" or "public".
Here's why:
-
"protected" - This access modifier allows the method to be accessed within the same package and also from the subclass. Since class Car is a subclass of FourWheeler, it can access the display() method if it's marked as protected.
-
"public" - This access modifier allows the method to be accessed from anywhere, including subclasses. So, class Car can also access the display() method if it's marked as public.
"private" would not work because private methods cannot be accessed or overridden by subclasses. They are only accessible within the class they are declared.
Similar Questions
class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}
Add the method to Car class to complete correctly.class FourWheeler {protected FourWheeler getObject(){ //logic with return statement}}class Car extends FourWheeler{ blank}class Driver{public static void main(String args[]){ FourWheeler object = new Car(); object.getObject();}} private FourWheeler getObject() {} protected Car getObject() {} Car getObject() {} protected void getObject() {}
What will be the output of the following program ?class FourWheeler{ public FourWheeler() { System.out.println("Class FourWheeler"); }}class Car extends FourWheeler{ public Car() { System.out.println("Class Car"); }}class Audi extends Car{ public Audi() { super(); System.out.println("Class Audi"); }}class Driver{ public static void main(String args[]) { Audi cc=new Audi(); }}Select one:a.Class AudiClass CarClass FourWheelerb.Compile Time Errorc.Class FourWheelerClass CarClass Audid.Exception occurs
Given:class FourWheeler { public FourWheeler () { System.out.print(1); } } class Car extends FourWheeler{ public Car() { System.out.print(2); } } class Audi extends Car{ public Audi() {System.out.print(3); } } public class Driver{ public static void main( String[] argv ) {new Audi(); } } What is the result when this code is executed?Select one:a.The code runs with no outputb.321c.123d.3
class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}
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.