The role of Encapsulation Inheritance (inheritance) Inheritance: To improve code reusability and maintainability software, 1000 dialogs, each of which requires 100 lines of code to be written separately, requires a total of 100,000 lines of code considering that each dialog box has a common portion of 70 lines, with a unique account of 30 rows. 70 lines of code are written once and reused. Total Required:70+30*1000=30,000 rows--How do I write code to implement inheritance? The Font dialog box Paragraph dialog box uses subclasses to inherit the parent class through extends: Superclass, base class, superclass subclass: derived class, inheritance in extension class Java, than C++to be simpler and more C++supports multiple inheritance (a subclass with multiple parent classes), and Java does not support several properties of inheritance: (1all non-private members in the parent class can inherit from the quilt class (the members of the default permission cannot be used outside the package) (2In General, the member that needs to be inherited is defined as protected (3A member that is normally inherited can use inheritance in a subclass as a member of its own special case: (1overwrite (override override) subclasses inherit the members of the parent class, in case the subclass also defines a member of the same type and name as the parent class, will there be any ambiguity? A member of a subclass masks out the parent class member, which is called overwrite (override) Note: Overrides do not allow the access rights of child class members to be strictly overridden by the access rights of the parent class members? You can maintain the personalization of subclasses (2when a relational subclass of a parent class and a subclass is instantiated, the system automatically instantiates the parent class object in advance for that subclass: parent class constructor What if there are parameters? The first sentence of the subclass constructor assigns parameter inheritance to the constructor of the parent class: Improve the reusability and maintainability of code to improve the reusability and maintainability of code in addition to inheritance, there are other ways? There is also a way to combine inheritance is to take members directly from the parent class as I use the combination is to use the functionality of another class to complete this class of tasks in a software, you need to write three classes: the first class: Connect to the network, the information stored in the second class: Connect the network, the information stored in the third class, accept user input To save information to a databaseclassnetworkconn{}classsavetodatabase{}classsavetofile{}classinput{}classc1{calls Networkconn and Savetodatabase}classc2{calls Networkconn and SaveToFile}classc3{call input and savetodatabase} for improved reusability, the combined use of the situation is about%, inherited as 20%The combination is divided into four categories (1) head instantiation, mouth automatic instantiation of the life cycle combination, the strongest couplingclassmouth{}classhead{Mouth m; Head () {m=NewMouth ();}} (2car instantiation, the engine is not instantiated, you can later use the SET function to assemble. Post-Assemblyclassengine{}classcar{Engine E; voidSetengine (Engine e) { This. E =e; }} (3) function-level combinations that can be used only in a functionclassdriver{}classcar{voidDrive (Driver d) { This, db=D; }} (4) function Internal combinationclassdriver{}classcar{voidDrive () {Driver D=NewDriver (); }}
classdialog{protectedString title; voidShow () {System.out.println ("Dialog.show"); }}classFontDialogextendsdialog{//subclass inherits the parent class intsize; protected intShow () {System.out.println ("Fontdialog.show"); } Public Static voidMain (string[] args) {FontDialog fd=NewFontDialog (); Fd.show (); }}
in the rewrite, the return value of our method can not be changed, the words of the person is not a rewrite, we call this method when we will have an error.
classdialog{protectedString title;classFontDialogextendsdialog{//subclass inherits the parent class protectedString title; intsize; protected voidShow () {title= "Font";//refers specifically to the title in a subclass Super. title = "Sadlfkjas;ldf";//refers specifically to the title in the parent class } Public Static voidMain (string[] args) {FontDialog fd=NewFontDialog (); Fd.show (); }}
//relationship of parent class and child classclassdialog{String title; Dialog () {System.out.println ("Dialog Constructor"); }}classFontDialogextendsdialog{//subclass inherits the parent classFontDialog () {System.out.println ("FontDialog Constructor"); } Public Static voidMain (string[] args) {FontDialog fd1=NewFontDialog (); FontDialog FD2=NewFontDialog (); }}
//relationship of parent class and child classclassdialog{String title; Dialog (String title) { This. title =title; System.out.println ("Dialog Constructor"); }}classFontDialogextendsdialog{//subclass inherits the parent classFontDialog () {Super("Font");//assigns a parameter to the parent class constructor, the sentence code//to put in the first sentence of a subclass constructorSystem.out.println ("FontDialog constructor"); } Public Static voidMain (string[] args) {FontDialog fd1=NewFontDialog (); System.out.println (Fd1.title); }}
//relationship of parent class and child classclassdialog{String title; Dialog (String title) { This. title =title; System.out.println ("Dialog Constructor"); }}classFontDialogextendsdialog{//subclass inherits the parent classFontDialog () {System.out.println ("FontDialog Constructor"); Super("Font"); } Public Static voidMain (string[] args) {FontDialog fd1=NewFontDialog (); System.out.println (Fd1.title); }}
Inheritance of Java