ACCP8.0 Java Course second semester-about inheritance

Source: Internet
Author: User
Tags class access modifiers instance method

This chapter focuses on:
The concept of inheritance, overriding and abstract classes
The difficulty of this chapter:
1. Understanding the principle of memory invocation when an object is created when an inheritance relationship exists
2. Understanding Abstract Classes

Learning methods:
1. I think of examples and analysis, with the idea of immediate implementation, do not utopian socialism
2. According to the requirements of the parent class, stator class, found properties, inductive characteristics


1. Inheritance
1.1 Inheritance is one of the object-oriented features, the subclass inherits the parent class, the subclass can have the methods and properties of the parent class, the benefit is to reuse the code, make the result clear, it is the necessary condition of polymorphism
1.2 Class access modifiers that require inheritance cannot be decorated with protected and private
1.3 Object is the parent class of all Java classes, and if a class does not explicitly inherit a class then it inherits the object class by default
1.4 (1) Full inheritance: properties and methods for public and protected adornments (2) Partial Inheritance: properties and methods for the same package with the default modifier decoration
(3) cannot inherit: property and method of private adornment and constructor
1.5 before a subclass object is created, the constructor of the parent class is called from the highest ancestor in turn.
1.6 If the parent class does not provide a default constructor, the constructor of the parent class must be explicitly called with the Super keyword to write to the first row
 PackageOrg.lyrk.accp8.s2.chapter.two;/*** Created by Niechen on 17/4/20.*/ Public classTest1 { PublicTest1 () {System.out.println ("Test1 Constructor"); }     Public Static voidMain (string[] args) {NewTestchildtwo (); }}classTestchildoneextendsTest1 { PublicTestchildone () {System.out.println ("Testchildone Constructor"); }}classTestchildtwoextendsTestchildone { PublicTestchildtwo () {System.out.println ("This is the Testchildtwo constructor."); }}//the access modifier for a subclass cannot be private or protected//protected class Testchildthree extends testchildtwo{////}output: Constructor of Test1 constructor Testchildone This is the constructor of Testchildtwo



The 2.super keyword is a reference to the parent class
2.1 is the keyword that invokes the parent class
2.2 Calling the properties and methods of the parent class
2.3 Calling the construction method of the parent class
2.4 The super and this keyword cannot appear in the constructor at the same time, and the instance method can

3. About rewriting
3.1 You can use the override annotation or not
3.2 Rules:
(1) Method name, return value (or its subclass), method parameters must be consistent.
(2) The access modifier cannot be controlled less than the access control permission of the parent class
(3) cannot throw an exception that is incompatible with the parent class
3.3 The difference between overloading (overload) and overriding (override)
(1) overloading is in a class, overriding in subclasses
(2) Overloading simply requires the method name to remain the same, while overriding is the return value method name parameter is consistent, and the access modifier is best kept consistent
(3) The construction method supports overloading, but the construction method cannot be overridden
 PackageOrg.lyrk.accp8.s2.chapter.two;Importjava.io.IOException;/*** Created by Niechen on 17/4/21.*/ Public classTest3 {

public int age = 20; Public voidtest1 () {} Public voidtest2 () {} Public voidtest3 () {} PublicTest3 test4 () {return NULL; } Public voidTEST5 ()throwsnullpointerexception {}}classTest3childextendsTest3 {//Error The parent class does not throw an exception Public voidTest1 ()throwsexception{}//error return value differs from parent class Public inttest2 () {return0; } Public voidTest3 (inttext) {} @Override Publictest3child test4 () {Super. test4 (); return NULL; } //error not compatible with exception of parent class@Override Public voidTEST5 ()throwsIOException {}}


4. Abstraction
4.1 Abstract class is an incomplete class, so it cannot be instantiated, modified with the keyword abstract
4.2 The ability to define an entity method in an abstract class can also define an abstract method, but a non-abstract class cannot define an abstract method
4.3 Non-abstract subclasses must override abstract methods
4.4 Abstract methods cannot be private
4.5 When a class urgently wants subclasses to override the method, consider defining an abstract class (it is necessary to implement it without knowing how)
1  PackageOrg.lyrk.accp8.s2.chapter.two;2 3 /**
* Abstract examples can act on classes and methods4 * Created by Niechen on 17/4/21.5 */6 Public Abstract classTest4 {7 8 Public Abstract voidtest1 ();9 Ten protected Abstract voidtest2 (); One A Abstract voidtest3 (); - - //Private abstract void test4 (); Abstract methods cannot be private the - Public voidTest5 () { -System.out.println ("This is an example method"); - } + } - + classTest4childextendsTest4 { A at @Override - Public voidtest1 () { - - } - - @Override in protected voidtest2 () { - to } + - @Override the voidtest3 () { * $ }Panax Notoginseng}



5 Final keyword
The 5.1 final class cannot be inherited, there are no subclasses, and the methods in the final class are final by default.
(1) The final method cannot be overridden by a method of a class, but can be inherited.
(2) The final member variable represents a constant, which can only be assigned once, and the value will no longer change after the assignment.
(3) Final cannot be used to modify the construction method.
5.2 Final action on the method:
If a class does not allow its subclasses to overwrite a method, you can declare this method as the final method.
There are two reasons for using the final method:
First, lock the method to prevent any inherited classes from modifying its meaning and implementation.
Second, efficient. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency
When 5.3 final acts on a parameter, it can be read using the parameter, but the value of the parameter cannot be changed.
 PackageOrg.lyrk.accp8.s2.chapter.two;/*** Created by Niechen on 17/4/25.*/ Public Final classTEST5 { Public Static FinalString Test_const = "This is a constant test";  Public Final voidtest1 () {System.out.println ("This is a final method"); }     Public Static FinalString test2 () {return NULL; }     Public voidtest3 () {Final intAge = 10; }     Public voidTest4 (FinalTest3 test3) {       //test3 =new Test3 (); Cannot create an object but can change the properties of an objectTest3.age =100; }}//The final class cannot be inherited//class Test5child extends Test5 {////}

ACCP8.0 Java Course second semester-about inheritance

Related Article

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.