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

Source: Internet
Author: User

Java object-Oriented programming basics


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


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


Encapsulation is the basis of a class that hides the relevant implementation details of a class, and sets the code for the data and implementation operations in the class to be placed inside the object, simply by using the class-reserved interface directly when invoking these classes.

inheritance provides a mechanism for subclasses to automatically own parent class data structures and methods, which represent a relationship between classes.

polymorphism is the technique by which an object is treated as another object, that is, the same operation or function, the procedure can be used on multiple types of objects, and different results are obtained.


2. Classes, objects, methods


class : An abstraction of an object with the same attributes (data elements) and behaviors (functions) is a class. That is, an object's abstraction is a class, the materialization 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 : An action performed by an object is called a method of a class.


3. Construction method and destruction method


A constructor method is a special method of a class that initializes a new object for the class. Each class in Java has a default constructor, which must have the same name as the class name and no return type, and the default return type of the constructed method is the type of the object itself, and the construction method cannot be static/final/synchronized/abstract/ Native decoration.

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

A protected type of Finalize () method is also provided in the Java object class, so any Java class can override this method in which to dispose of the related resources that the object occupies.


In Java, object-oriented programming notes such as:



Finally, show the sample code:

/****************************************************** * "Defining class"--how to define a class-*/public class 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 parameter) 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);}






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.