Skip to content

Commit 38cdf07

Browse files
authored
Merge pull request #40 from realpython/oop-in-java-vs-python
Add support files for OOP in Java vs Python article
2 parents f27b086 + dcb61a0 commit 38cdf07

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

oop-in-java-vs-python/Car.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Car class
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*
6+
* Requires both Vehicle.java and Device.java
7+
*/
8+
9+
public class Car extends Vehicle implements Device {
10+
11+
private int year; // Year of our car
12+
private int voltage; // Battery voltage
13+
private static int wheels; // How many wheels do we have
14+
15+
/**
16+
* Car constructor
17+
*/
18+
public Car(String color, String model, int year) {
19+
super(color, model);
20+
this.year = year;
21+
this.voltage = 12;
22+
}
23+
24+
/**
25+
* Override the interface method.
26+
*/
27+
@Override
28+
public int getVoltage() {
29+
return voltage;
30+
}
31+
32+
/**
33+
* getYear returns the Car's year
34+
*/
35+
public int getYear() {
36+
return year;
37+
}
38+
39+
/**
40+
* setYear changes Car's year
41+
*/
42+
public void setYear(int year) {
43+
this.year = year;
44+
}
45+
46+
/**
47+
* getWheels returns the number of wheels
48+
*/
49+
public static int getWheels() {
50+
return wheels;
51+
}
52+
53+
/**
54+
* setWheels sets the number wheels
55+
*/
56+
public static void setWheels(int count) {
57+
wheels = count;
58+
}
59+
60+
/**
61+
* Return a human readable string reoresenting the Car
62+
*/
63+
public String toString() {
64+
return "Car: " + getColor() + " : " + getModel() + " : " + getYear();
65+
}
66+
}

oop-in-java-vs-python/Device.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Device Interface
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*/
6+
public interface Device {
7+
8+
/**
9+
* getVoltage returns the current battery voltage
10+
*/
11+
int getVoltage();
12+
}

oop-in-java-vs-python/Vehicle.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Vehicle class
3+
*
4+
* Used for the Real Python OOP in Java vs Python article
5+
*
6+
* Implements basic functionality for our Vehicle
7+
*/
8+
public class Vehicle {
9+
10+
private String color; // What color are we?
11+
private String model; // What model vehicle are we?
12+
13+
/**
14+
* Vehicle constructor
15+
*/
16+
public Vehicle(String color, String model) {
17+
this.color = color;
18+
this.model = model;
19+
}
20+
21+
/**
22+
* getColor returns the Vehicles color
23+
*/
24+
public String getColor() {
25+
return color;
26+
}
27+
28+
/**
29+
* setColor changes the Vehicles color
30+
*/
31+
public void setColor(String color) {
32+
this.color = color;
33+
}
34+
35+
/**
36+
* getModel returns the Vehicles model
37+
*/
38+
public String getModel() {
39+
return model;
40+
}
41+
42+
/**
43+
* setModel changes the Vehicles model
44+
*/
45+
public void setModel(String model) {
46+
this.model = model;
47+
}
48+
}

oop-in-java-vs-python/car.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""Car, Device, and Vehicle classes
2+
3+
Used to demonstrate object oriented techniques in Java vs Python
4+
"""
5+
6+
7+
class Vehicle:
8+
"""The Vehicle class is the parent for all vehicles."""
9+
10+
def __init__(self, color, model):
11+
"""Define the color and model of our vehicle"""
12+
self.color = color
13+
self.model = model
14+
15+
16+
class Device:
17+
"""The Device class defines objects which have a battery."""
18+
19+
def __init__(self):
20+
"""Define the base voltage for our device."""
21+
self._voltage = 12
22+
23+
24+
class Car(Vehicle, Device):
25+
"""The Car class is both a Vehicle and a Device."""
26+
27+
wheels = 0
28+
29+
def __init__(self, color, model, year):
30+
"""Call our parent classes, then define the year."""
31+
Vehicle.__init__(self, color, model)
32+
Device.__init__(self)
33+
self.year = year
34+
35+
def add_wheels(self, wheels):
36+
"""Change the number of wheels we have."""
37+
self.wheels = wheels
38+
39+
@property
40+
def voltage(self):
41+
"""Allow us to access the Device._voltage property as voltage"""
42+
return self._voltage
43+
44+
@voltage.setter
45+
def voltage(self, volts):
46+
"""Warn the user before resetting the voltage"""
47+
print("Warning: this can cause problems!")
48+
self._voltage = volts
49+
50+
@voltage.deleter
51+
def voltage(self):
52+
"""Warn the user before deleting the voltage"""
53+
print("Warning: the radio will stop working!")
54+
del self._voltage
55+
56+
def __str__(self):
57+
"""Improved human readable version of the object"""
58+
return f"Car {self.color} : {self.model} : {self.year}"
59+
60+
def __eq__(self, other):
61+
"""Do these objects have the same year?"""
62+
return self.year == other.year
63+
64+
def __lt__(self, other):
65+
"""Which object was made earlier than the other?"""
66+
return self.year < other.year
67+
68+
def __add__(self, other):
69+
"""Add the objects together in our predefined way."""
70+
return Car(
71+
self.color + other.color,
72+
self.model + other.model,
73+
int(self.year) + int(other.year),
74+
)

0 commit comments

Comments
 (0)