Start object-oriented programming using Java

Source: Internet
Author: User
Use Java to start Object-Oriented Programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. What does a language mean by object-oriented? If a programming language is a real object-oriented language, it must support the following features:

Encapsulation-hide Implementation Details

Polymorphism-ability to send the same message to different objects and make each object respond to the message in a predetermined manner

Inheritance-extends the existing classes to generate a proprietary class to inherit the status and behavior of the original class.

Dynamic binding-the ability to send messages to objects without knowing the specific types of objects during programming

Let's take a look at how Java supports these features and how it provides additional features to make the transition from procedural programming to object-oriented development easier.

Object-oriented features in Java

Java is an object-oriented (OOP) Programming Language released by Sun Microsystems In The Middle Of 1990s. you can download the latest Java Development Kit (JDK) from Sun's website ). java is an explanatory language, which means that its source program is first compiled into the form of intermediate code and then interpreted by the virtual machine before each running, it is an object-oriented programming language.

Java hides the complexity and obfuscation of many traditional object-oriented programming languages, such as C ++ and Object Pascal. for example, Java will automatically clear the reference type for programmers without pointers, and all variables will be automatically initialized to the appropriate default values. in addition to the original data type, everything in Java is an object. When necessary, it can even provide encapsulation for the original data type.

Object Introduction

Objects are software programming entities that represent physical objects in real life, such as bank accounts, computer users, buttons on user interfaces, window menus, and so on. objects are defined by their statuses and behaviors. for example, a bank account has a status, such as the current revenue and expenditure status, the account owner, and the minimum allowed transaction amount. Its behavior includes extraction, deposit, balance of payments, etc.

The state of an object is defined by variables that only the object knows. java calls these variables data fields or member variables. data domains are private to objects unless they are explicitly defined using keywords to make them visible to other classes. we will discuss the scope of variables later.

The action of an object is defined by the operation above. in Java, these operations are called methods. the method can change the state of an object, create a new object, and implement practical functions.

Class

A class is an entity that defines the running mode of an object and the data contained when the object is created or instantiated. the role of a class is like a template. One or more objects can be created based on it. the following is an example of using the Java object-oriented concept to declare the HelloWorld application:

Public class HelloWorld

{

Private
String helloMsg = "Hello World! ";

Public
Static void main (String [] args)

{

HelloWorld hw = new HelloWorld ();

}

Public
HelloWorld ()

{

// Display our "Hello"
World "Message

System. out. println (helloMsg );

}

}

The preceding example defines a template. The real HelloWorld object can be created from this template.
Static void main (String [] args) This line begins with a strange code. this Code defines a special method main, which is actually the entry point of our HelloWorld program, the above program is a typical demonstration of how all Java applications define their entry points. note that even this main entry point is encapsulated in the class. in this example, we encapsulate it in the HelloWorld class. the above program shows how to define a class, HelloWorld, and a data field, helloMsg and the two methods main and HelloWorld. the HelloWorld method is a special method called constructor. we will discuss the details and differences of common methods, constructor and static member functions in subsequent articles.

In Java, all source code related to a special class is written in a suffix with the same name as the class. java file. the Java compiler reads the source files and translates them into platform-independent, binary-format code into byte code. Then, the code is classified and stored in the same name as the class but suffixed. class file. you will eventually get a class file for each class.

Compile and run our example Program

Once you have downloaded the JDK from Sun's Web site and installed it on your machine, you can compile and run the Java program. compile and run our example program, paste the code of the HelloWorld class into your favorite document editor, and save the file as HelloWorld. java, and then, at the command prompt, change the current path to the path containing the file. now you can compile the program by typing the following command at the command line prompt:

Windows:

<你的jdk所在目录> \ Bin \ javac HelloWorld. java

UNIX or Linux:

<你的jdk所在目录> /Bin/javac HelloWorld. java

This command will generate a new file named HelloWorld. class in the same directory. To run this program, enter the following command at the command prompt:

Windows:

<你的jdk所在目录> \ Bin \ java HelloWorld

UNIX or Linux:

<你的jdk所在目录> /Bin/java HelloWorld

You should see Hello World on the screen!

Summary

We have gained some knowledge about object-oriented programming using Java programming language. next time, we will analyze our example program, add more features to it, and discuss more related objects, class and other basic concepts of object-oriented programming and how to implement them using Java.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.