The note introduces the principles of object-oriented programming (OOP) in Java.
Java notes of open courses @Codecademy.
Intro
Java is an object-oriented programming (OOP) language, which means that we can design classes, objects, and methods that can perform certain actions. These behaviors are important in the construction of larger, more powerful Java programs.
Classes
In each part of this section, please pay attention to how to apply those points to the example below!
Syntax
One fundamental concept of object-oriented programming in Java is the class.
A class is a set of instructions that describe how a data structure should behave.
Java provides us with its own set of pre-defined classes, but we are also free to create our own custom classes.
Constructors
If we created a Java class, but it currently does not do anything; we need to describe the behavior of the class for it to be useful.
Let's start by creating the starting state of our class. We can do this by adding a class constructor to it.
A class constructor will allow us to create Car
instances. With a class constructor, we can set some information about the Car
.
If we do not create a class constructor, Java provides one that does not allow you to set initial information.
Instance Variables
We save specific details into instance variables, which model class/object attributes.
Constructor Parameters
For the Car
class, we can specify the initial year by adding parameters to the class constructor.
Parameters allow data types to be created with specified attributes.
Methods
The main Method
Java's built-in main method: public static void main(String[] args) {}
.
When Java runs your program, the code inside of the main
method is executed.
Objects
To use the Car
class, we must create an instance of the Car
class. An instance of a class is known as an object in Java.
Methods I
A method is a pre-defined set of instructions. Methods are declared within a class. Java provides some pre-defined methods available to all classes, but we can create our own as well.
Note that the startEngine
method is created outside of the main
method, like the constructor was.
Now the startEngine
method is available to use on the myFastCar
object. We can do this by calling the method on myFastCar
. Again, this occurs inside of the main
method. Running the program results in printing Vroom!
to the console.
Methods II
In the example below, we create a drive
method that accepts an int
parameter called distanceInMiles
. In the main
method, we call the drive
method on the myFastCar
object and provide an int
parameter of 1628
.
Calling the drive
method on myFastCar
will result in printing Miles driven: 1628
to the console.
Keywords Used in Declaring a Method
-
The
void
keyword (which means "completely empty") indicates to the method that no value is returned after calling that method. -
Alternatively, we can use data type keywords (such as
int
,char
, etc.) to specify that a method should return a value of that type.
Within main
, we called the numberOfTires
method on myFastCar
. Since the method returns an int
value of 4
, we store the value within an int
variable called tires
. We then print the value of tires
to the console.
A Comprehensive Example
//1. Create a custom Car class
class Car {
//3. Using instance variables to model our Car class after a real-life car
int modelYear;
//2. The class constructor for the Car class
//4. Adding parameters
public Car(int year) {}
//7. Creating new method I
public void startEngine() {
System.out.println("Vroom!");
}
//9. Creating new method II
public void drive(int distanceInMiles) {
System.out.println("Miles driven: " + distanceInMiles);
}
//10. Creating new method III with `int`
public int numberOfTires() {
return 4;
}
//5. Java's built-in main method
public static void main(String[] args) {
//6. Creating an object in `Car` class
Car myFastCar = new Car(2007);
//8. Calling the defined method I, II, III
myFastCar.startEngine();
myFastCar.drive(1628);
int tires = myFastCar.numberOfTires();
System.out.println(tires);
}
}
Inheritance
One of the object-oriented programming concepts that allows us to reuse and maintain code more efficiently is called inheritance. It is used to share or inherit behavior from another class. Let's look at an example:
class Car extends Vehicle {
int modelYear;
public Car(int year) {
modelYear = year;
}
//Other methods omitted for brevity...
public static void main(String[] args){
Car myFastCar = new Car(2007)
myFastCar.checkBatteryStatus();
}
}
class Vehicle {
public void checkBatteryStatus() {
System.out.println("The battery is fully charged and ready to go!");
}
}
In the example above, the extends
keyword is used to indicate that the Car
class inherits the behavior defined in the Vehicle
class. This makes sense, since a car is a type of vehicle.
Within the main
method of Car
, we call the checkBatteryStatus
method on myFastCar
. Since Car
inherits from Vehicle
, we can use methods defined in Vehicle
on Car
objects.
Review:
- Class: a blueprint for how a data structure should function
- Constructor: instructs the class to set up the initial state of an object
- Object: instance of a class that stores the state of a class
- Method: set of instructions that can be called on an object
- Parameter: values that can be specified when creating an object or calling a method
- Return value: specifies the data type that a method will return after it runs
- Inheritance: allows one class to use functionality defined in another class
网友评论