So let's create superclass BusinessCompany that has two instance variables: name, numOfWorkers and one method profit(...):
class BusinessCompany {
String name;
int numOfWorkers;
public double profit(double x) {
double m = Math.pow(x, 2);
return m;
}
}Let's create three subclasses: Company1, Company2, Company3. These three classes have their names, number of workers and functions of profit growth. Company1 and Company 3 have equal functions of profit growth: (such as BusinessCompany), for example, let's – number of orders:
So Company1:
class Company1 extends BusinessCompany {
String name = "Adaptec";
int numOfWorkers = 143;
}Company3:
class Company3 extends BusinessCompany {
String name = "Adobe Systems";
int numOfWorkers = 1175;
}Company2 has another function of profit growth . Method profit() should be overridden by this subclass:
class Company2 extends BusinessCompany {
String name = "AECOM";
int numOfWorkers = 171;
public double profit(double x) {
double m = Math.pow(x, 4);
return m;
}
}So we have superclass and three subclasses. Every subclass can use instance variables and methods of superclass. And let's create a main class:
class MainC {
public static void main(String[] args) {
Company1 A = new Company1();
Company2 B = new Company2();
Company3 C = new Company3();
System.out.println(A.name+"; "+A.numOfWorkers+"; "+A.profit(32));
System.out.println(B.name+"; "+B.numOfWorkers+"; "+B.profit(12));
System.out.println(C.name+"; "+C.numOfWorkers+"; "+C.profit(22));
}
}
Comments