Because many of this programming style is related to objects and classes, we will further examine them in the following. The object of the object uses the object of the object is how when you browse the system analysis document or design documentation Determine them. Because the object is generally representative, local or things, a basic way to determine the object is to identify the nouns used in the sentence. There is a simple example. In the sentence "a customer can have more than one bank Accounts, we have identified two objects, customers and accounts. In the sentence "Kitten 喵 喵", we can determine an object, cat. In front of the class, we have learned a class is how to define how objects And what entities should be included when the object is created or instantiated. In the discussion of animals, we can say that "the dog is called, the cat is called, the duck is called." Determine the object in the sentence I got the dog, cat and duck. As for Wang Wang, 喵, 嘎 叫 叫, it is the behavior movement of our object. To implement these objects, we need to create three objects called Dog, Cat and Duck. To achieve their behavior, we can create a method of sound emitted by each object, and we call this method as Speak or, if we play imagination, you can call this method as Sayello. In the context of the program In order to demonstrate these concepts, let us modify the HelloWorld program in the article, add these three new objects and add a Sayhello method to each of them, as shown below: Public class helloworld {public static void main (String [] ARGS) {DOG Animal1 = New DOG (); Cat Animal2 = New Cat (); Duck Animal3 = New Duck (); Animal1.SayHello (); Animal2.SayHello (); Animal3.SayHello (); Animal3. Sayello ();}} Class DOG { Public void syhello () {system.out.println ("bar");}} class cat {public void sayhello () {system.out.println ("meow");}} Class Duck {public void syhello () { System.out.println ("quack");}} After compiling and running this program, the output should be as follows: Bark Meow Quack Take a look at me Our programs, we immediately noticed something: Each object represents an animal, and each object has realized an identical method, Sayhello. Suppose we want to give more functions and represent the object The method and attribute of the animal refers to. For example, we can add a method to determine that an animal is not breastfeeding, or we add a method to determine whether an animal is not meat. We can add this to each object. Two methods are implemented or we can also use OOP's two most powerful features: inheritance and polymorphism. Because all objects represent an object, we will create a "base class" or "super" Class ", its name is Animal. We can then let our objects inherit the same feature from the Animal class and force each object to implement different features with the Animal class. Java uses the Extends keyword to specify a class from another Inherit. Let us use inheritance and polymorphic concepts to get the benefits of code reuse to rebuild our procedures and make each object only different features with base class animal: public class helloworld {public static void main (String [] args {DOG Animal1 = New Dog (); Cat Animal2 = New Cat (); Duck Animal3 = New Duck (); System.out.Println ("A DOG SAYS" Animal1.getHello () ", IS Carnivors:" Animal1.iscarnivorous ()
", IS A Mammal:" Animal1.isamammal (); System.out.Println ("a cat sign" animal2.gethello () ", IS Carnivors:" Animal2.iscarnivors () ", IS A Mammal: " animal2.isamammmmmmmal ()); System.out.Println (" a Duck Says " Animal3.getHello () ", IS Carnivors: " Animal3.iscarnivors () ", IS A Mammal: " animal3.isAMammal ());}} abstract class Animal {public boolean isAMammal () {return (true);} public boolean isCarnivorous () {return (true);} abstract public String getHello ();} class Dog extends Animal { Public string getHello () {return ("bar");}} class cat extends animal {transn ("meow");}}}}} class duck outnds animal {public boolean isamammal () {Return (false) Public boolean iscarnivors () {return (false);} public string getHello () {Return ("Quack");}} After compiling and running our program, the output should be as follows: a dog say bark, is carnivorous: True, IS A Mammal: True a Cat Says Meow, IS Carnivous: True, IS A Mammal: True A Duck Says Quack, IS Carnivorous: False, IS A Mammal: Fals e Look at our example, you will find that we define a new class called Animal, which defines three ways: isamammal, iscarnivorous, and getHello. You still noticed, we are in front of each existing class declaration These statements have been added. This statement tells the compiler that these objects are subclasses of the Animal class. Because ISAMAMMAL and ISCARNIVORS return TRUE, the DOG and CAT classes do not reinforce - that is, "overload" these two Method. But the duck is neither a mammal is neither a meat, so the DUCK class needs to overload these two methods to return the correct value. All our objects say "Hello" in their own unique way, so they all need to be heavy Carrying a GetHello method. Because each animal says "Hello"