Home Page > Learning the Java Language > Language Basics > Variables

Home Page > Learning the Java Language > Object-Oriented Programming Concepts

kitt posted @ 2014年1月06日 12:29 in Java , 11888 阅读




Software objects have state(状态) in fields (variables) and expose its behavior through methods(方法). 

Data encapsulation = Hiding internal state and requiring all interaction to be performed through an object's methods.

Software objects benefits:

Modularity: The source code for an object can be written and maintained independently of the source code for other objects.

Information-hiding: Internal implementation details remain hidden

Code re-use

Pluggability and debugging ease: Remove a problematic object from your application and plug in a different one.



现实世界中很多对象属于同一类, your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.


class BicycleDemo {

    public static void main(String[] args) {


        // Create two different 

        // Bicycle objects

        Bicycle bike1 = new Bicycle();

        Bicycle bike2 = new Bicycle();


        // Invoke methods on 

        // those objects

















cadence:50 speed:10 gear:2

cadence:40 speed:20 gear:3



Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In Java, each class has one direct superclass, and each superclass can have an unlimited number of subclasses.


class MountainBike extends Bicycle {


    // new fields and methods defining 

    // a mountain bike would go here




This gives MountainBike all the same fields and methods as Bicycle.


An interface is a group of related methods with empty bodies (no implementation). 


interface Bicycle {


    //  wheel revolutions per minute

    void changeCadence(int newValue);


    void changeGear(int newValue);


    void speedUp(int increment);


    void applyBrakes(int decrement);



To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle). To actually compile the ACMEBicycle class, you'll need to add the public keyword to the beginning of the implemented interface methods. 

class ACMEBicycle implements Bicycle {


    // remainder of this class 

    // implemented as before



Interfaces form a contract(合约) between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.



A package is a namespace that organizes a set of related classes and interfaces. Conceptually packages=different folders on your computer. Java提供了一个class library叫"Application Programming Interface", or “API”,里面有常用的包,比如String, File, Socket, GUI objects. The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods supplied by the Java SE platform.


登录 *

loading captcha image...
or Ctrl+Enter