forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceDemo.java
More file actions
55 lines (46 loc) · 1.28 KB
/
InheritanceDemo.java
File metadata and controls
55 lines (46 loc) · 1.28 KB
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
package com.thealgorithms.oopconcepts;
class Vehicle {
int tyres;
String colour;
String companyName;
void setTyres(int tyres) {
this.tyres = tyres;
System.out.println("Number of tyres: " + tyres);
}
void setColour(String colour) {
this.colour = colour;
System.out.println("Colour of vehicle: " + colour);
}
void setCompanyName(String companyName) {
this.companyName = companyName;
System.out.println("Company name: " + companyName);
}
}
class Truck extends Vehicle {
int wheels = 8, headlights = 2;
String model;
Truck(String model) {
this.model = model;
System.out.println("This is a truck: " + model);
}
}
class Motorcycle extends Vehicle {
int wheels = 2, headlights = 1;
String model;
Motorcycle(String model) {
this.model = model;
System.out.println("This is a motorcycle: " + model);
}
}
public class InheritanceDemo {
public static void main(String[] args) {
Motorcycle mc = new Motorcycle("Dream");
mc.setColour("Black");
mc.setTyres(2);
mc.setCompanyName("Honda");
Truck tk = new Truck("Tata Ultra");
tk.setTyres(8);
tk.setColour("White");
tk.setCompanyName("Tata");
}
}