Java Objects and classes

Source: Internet
Author: User

Java, as an object-oriented language, supports the following basic concepts

Polymorphism, inheritance, encapsulation, abstraction, class, object, instance, method, overload.

In this section we focus on the concepts of objects and classes.

Object, object is a strength of class, having state and behavior, class such as, a dog is an object, its state has color, name, breed; the behavior has: WAG tail, bark, eat etc

Class, a class is a template that describes the behavior and state of a class of objects.

Objects in Java,

Now let me know what is the object, look around the real world, back to find a lot of objects around, cars, dogs, people and so on. All of these objects have their own state and behavior.

Take a dog for example, its state has a name, variety, color, behavior is called, wagging its tail and running.

Compare display objects and software objects, which are very similar.

The software object also has the state and the behavior, the software object's state is the attribute, the behavior manifests through the method.

In software development, the internal state of the method operation object is changed, and the object invocation is done by means of the method.

Classes in Java

Class can be seen as a template for creating Java objects.

Use the following simple class to understand the definition of a class in Java;

 Public class dog{    String breed;     int Age ;    String color;     void Barking () {            }    void  hungry () {            }    void  sleeping (){            }}

A class can contain the following types of variables;

Local variables, in methods, constructed methods or variables defined in a huge amount of money become local variables, variable declaration and initialization are in the method, the end of the method, the variable will be automatically destroyed.

Member variables, which are variables defined in the class, outside the method body, are instantiated when the object is created, and the member variables can be used to chant methods in the class, construct methods, and access the block of statements for a particular class.

Class variables are also declared in the class, outside the method body, but must be declared as static types.

A class can have multiple methods, in the example above, barking (), hungry (), and sleeping (), are all methods of the dog class.

Construction method

Each class has a constructor method, and if you do not explicitly define a construction method for the class, the Java compiler will provide a constructor method for the class.

When creating an object, at least one constructor is called, and the name of the constructed method must be the same as the class, and a class can have more than one constructor method.

The following is an example of a construction method;

class puppy{    public  Puppy () {    }     public  Puppy (String name) {         // This constructor has only one parameter: name     }}

Creating objects

object is created from a class, in Java, using the keyword new to create a new object, creating the object requires the following three steps;

Declaration, which declares an object, including the object name and the object type.

Instantiate, using the keyword new to create an object.

Initializes the constructor method to initialize the object when the object is created using new.

Here is an example of creating an object:

 Public class puppy{    public  Puppy (String name) {        System.out.println ("Puppy's name is:" +name);            }     publicstaticvoid  main (Sting [] args) {        Puppy  New Puppy ("Tommy");}    }

Compile and run the above program and print out the following results

The puppy's name is: Tommy

Azimuth instance variables and methods

Access member variables and member methods through the objects you have created, as shown below;

/** *new  Constructor (); /*  */objectreference.variablename; /*  */Objectreference.methodname ();

Instance

The following example shows how to position an instance variable and invoke a member method;

 Public classpuppy{intPuppyage;  PublicPuppy (String name) {//This constructor has only one parameter; nameSyetem.out.println ("Puppy's name is:" +name); }     Public voidSetage (intAge ) {Puppyage=Age ; }     Public intGetage () {System.out.println ("Puppy's Age:" +puppyage); returnPuppyage; }     Public Static voidmain (String [] args) {//Creating ObjectsPuppy Mypuppy =NewPuppy ("Tommy"); //use the method to set ageMypuppy.setage (); //called to make a method get the ageMypuppy.getage (); //You can also access member variables as followsSYSTEM.OUT.PRINTLN ("Variable Value:" +mypuppy.puppyage); }}
Source file claim rules

In the final part of this section, we will learn the claim rules for the source file. When you define multiple classes in one source file, and you also have import statements and package statements, pay particular attention to these rules.

    • There can be only one public class in a source file
    • A source file can have multiple non-public classes
    • The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.
    • If a class is defined in a package, then the packages statement should be in the first row of the source file.
    • If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
    • The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.

The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in the Access Control section.

In addition to some of the types mentioned above, Java also has some special classes, such as: Inner class, anonymous class.

Java Package

Packages are primarily used to classify classes and interfaces. When developing Java programs, you might write hundreds or thousands of classes, so it is necessary to categorize classes and interfaces.

Import statement

In Java, if you give a full qualified name, including the package name, the class name, the Java compiler can easily navigate to the source code or class. The import statement is used to provide a reasonable path so that the compiler can find a class.

For example, the following command line will command the compiler to load all classes under the Java_installation/java/io path

Import java.  Io. *;   

Java Objects and classes

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.