JAVA Learning (5): Java Object-Oriented Programming basics, java Object-Oriented Programming

Source: Internet
Author: User

JAVA Learning (5): Java Object-Oriented Programming basics, java Object-Oriented Programming

Java Object-Oriented Programming Basics


Object oriented programming (OOP) is a powerful software development method.Data abstractionAndInformation HidingTechnology to simplify software development to achieveCode reuse.


1. Three features of OOP (encapsulation, inheritance, and polymorphism)


EncapsulationIs the basis of the class. It refers to hiding the implementation details of the class, and putting data and code for implementation operations in the class inside the object, when calling these classes, you only need to directly use the class reserved interface.

InheritanceProvides a mechanism for subclass to automatically own the data structure and method of the parent class, which indicates a relationship between classes.

PolymorphismThe technology that instructs an object to be regarded as another object. That is, the same operation or function and process can act on multiple types of objects and obtain different results.


2. Classes, objects, and methods


Class: The abstraction of objects with the same features (data elements) and behaviors (functions) is a class. That is, the object is abstracted as a class, the class is embodied as an object, or the instance of the class is an object. (Class is a conceptual model, and class is the basic unit for constructing Object-oriented Programs)

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

MethodThe operation performed by an object is called a class method.


3. constructor and constructor


Constructor is a special class method used to initialize a new object of the class. Each class in JAVA has a default constructor. It must have the same name as the class name and has no return type. The default return type of the constructor is the object type, the constructor cannot be modified by static/final/synchronized/abstract/native.

The Destructor is opposite to the constructor. when an object is out of its scope (for example, the method where the object is located has been called), the system automatically executes the destructor, the Destructor is often used to clear fragments.

The finalize () method of the protected type is also provided in the Object class of JAVA. Therefore, any java class can override this method, in this method, release the resources occupied by the object.


Notes for Object-Oriented Programming in java are as follows:



Finally, the sample code is provided:

/*************************************** * *************** "Definition class" -- how to define a class */public class Oopbasic // declare a class {private String name; // write the class attribute private int age; // write the class attribute public void tell () {// compile the class method (no return parameter) System. out. println (name + "this year" + age + "years old! ");} Public int returnMax (int a, int B) {// compile the class method (with return parameters) return Math. max (a, B);} public static void main (String [] args) {// main function Oopbasic fa = new Oopbasic (); // create an instance, that is, the 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.