Java Tutorials(Object-Oriented Programming Concepts)

Object-Oriented Programming Concepts

Core concepts: objects, messages, classes, and inheritance.

What is an Object?

Real-world objects share two characteristics: 【state】 & 【behavior】
eg: Dog
-> state: name, color, breed, hungry
-> behavior: barking, fetching, wagging tail

Bundling code into individual software objects provides a number of benefits, including:

  1. Modularity(模块化):The source code for an object can be written and maintained independently of the source code for other objects. Once created , an object can be easily passed around inside the system.
  2. Information-hiding(信息隐藏): By interacting only with an object's methods. the details of its internal implementation remain hidden from the outside world.
  3. Code re-use(代码复用): If an object already exists(perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
  4. Pluggability and debugging ease(插件化和易于调试): If a particular object turns out to be problematic, you can simple remove it from you application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire matchine

What is an Class?

A class is blueprint or prototype from which objects are created.

public class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    public int getCadence() {
        return cadence;
    }

    public void setCadence(int cadence) {
        this.cadence = cadence;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getGear() {
        return gear;
    }

    public void setGear(int gear) {
        this.gear = gear;
    }

    public static void main(String[] args) {
        // Create two different Bicycle objects from the same blueprint of Bicycle.
        Bicycle bike01 = new Bicycle();
        Bicycle bike02 = new Bicycle();
        
        bike01.setCadence(50);
        bike01.setSpeed(20);
        
        bike02.setCadence(30);
        bike02.setCadence(15);
    }
}

What is an Inheritance?

Inheritacne provides a powerful and natural mechanism for organizing and structing software.
Object-oriented programming allows classes to inherit commonlly used states and behaviors from other classes.

What is an Inteface?

An interface is a contract(this contract is enforced at build time by the compiler) between a class and outside world.
In its most common form, an interface is a group of related methods with empty bodies.

public interface IBicycle {
    public abstract void changeCadence(int newValue);
    public abstract void changeGear(int newValue);
    public abstract void speedUp(int increment);
    public abstract void applyBrakes(int decrement);
}

What is a Package?

A package is a namespace that organize a set of related classes and interfaces.

  • Bulletpoints
  1. Real-world objects contain states and behaviors
  2. A software object's state is stored in fields
  3. A software object's behavior is exposed through methods
  4. Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ecapsulation
  5. A blueprint for a software object is called a class.
  6. Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword
  7. A collection of methods with no implementation is called an interface
  8. A namespace that organizes classess and interfaces by functionality is called a package
  9. The term API stands for Application Programming Interface
posted @ 2020-08-05 02:12  Felix_Openmind  阅读(257)  评论(0)    收藏  举报
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}