Observe the below code :abstract public class Phone { private String model; private double cost;}public class SmartPhone extends Phone{ private String cameraDetails;}Which of the below are valid declarations ?Select one or more:Phone phoneObj = new SmartPhone();SmartPhone phoneObj = new SmartPhone();SmartPhone phoneObj = new Phone();SmartPhone phoneObj = new Phone();Phone ph = (Phone)phoneObj;Phone phoneObj = new Phone();Phone phoneObj = new SmartPhone();SmartPhone smartph = (SmartPhone) phoneObj
Question
Observe the below code :abstract public class Phone { private String model; private double cost;}public class SmartPhone extends Phone{ private String cameraDetails;}Which of the below are valid declarations ?Select one or more:Phone phoneObj = new SmartPhone();SmartPhone phoneObj = new SmartPhone();SmartPhone phoneObj = new Phone();SmartPhone phoneObj = new Phone();Phone ph = (Phone)phoneObj;Phone phoneObj = new Phone();Phone phoneObj = new SmartPhone();SmartPhone smartph = (SmartPhone) phoneObj
Solution
The valid declarations are:
- Phone phoneObj = new SmartPhone();
- SmartPhone phoneObj = new SmartPhone();
- Phone ph = (Phone)phoneObj;
- Phone phoneObj = new SmartPhone();
- SmartPhone smartph = (SmartPhone) phoneObj;
Explanation:
- Phone phoneObj = new SmartPhone(); is valid because a superclass reference variable can refer to a subclass object.
- SmartPhone phoneObj = new SmartPhone(); is valid because a subclass reference variable can refer to a subclass object.
- Phone ph = (Phone)phoneObj; is valid because we can cast a subclass object to a superclass.
- Phone phoneObj = new SmartPhone(); is valid because a superclass reference variable can refer to a subclass object.
- SmartPhone smartph = (SmartPhone) phoneObj; is valid because we can cast a superclass object to a subclass if the object actually holds a subclass instance.
The invalid declarations are:
- SmartPhone phoneObj = new Phone(); is invalid because a subclass reference variable cannot refer to a superclass object.
- Phone phoneObj = new Phone(); is invalid because Phone is an abstract class and we cannot create an instance of an abstract class.
Similar Questions
public abstract interface Insurance{public void insuranceDescription(String s);} Which is the correct class? Select one:a.public abstract class Car implements Insurance{ public abstract void insuranceDescription (String s) { }}b.public abstract class Car implements Insurance { }c.public class Car extends Insurance{public void insuranceDescription (Integer i) { }}d.public class Car implements Insurance{public void insuranceDescription (Integer i) { }}
Which statements are most accurate regarding the following classes? class A { private int i; protected int j; } class B extends A { private int k; protected int m; } Group of answer choicesAn object of B contains data fields i, j, k, m.An object of B contains data fields j, k, m.An object of B contains data fields j, m.An object of B contains data fields k, m.
Observe the below code. You have a class InsurancePolicy and another class VehicleInsurancePolicy that inherits InsurancePolicy.InsurancePolicy.javapublic class InsurancePolicy { private int policyNumber; private String holderName; private double amount; double calculateBonusAmount(){ //some logic return 0; } }VehicleInsurancePolicy.javaclass VehicleInsurancePolicy extends InsurancePolicy { private String vehicleNumber; private int noOfMonths; double calculateBonusAmount(){ //some logic return 0; }}Which access specifier can be given to the overridden method calculateBonusAmount.
Observe the below code : abstract public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } }Identify the correct statements.Select one:a.Code compiles successfullyb.code will not compile, because abstract class cannot have a constructorc.code will not compile because the abstract class do not have abstract methods
e correct answerUsing which of the following, multiple inheritance in Java can be implemented?
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.