can anyone help with this questions? Suppose that we have the following classes:
ID: 3864578 • Letter: C
Question
can anyone help with this questions?
Suppose that we have the following classes: public class ClassA//Line 1 {private int x;//Line 2 protected void setX(int a)//Line 3 {x =;//Line 4}} public class Test//Line 5 {public static void main(String [] args)//Line 6 {ClassA aObject;//Line 7 aObject.setX(4);//Line 8}} Explain the TWO reasons that make the above code incorrect? a) ______________ b) __________ 2. Suppose that Truck and Motorcycle are subclasses of Vehicle, Identify the following declarations as being valid or invalid. lf invalid tell why I. Truck t = new vehicle(); _________ ll. Vehicle v = new Truck(); ________. III. Motorcycle m1 = new vehicle(); IV. Motorcycle m2 = new Truck(); _________Explanation / Answer
A)In Line 7 ClassA aObject;
Here we just created reference and its not pointing to any object.
2)Here the scope of the setX() method is protected.Then , If ClassA is not present in the same package the we can execute the getX() method.Its recommended to mention the scope of the getX() as public.
______________________
2)Ques)
I)Ans) Invalid.because subclass reference is referring to the Super class object.
II)Valid
III)Invalid, .because subclass reference is referring to the Super class object.
iV)Invalid
Reason Motorcycle is not of type Truck.So we cannot refer to the Truck class object by using the reference of Motorcycle object reference.
._________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.