Object-oriented Java Learning (4)

Source: Internet
Author: User

One of the three main features of Java object-oriented encapsulation, which now says one of the three characteristics of inheritance and abstract class. These are just my personal knowledge, the shortcomings of the Please forgive me.

1, inheritance is one of the three main characteristics of object-oriented, then what is inheritance?

Inheritance refers to the property and method of an object using another object directly, by extends the keyword to create an inheritance relationship between the class and the class. The inherited class is called the parent class, and the class that inherits the parent class is called a subclass (derived class).

 //  parent class  class   Animal { //  protected   String name;  protected  void   Dec () {}}  //  subclass  class   Dog extends animal{System.  out . println ( " name   +name);}   

2. Advantages and disadvantages of inheritance:

Inheritance can improve the reusability of code, and make the relationship between class and class, which provides the precondition for the third characteristic polypeptide. Of course, inheritance has its limitations: only single inheritance can be implemented in Java, that is, a class can have only one parent class. However, Java allows multiple layers of inheritance, where a subclass can have a parent class, and the parent class can have a parent class. and inheritance can inherit only properties and methods that are not private to the parent class. And the construction method cannot be inherited.

3, what is single inheritance, multi-inheritance?

The so-called single inheritance is a subclass that can have only one parent class. Multiple inheritance is a subclass that can have multiple parent classes (not allowed in Java, but can be improved, not directly inherited, because there are likely to be the same members in more than one parent class, generating the uncertainty of the call, embodied in Java through "multi-implementation"). The multi-layered inheritance supported in Java is: C Inherits B,b inherit A, there will be a so-called inheritance system. When using an inheritance system, we should first look at the top floor of the system, understand the basic functions of the system, and second, create the most sub-class object in the system, and complete the use of the function.

4, then when can we define inheritance? Inheritance is defined when a relationship exists between a class and a class. XXX is one of the yyy. XXX extends YYY affiliation: is a relationship.

5, using inheritance can inherit the methods and properties of the parent class, that when we do not need a method in the parent class, or need to be redefined, it is possible to use the overloaded method. That is, overwrite. What do you mean, cover? Now, when a subclass appears with a member function and a parent class, the function in the subclass is run, which is called the overwrite operation. Overriding considerations: First, when overriding the parent class method, the subclass permission must be greater than or equal to the permission in the parent class;

The second is that static methods can only override static, or be statically overwritten. Now that's the problem, when do you use the overwrite operation? When you extend a subclass to a class, the subclass needs to preserve the function declaration of the parent class, but when you define the unique content of the feature in the subclass, you use the Overwrite feature. Following

1 classphone{2 String name;3   voidCall () {}4   voidShow () {}5System. out. println (" Number");6 }7 classNewphone extends Phone {8    voidShow {9System. out. println ("name"+name);Ten super.show (); One     } A  -  -}

6, after inheritance, the characteristics of the constructor in the child parent class: When the subclass constructs the object, when the child class constructor is found, the constructor in the parent class is also running. Why does this happen? The reason is that there is a default implicit statement in the first row in the constructor of the subclass: Super (), and all subclasses in the child class will access the constructor of the null argument in the parent class.

Note: Private methods in the parent class cannot be overwritten, the static method in the parent class cannot be overwritten, and when overridden, the subclass permission must be greater than or equal to the Frey method permission.

7, the definition of properties and methods will use a keyword final: In the program, a data is fixed, direct use is also possible, but poor reading, so give the data a name, and the value of this variable will not change, so with the final decoration fixed, will not change. Instead, use the final keyword to declare a class, a method, and a constant. Of course, a class that uses final declaration cannot be inherited, the declared method cannot be overridden, the declared property is a constant, and can only be assigned once.

 Public  Static  Final  double  my_pi=3.14; Static  int  x=7; void    Method () {   final  int  number=5;}   

8, the concept of abstract class: Many of the same characteristics and behavior of the object can be abstracted as a class, and many have the same characteristics and behavior of the class can be abstracted as an abstract class. Abstract classes are classes declared using the abstract keyword.

Note: A concrete class that inherits an abstract class must implement all the abstract methods in the abstract class.

Declares an abstract class:

1  Public  Abstract  classAnimal {2     PrivateString name;3      Public  Abstract  voidSay ();//abstract method, no implementation body4 5 }6 classCat extends animal{7      Public voidsay () {8System. out. println ("I am a cat!");9       Ten     } One     A}

9, the use of abstract classes must follow the rules: Abstract classes can have no abstract method, but the abstract method of the class must be abstract class, non-abstract class inheriting abstract class must implement all the abstract methods, abstract class can have method implementation and properties;

Abstract classes cannot be instantiated (calling abstract methods are meaningless); Abstract classes cannot be decorated with final. Abstract class has the characteristics of: The method is only the declaration is not implemented, the method is an abstract method, need to be abstract modified. An abstract method must be defined in an abstract class, which must also be an abstract class, and an abstract class must have its subclasses overwrite all the abstract methods before it can be instantiated, or the subclass is an abstract class.

Note: Abstract classes can have constructors for the initialization of subclass objects, and abstract classes can not define abstract methods, but are less common, and are not intended to allow the class to create objects. Usually this class has a method body, but no content, the AWT adapter is this. Abstract keyword abstractions can not coexist with private, static, final keywords. An abstract class must be a parent class, because subclasses need to overwrite all methods in the parent class before they can be instantiated.

10. The similarities and differences between abstract class and general class:

The same point: both abstract and generic classes are used to describe things, and members are defined internally.

Differences: The general class has enough information to describe things, and abstract classes may not have the information to describe things, abstract methods can not be defined in a generic class, only non-abstract methods are defined, and abstract methods may be defined in abstract classes, non-abstract methods can be defined, generic classes can be instantiated, and abstract classes cannot be instantiated.

11, the origin of abstract methods: multiple objects have the same function, but the specific content of the function is different, in the extraction process, only the function definition is extracted, not the functional body, then only the function of the Declaration, no functional body method is called abstract method.

Object-oriented Java Learning (4)

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.