Question: Please explain the function of each code package ElectronicDevice; class Device{ protected String name; protected double price; void display() { System.out.print(Device name: +name+ Price: +price);

Please explain the function of each code

package ElectronicDevice; class Device{ protected String name; protected double price; void display() { System.out.print("Device name: "+name+" Price: "+price); } } class Computer extends Device { String model; } class Printer extends Device { String model; } class Laptop extends Computer { String model; } class Desktop extends Computer { String model; }

b.

Device Dev1 = new Printer(); Dev1.getName();

In the above code Device is the parent class of Printer. And we are creating a parent instance by assigning a child class object into it. So, getName() method should be implemented in the Device class as well.

class Device{ protected String name; protected double price; void display() { System.out.println("Device name: "+name+" Price: "+price); } void getName() { System.out.println("Device name: Parent device"); } }

class Printer extends Device { String model; void getName() { System.out.println("Device name: Child device"); } }

c.

Here, we are creating a parent instance that is Dev1 by assigning a child class object into it. So, If we call the display() method then the method inside the child class will be executed. So, in this case "Child device" will be printed.

TestDevice.java:

package ElectronicDevice; class Device{ protected String name; protected double price; void display() { System.out.println("Device name: "+name+" Price: "+price); } void getName() { System.out.println("Device name: Parent device"); } } class Computer extends Device { String model; } class Printer extends Device { String model; void getName() { System.out.println("Device name: Child device"); } } class Laptop extends Computer { String model; } class Desktop extends Computer { String model; } public class TestDevice {

public static void main(String[] args) { Device Dev1 = new Printer(); Dev1.getName();

}

}

d.

Device Dev1 = new Device(); // Here we are creating the object of Device class Device Dev2 = new Computer(); // Here we are initializing the instasnce of Device class with the // object of Computer class Computer comp1=new Computer(); // Here we are creating the object of Computer class

S, we can say that all of the above declarations are valid.

Device name: Child device

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!