-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance.java
66 lines (56 loc) · 1.58 KB
/
Inheritance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javavideos;
/*
* This java file is supposed to be an example of how to use inheritence.
*/
//creates a worker class
class Worker
{
//creates local worker id an worker number variables
int workID;
int workNumber;
//rate of pay method that will be overriden
public void rateOfPay()
{
//outputs a workers rate of pay
System.out.println("This worker is paid $12 an hour");
}
//a set id method to set the id number
public void setID(int idNum){
workID = idNum;
}
//getid method to get the id number
public int getID(){
return workID;
}
}
//NewWorker class that extends the worker class
class NewWorker extends Worker
{
//rate of pay method that will override the method of the same name in the
//worker method
public void rateOfPay()
{
//outputs a workers rate of pay
System.out.println("This worker is paid $8 an hour");
}
}
//main method
class WorkerTest
{
public static void main(String args[])
{
Worker firstGuy = new Worker(); //worker object but using worker class
Worker secondGuy = new NewWorker(); //worker object but using the NewWorker class
firstGuy.rateOfPay();//calls the rate of paymethod for the first object
secondGuy.rateOfPay();//calls the rate of paymethod for the second object
//sets the second guy object id
secondGuy.setID(234);
//gets the objects id
System.out.println(secondGuy.getID());
}
}