Chapter 10 Introduction to InheritanceStudent Learning Outcomes. Show
1.Learn about the concept of inheritance Important Points
↑ indicates relationship of inheritance with EmployeeWithTerritory from Employee
c. Inheritance saves time because employee fields and methods already exist, and they reduce errors because the Employee methods already have been tested. They also reduce the amount of new learning required to use the new class. 2. Extending Classes: The keyword extends is used to achieve inheritance in Java. public class EmpolyeeWithTerritory extends Employee//automatically receives the data fields and methods of the superclass Employee: { private int empTerritory; public int getEmpTerritory() { �� ���return empTerritory; } public void setEmpTerritory (int num) { �empTerritory=num; } } a. Inheritance is a one way street. A child inherits from the parent only. A parent class does not have access to the child's methods or data. 3. Overriding
SuperClass methods. Sometimes the superclass data fields and methods are not entirely appropriate for the subclass objects; When true superclass has to be overriden.
4. Calling constructors during inheritance. When you instantiate an object that is a member of a subclass two constructors are called: the base class and the derived class. The superclass constructors must execute first and then the subclass constructor executes. 5. Accessing
Superclass constructors. When a superclass has a default constructor, you can create a subclass with or without its own constructor. If the subclass contains no constructor, all subclass objects use the superclass default constructor when they are instantiated. a. The super() statement must be the first statement in any subclass constructor that uses it.b. You cannot use both this() and super() in the same constructor because both are required to be the first statement in any constructor. c. You can use the keyword super from within a derived class method to access an overridden base class method. d. You can use the keyword super from within a derived class method to access a base class method that has not been overridden. 6. Employing Information Hiding. Without public get and set methods, there would be no way to access the private data fields. You cannot access a private data member of an object with someStudent.idNum=812;instead you use Information Hiding with private data can be altered by the methods you choose and only in ways that you can control. Private members of the parent class are not accessible within a
child�s class methods. 7. Methods you cannot override in a
subclass are: When you create any subclass object the subclass constructor must execute first?When you create any subclass object, the subclass constructor must execute first, and then the superclass constructor executes. A nonstatic method cannot override a static member of a parent class. You cannot declare a class to be final.
When an object that is a member of a subclass is instantiated which Constructors is called?When you instantiate an object that is a member of a subclass two constructors are called: the base class and the derived class. The superclass constructors must execute first and then the subclass constructor executes.
What is the first action of a constructor in a derived class?A constructor for a derived class begins with an invocation of a constructor for the base class.
Does a subclass inherit the constructor?A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
|