Java Object and Classes

Java objects and classes



Java as an object - oriented language. Support the following basic concepts:
  • Polymorphism
  • inherit
  • Package
  • abstract
  • class
  • Object
  • Examples
  • method
  • Overloaded
In this section we focus on the concept of objects and classes.
  • Object : The object is an instance of the class (the object is not looking for a girlfriend ), state and behavior. For example, a dog is an object, its state is: color, name, variety; behavior: wagging tail, called, eat and so on.
  • Class : A class is a template that describes the behavior and state of a class of objects.
The following figure boy girl for class, and the concrete one for each class object:

Object in Java

Now let's take a closer look at what is the object. Look around the real world, will 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 is: name, variety, color, behavior are: called, wagging the tail and running.
Contrast real objects and software objects, which are very similar.
The software object also has state and behavior. The state of the software object is the attribute, the behavior is reflected by the method.
In software development, the method of operating the internal state of the object changes, the object of each call is done through the method.

Classes in Java

Classes can be seen as templates 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 : Variables defined in methods, constructors, or statements are called local variables. Variable declaration and initialization are in the method, the method is over, the variable will be automatically destroyed.
  • Member variable : A member variable is a variable that is defined outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by methods in the class, constructors, and block of specific classes.
  • Class variables : Class variables are also declared in the class, outside the method body, but must be declared as static.
A class can have multiple methods, in the above example: barking (), hungry () and sleeping () are the Dog class methods.

Construction method

Each class has a constructor. If the constructor is not explicitly defined for the class, the Java compiler will provide a default constructor for that class.
When creating an object, at least one constructor is called. The name of the constructor must be the same name as the class, and a class can have multiple constructors.
Here is an example of a constructor:
Public class Puppy { public Puppy ( ) { } public Puppy ( String name ) { // This constructor has only one argument: name } }

Create an object

The object is created from the class. In Java, use the keyword new to create a new object. Creating an object requires the following three steps:
  • Declare an object that includes an object name and an object type.
  • Instantiation : Use the keyword new to create an object.
  • Initialization : When creating objects with new, the constructor is called to initialize the object.
Here is an example of creating an object:
Public class Puppy { public Puppy ( String name ) { // This constructor has only one argument: name The System . OUT . The println ( " dog name is: " + name ) ; } public static void main ( String [ ] args ) { // The following statement creates a target Puppy Puppy myPuppy = new Puppy ( " tommy " ) ; } }
Compile and run the above program, will print out the following results:
The name of the dog is : tommy 

Access instance variables and methods

Access member variables and member methods through created objects, as follows:
/ * The object in the object * / ObjectReference = new Constructor ( ) ; / * access the variable * / ObjectReference . VariableName ; / * access the method in the class * / ObjectReference . MethodName ( ) ;

Examples

The following example shows how to access instance variables and call member methods:
Public class Puppy { int puppyAge ; public Puppy ( String name ) { // This constructor has only one argument: name The System . OUT . The println ( " dog name is: " + name ) ; } public void the setAge ( int Age ) { puppyAge = Age ; } public int getAge ( ) { the System . OUT . The println ( " dog's age : " + PuppyAge ) ; return puppyAge ; } Public static void main ( String [ ] args ) { / * create the object * / Puppy myPuppy = new new Puppy ( " Tommy " ) ; / * set by a method Age * / myPuppy . The setAge ( 2 ) ; / * invoke another A method to get age * / myPuppy . GetAge ( ) ; / * You can also access the member variable * / System . Out as follows.the println ( " variable value: " + myPuppy . puppyAge ) ; } }
Compile and run the above program, resulting in the following results:
The name of the dog is : tommy
 The age of the puppy is : 2 Variable value : 2   
  

Source file declaration rule

In the last part of this section, we will learn the rules of the source file. When you define multiple classes in a source file, and there are import statements and package statements, you should pay special attention to these rules.
  • A source file can only have one public class
  • 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, if the class name of the public class in the source file is Employee, the source file should be named Employee.java.
  • If a class is defined in a package, then the package statement should be in the first line 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, then the import statement should be in the source file in the front.
  • The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you can not declare different packages for different classes.
Classes have several access levels, and classes are also of different types: abstract classes and final classes. These are described in the Access Control section.
In addition to the above mentioned types, Java also has some special classes, such as: internal class, anonymous class.

Java package

The package is mainly used to classify classes and interfaces. When developing Java programs, it is possible to write hundreds of classes, so it is necessary to classify classes and interfaces.

Import statement

In Java, if you give a full qualified name, including the package name, class name, then the Java compiler can easily locate 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 . *;

A simple example

In this example, we create two classes: Employee and EmployeeTest .
First open the text editor, paste the following code into it. Note Save the file as Employee.java.
The Employee class has four member variables: name, age, designation, and salary. This class explicitly declares a constructor that has only one argument.

Employee.java File code:

Import java . Io . *; Public class Employee { String name ; int age ; String designation ; double salary ; // Employee class constructor Public Employee ( String name ) { this . Name = name ; } // Set the value of age public void empAge ( int empAge ) { Age = empAge ; } / * set value designation of * / public void empDesignation ( String empDesig ) { designation = empDesig ; } / * set value of the salary column * / public void empSalary ( Double empSalary ) { Salary = empSalary ; } / * Print information * / public void printEmployee ( ) { System . OUT . Println ( " name: " + name ) ; System . OUT . Println ( " Age: " + Age ) ; System . OUT . Println ( " Position: " + Designation ) ; the System . OUT .Println ( " salary: " + salary ) ; } }
The program is executed from the main method. In order to run this program, you must include the main method and create an instance object.
The EmployeeTest class is given below, which instantiates an instance of two Employee classes and calls the value of the method setting variable.
Save the following code in the EmployeeTest.java file.

EmployeeTest.java File code:

Import Java . IO *;. public class EmployeeTest { public static void main ( String args [ ] ) { / * use constructor creates two objects * / the Employee empOne = new new the Employee ( " RUNOOB1 " ) ; the Employee empTwo = new new the Employee ( " RUNOOB2 " ) ; // call the member methods of the two objects empOne . empAge ( 26 ) ; empOne . empDesignation ( " senior programmer " ) ; empOne . empSalary ( 1000 ) ; empOne . printEmployee ( ) ; empTwo . empAge ( 21 ) ; empTwo . empDesignation ( " novice programmer " ) ; empTwo . empSalary ( 500 ) ; EmpTwo . PrintEmployee ( ) ; } }
Compile the two files and run the EmployeeTest class, you can see the following results:
$ Javac EmployeeTest . Java
The Java $ EmployeeTest name: RUNOOB1
 Age: 26 Position: Senior Programmer Salary: 1000.0 name: RUNOOB2
 Age: 21 Position: novice programmer salary: 500.0 





EmoticonEmoticon