Java Learning (V): Java Object-Oriented programming basics

Source: Internet
Author: User

Java object-Oriented programming basics


Object Oriented Programming,oop technology is a powerful software development method, which uses data abstraction and information hiding technology to simplify software development to achieve code reuse . The purpose.


1.3 Features of OOP (encapsulation, inheritance, and polymorphism)


Encapsulation is the basis of a class. Hides the relevant implementation details of the class, centralizing the data and the code that implements the operation in the class and putting it inside the object. These classes can be called only by using the interface reserved by the class directly.

inheritance provides a mechanism for subclasses to own the parent data structures and methods of their own initiative. It represents a relationship between classes.

polymorphism directs an object to be seen as a technique that has an object. That is, the same operations or functions, procedures can be used on multiple types of objects, and get different results.


2. Classes, objects, methods


class : An abstraction of an object with the same attribute (data element) and behavior (function) is the class. That is, the object's abstraction is a class. The detail of a class is an object, or an instance of a class is an object. (A class is a conceptual model, and a class is a basic unit for constructing object-oriented programs.)

Object : Everything is an object. (object is a real entity)

method : The operation that the object runs is called a method of the class.


3. Construction method and destruction method


A construction method is a special method of a class. A new object that is used to initialize the class.

Each class in Java has a default construction method that must have the same name as the class name. And there is no return type. The default return type of a constructed method is the type of the object itself, and the construction method cannot be static/final/synchronized/abstract/native decorated.

In contrast to the construction method, when an object is detached from its scope (for example, the object's method has been called to completion), the system itself runs the destructor, and the destructor is often used to clean up the fragments.

A finalize () method of type protected is also available in the object class of Java. So no matter what Java class can override this method, in this method to dispose of the related resources that the object occupies the operation.


In Java, object-oriented programming notes such as:



Finally, the demo sample code is given:

/****************************************************** * "Define class"--how to define a class */public classes oopbasic//declares a class {private String name;//writes the properties of the class private int age;//write the properties of the class public void tell () {//method of writing the class (no return parameters) System.out.println (name+ "+age+" year old!) ");} public int Returnmax (int a,int b) {//method of writing class (with return parameters) return Math.max (A, b);} public static void Main (String [] args) {//main function Oopbasic fa=new oopbasic ();//Create an instance, Object Fa.name= "Fred"; Fa.age=10;fa.tell ( ); int X=12,y=15;int Zuida = Fa.returnmax (x, y); System.out.println ("max=" +zuida);}






Java Learning (V): Java Object-Oriented programming basics

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.