Skip to content

Commit 303bd11

Browse files
committed
Interface and Abstract Classes 1
0 parents  commit 303bd11

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
practice_self
2+
General/
3+
*.class
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public abstract class Crashable{
2+
/**variables defined inside abstract need not be constants like in the Interface */
3+
boolean carDrivable = true;
4+
5+
public void youCrashed(){
6+
this.carDrivable = false;
7+
}
8+
9+
public abstract void setCarStrength(int carStrength);
10+
public abstract int getCarStrength();
11+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**Interfaces are basically compact */
2+
public interface Drivable {
3+
double PI = 3.14;
4+
double getSpeed();
5+
void setSpeed(double speed);
6+
7+
/**Whenever we are defining methods inside an interface, they are already abstract methods.
8+
* Meaning: here `void setSpeed(double speed)` is in fact:`public abstract void setSpeed(double speed)`;
9+
*/
10+
11+
int getWheel();
12+
void setWheels(int numWheels);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class NewVehicle{
2+
public static void main(String[] args)
3+
{
4+
5+
/**This is with the parameterized constructor */
6+
Vehicle car = new Vehicle(4,50);
7+
System.out.println("The number of wheels on a car is: " + car.numberOfWheels);
8+
System.out.println("The top speed of the car is: " + car.theSpeed);
9+
System.out.println("The strength of the car is: " + car.carStrength);
10+
11+
/**If it was the default constructor then we have to work like this */
12+
Vehicle bike = new Vehicle();
13+
bike.numberOfWheels = 2;
14+
bike.setSpeed(100);
15+
System.out.println("Number of wheels on a bike is: " + bike.numberOfWheels);
16+
System.out.println("Speed of bike is: " + bike.theSpeed);
17+
18+
/**Use of Abstract Class methods */
19+
car.setCarStrength(100);
20+
System.out.println("The strength of car is: " + car.carStrength);
21+
22+
23+
}
24+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**abstract class goes before Interface that's why we write extends Crashable before implements Interface */
2+
public class Vehicle extends Crashable implements Drivable {
3+
4+
int numberOfWheels = 4;
5+
double theSpeed = 0;
6+
int carStrength = 0;
7+
8+
/**All the methods of Drivable Interface.
9+
* When we are implementing an interface,
10+
* the class should contain all the methods
11+
* of the interface.
12+
*/
13+
public double getSpeed(){
14+
return theSpeed;
15+
}
16+
17+
public void setSpeed(double speed){
18+
this.theSpeed = speed;
19+
}
20+
21+
public int getWheel(){
22+
return numberOfWheels;
23+
}
24+
25+
public void setWheels(int numWheels){
26+
this.numberOfWheels = numWheels;
27+
}
28+
29+
/**Parameterized Constructor */
30+
public Vehicle(int wheels, double speed){
31+
this.numberOfWheels = wheels;
32+
this.theSpeed = speed;
33+
}
34+
35+
/**Default Constructor */
36+
public Vehicle(){
37+
38+
}
39+
40+
/**Derived from Abstract Class */
41+
public void setCarStrength(int carStrength){
42+
this.carStrength = carStrength;
43+
}
44+
public int getCarStrength(){
45+
return this.carStrength;
46+
}
47+
}

Interface and Abstract Class/readme.md

Whitespace-only changes.

0 commit comments

Comments
 (0)