《inheritance》由会员分享,可在线阅读,更多相关《inheritance(21页珍藏版)》请在金锄头文库上搜索。
1、Inheritance,2,Inheritance,Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass The derived class is called the child class or subclass. Creates an is-a relationship The subclass is a more specific version of th
2、e Original (Remember has-a is aggregation.),Inheritance,The child class inherits the methods and data defined for the parent class To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones Software reuse is at the heart of inheritance By using exist
3、ing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software,4,Deriving Subclasses,In Java, we use the reserved word extends to establish an inheritance relationship,class Dictionary extends Book / class c
4、ontents ,Dictionary webster = new Dictionary(); webster.message(); webster.defMessage();,public class Book protected int pages = 1500; public String message() System.out.println(“Number of pages: ” + pages); ,public class Dictionary extends Book private int definitions = 52500; public void defMessag
5、e() System.out.println(“Number of definitions” + definitions); System.out.println(“Definitions per page: ” + (definitions/pages); ,Number of pages: 1500,Number of definitions: 52500,Definitions per page: 35,Some Inheritance Details,An instance of a child class does not rely on an instance of a paren
6、t class Hence we could create a Dictionary object without having to create a Book object first Inheritance is a one-way street The Book class cannot use variables or methods declared explicitly in the Dictionary class,7,The protected Modifier,Visibility modifiers determine which class members are in
7、herited and which are not Variables and methods declared with public visibility are inherited; those with private visibility are not But public variables violate the principle of encapsulation There is a third visibility modifier that helps in inheritance situations: protected,8,The protected Modifi
8、er,The protected modifier allows a member of a base class to be inherited into a child Protected visibility provides more encapsulation than public visibility does the best possible encapsulation that permits inheritance,9,The super Reference,Constructors are not inherited, even though they have pub
9、lic visibility Yet we often want to use the parents constructor to set up the “parents part“ of the object The super reference can be used to refer to the parent class, and often is used to invoke the parents constructor,The super Reference,A childs constructor is responsible for calling the parents
10、 constructor The first line of a childs constructor should use the super reference to call the parents constructor The super reference can also be used to reference other variables and methods defined in the parents class,public class Book protected int pages; Book(int numPages) pages = numPages; ,p
11、ublic class Dictionary private int definitions; Dictionary(int numPages, int numDefinitions) super(numPages); definitions = numDefinitions; ,Multiple Inheritance,Java supports single inheritance, meaning that a derived class can have only one parent class Multiple inheritance allows a class to be de
12、rived from two or more classes, inheriting the members of all parents Collisions, such as the same variable name in two parents, have to be resolved Java does not support multiple inheritance In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead (will dis
13、cuss later),13,Overriding Methods,When a child class defines a method with the same name and signature as a method in the parent class, we say that the childs version overrides the parents version in favor of its own. Signature: methods name along with number, type, and order of its parameters The n
14、ew method must have the same signature as the parents method, but can have a different body The type of the object executing the method determines which version of the method is invoked,Overriding,A parent method can be invoked explicitly using the super reference If a method is declared with the fi
15、nal modifier, it cannot be overridden The concept of overriding can be applied to data and is called shadowing variables Shadowing variables should be avoided because it tends to cause unnecessarily confusing code,public class Book protected int pages; Book(int numPages) pages = numPages; public voi
16、d message() System.out.println(“Number of pages: ” + pages); ,public class Dictionary protected int definitions; Dictionary(int numPages, int numDefinitions) super(numPages); definitions = numDefinitions; public void message() System.out.println(“Number of definitions” + definitions); System.out.pri
17、ntln(“Definitions per page: ” + (definitions/pages); super.message(); ,16,Overloading vs. Overriding,Dont confuse the concepts of overloading and overriding Overloading deals with multiple methods with the same name in the same class, but with different signatures Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature Overloading lets you define a similar operation in different ways for different data Overriding lets you define a similar operation in different ways for different object types,