Assume that class Item has following two private data fields: private String name; private int code; and following constructor: public Item(String n, int c) { name = n; code = c; } Now, we want to write a class called ElectronicItem that inherits the properties of class Item as follows having only one data field: public class ElectronicItem extends Item { private double price; The constructor of class ElectronicItem can be written as follows: public ElectronicItem(String n, int c, double p) { this(n, c); price = p; } public ElectronicItem (String n, int c, double p) { Item.super(n, c); price = p; } public ElectronicItem (String n, int c, double p) { Item.name = n; Item.code = c; price = p; } public ElectronicItem(String n, int c, double p) { super(n, c); price = p; }
Assume that class Item has following two private data fields:
private String name; private int code;
and following constructor:
public Item(String n, int c) { name = n; code = c; }
Now, we want to write a class called ElectronicItem that inherits the
properties of class Item as follows having only one data field:
public class ElectronicItem extends Item {
private double price;
The constructor of class ElectronicItem can be written as follows:
-
public ElectronicItem(String n, int c, double p)
{ this(n, c); price = p; }
-
public ElectronicItem (String n, int c, double p)
{ Item.super(n, c); price = p; }
-
public ElectronicItem (String n, int c, double p)
{ Item.name = n; Item.code = c; price = p; }
-
public ElectronicItem(String n, int c, double p)
{ super(n, c); price = p; }
Step by step
Solved in 2 steps