Skip to content

Commit e34136b

Browse files
committed
used Super() constrictor in the inherited class
1 parent 773bf03 commit e34136b

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

Class & Object/Car.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Car(){}
2525

2626
// using super()
2727
public Car(String id, String name, int noOfTyre, String color, double milesPerLitre){
28-
super(id, name, noOfTyre, color, milesPerLitre);
28+
super(id, name, noOfTyre, color, milesPerLitre); // this calls the parent constructor
2929

3030
}
3131
}

Class & Object/Main.java

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class Main {
22
public static void main(String[] args) {
33
/* creating object of the Vehicle class */
4-
Vehicle v1 = new Vehicle("KL46P6556","Devid's car", 4);
4+
Vehicle v1 = new Vehicle("KL46P6556","Devid's Bike", 2);
55

66
// using getter
77
v1.getVehicle();
@@ -10,53 +10,71 @@ public static void main(String[] args) {
1010
v1.setMilesPerLitre(25.7);
1111
v1.setColor("White");
1212

13-
// calling getter after setter
1413
System.out.println("\n/* After using setter */\n");
14+
15+
// calling getter after setter
1516
v1.getVehicle();
1617

17-
System.err.println("\n\n\n Creating new Car obj \n\n\n");
18+
System.out.println("\n\n\n Creating new Car obj \n\n\n");
1819

1920
/* creating new object of the Car class */
2021
Car c1 = new Car("KL17X4678","My Car", 4, "Black", 17.8);
2122

2223
c1.getVehicle();
2324

24-
/* Expected output */
25-
26-
// noOfDriver is set to 1.
25+
/* Calling static method */
26+
Car.setNoOfDriver(2);
2727

28+
System.out.println("\n/* Creating new obj after calling static method */\n");
2829

29-
// ************ KL46P6556 **************
30-
// Name: Devid's car
31-
// No of Tyres: 4
32-
// Color: null
33-
// Miles per Litre: 0.0
34-
// No of Driver: 1
35-
// ============ KL46P6556 ==============
30+
/* new car obj */
31+
Car c2 = new Car("KL17X4678","My Car", 4, "Black", 17.8);
3632

37-
// /* After using setter */
33+
// getters
34+
c2.getVehicle();
35+
36+
}
37+
}
38+
/* Expected output */
3839

39-
// ************ KL46P6556 **************
40-
// Name: Devid's car
41-
// No of Tyres: 4
42-
// Color: White
43-
// Miles per Litre: 25.7
44-
// No of Driver: 1
45-
// ============ KL46P6556 ==============
4640

41+
// noOfDriver is set to 1 using the static block.
4742

43+
// ************ KL46P6556 **************
44+
// Name: Devid's Bike
45+
// No of Tyres: 2
46+
// Color: null
47+
// Miles per Litre: 0.0
48+
// No of Driver: 1
49+
// ************ KL46P6556 ************
4850

49-
// Creating new Car obj
51+
// /* After using setter */
5052

53+
// ************ KL46P6556 **************
54+
// Name: Devid's Bike
55+
// No of Tyres: 2
56+
// Color: White
57+
// Miles per Litre: 25.7
58+
// No of Driver: 1
59+
// ************ KL46P6556 ************
5160

61+
// Creating new Car obj
5262

53-
// ************ KL17X4678 **************
54-
// Name: My Car
55-
// No of Tyres: 4
56-
// Color: Black
57-
// Miles per Litre: 17.8
58-
// No of Driver: 1
59-
// ============ KL17X4678 ==============
63+
// ************ KL17X4678 **************
64+
// Name: My Car
65+
// No of Tyres: 4
66+
// Color: Black
67+
// Miles per Litre: 17.8
68+
// No of Driver: 1
69+
// ************ KL17X4678 ************
70+
71+
// /* Creating new obj after calling static method */
72+
73+
// ************ KL17X4678 **************
74+
// Name: My Car
75+
// No of Tyres: 4
76+
// Color: Black
77+
// Miles per Litre: 17.8
78+
// No of Driver: 2
79+
// ************ KL17X4678 ************
6080

61-
}
62-
}

Class & Object/Vehicle.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Vehicle {
1414
// This block run only at the time of this class file loaded.
1515
// And this is the 1st block which start the execution.
1616
noOfDriver = 1;
17-
System.out.println("\n\nnoOfDriver is set to "+noOfDriver+".\n\n");
17+
System.out.println("\n\nnoOfDriver is set to "+noOfDriver+" using the static block.\n\n");
1818
}
1919

2020
/* Constructors */
@@ -68,7 +68,7 @@ public void getVehicle(){
6868
System.out.println("Color: "+this.color);
6969
System.out.println("Miles per Litre: "+this.milesPerLitre);
7070
System.out.println("No of Driver: "+noOfDriver);
71-
System.out.println("============ "+this.id+" ==============");
71+
System.out.println("************ "+this.id+" ************");
7272
}
7373

7474
// setters

0 commit comments

Comments
 (0)