-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34_Inheritance
More file actions
41 lines (37 loc) · 1.05 KB
/
34_Inheritance
File metadata and controls
41 lines (37 loc) · 1.05 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
//inheritance= the process where one class acquires,
// the attributes and methods of another.
<Main.java>
public class Main {
public static void main(String[] args) {
//inheritance= the process where one class acquires,
// the attributes and methods of another.
Carr car = new Carr();
//car.go(); //>>This vehicle is moving
Bicycle bike = new Bicycle();
//bike.stop(); //>>This vehicle is stopped
System.out.println(car.speed); //>>0.0
System.out.println(bike.speed);//>>0.0
System.out.println(car.doors);//>>4
System.out.println(bike.pedals);//>>2
}
}
<Vehicle.java> : Parent class
public class Vehicle {
double speed;
void go(){
System.out.println("This vehicle is moving");
}
void stop(){
System.out.println("This vehicle is stopped");
}
}
<Carr.java> : Child class
public class Carr extends Vehicle{
int wheels =4;
int doors = 4;
}
<Bicycle.java> : Child class
public class Bicycle extends Vehicle{
int wheels = 2;
int pedals = 2;
}