Presentation is loading. Please wait.

Presentation is loading. Please wait.

The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.

Similar presentations


Presentation on theme: "The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor."— Presentation transcript:

1 The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor to set up the "parent's part" of the object The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor The reserved word super can be used in a class to refer to its parent class. Using the super reference, we can use a parent’s members. Like the this reference, what the word super refers to depends on the class in which it is used. However, unlike the this reference, which refers to a particular instance of a class, super is a general reference to the members of the parent class.

2 One use of the super reference is to invoke a parent’s constructor
One use of the super reference is to invoke a parent’s constructor. The Words2 class instantiates a derived class and invokes its inherited and local methods.

3 Book2 is the parent of the class Dictionary2 which we will see in a moment. Book2 and Dictionary2 have explicit constructors used to initialize their instance variables. Book2 represents a book. It is used as the parent of a derived class to demonstrate inheritance and the use of the super reference.

4 Dictionary2 represents a dictionary, which is a book
Dictionary2 represents a dictionary, which is a book. It is used to demonstrate the use of the super reference. The Dictionary2 constructor takes tow integer values as parameters for the number of pages and definitions in the book. The Book2 class already has a constructor that sets up the parts of the dictionary that were inherited. But we cannot invoke it directly, so we use the super reference to get to it in the parent class. The Dictionary2 constructor than initializes its definitions variable. A child’s constructor calls its parent’s constructor. Generally, the first line of a constructor uses the super reference call to a constructor of the parent class. Otherwise Java will automatically make a call to super at the beginning of the constructor. This way a parent class always initializes its variables before the child class constructor begins to execute. We can only use the super reference to invoke a parent’s constructor in the children’s constructor. If we do this, the super reference must be the first line of the constructor. The super reference can also be used to reference other variables and methods defined in the parent’s class.

5 The super Reference A child’s constructor is responsible for calling the parent’s constructor The first line of a child’s constructor should use the super reference to call the parent’s constructor The super reference can also be used to reference other variables and methods defined in the parent’s class

6 Multiple Inheritance Java supports single inheritance, meaning that a derived class can have only one parent class Multiple inheritance allows a class to be derived 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

7 Overriding Methods A child class can override the definition of an inherited method in favor of its own The new method must have the same signature as the parent's method, but can have a different body The type of the object executing the method determines which version of the method is invoked When a child class defines a method with the same name and signature as a method in the parent class, we say that the child’s version overrides the parent’s version in favor of its own. Overriding happens often in inheritance.

8 Overloading vs. Overriding
Don't 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

9 Class Hierarchies A child class of one parent can be the parent of another child, forming a class hierarchy Business Walmart Macys ServiceBusiness Great Clips Retail Business A child class can be the parent of its own child class. What’s more, many classes can be created from a single parent. There is no limit to the number of children a class can have or to the number of levels a class hierarchy can have. Two children of the same parent are called siblings. Although siblings share the characteristics of their common parent, they are not related by inheritance because one is not used to create the other.

10 Class Hierarchies Two children of the same parent are called siblings
Common features should be put as high in the hierarchy as is reasonable An inherited member is passed continually down the line Therefore, a child class inherits from all its ancestor classes There is no single class hierarchy that is appropriate for all situations

11 The Object Class A class called Object is defined in the java.lang package of the Java standard class library All classes are derived from the Object class If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class Therefore, the Object class is the ultimate root of all class hierarchies

12 The Object Class The Object class contains a few useful methods, which are inherited by all classes For example, the toString method is defined in the Object class Every time we have defined toString, we have actually been overriding an existing definition The toString method in the Object class is defined to return a string that contains the name of the object’s class together along with some other information

13 The Object Class All objects are guaranteed to have a toString method via inheritance Thus the println method can call toString for any object that is passed to it

14 The Object Class The equals method of the Object class returns true if two references are aliases We can override equals in any class to define equality in some more appropriate way The String class (as we've seen) defines the equals method to return true if two String objects contain the same characters Therefore the String class has overridden the equals method inherited from Object in favor of its own version


Download ppt "The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor."

Similar presentations


Ads by Google