Java Notes (3) - Object-Oriented

2018-03-16  本文已影响8人  SilentSummer

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

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:

External Resources

上一篇下一篇

猜你喜欢

热点阅读