0% found this document useful (0 votes)
1K views

Absolute Java Chapter 7

Inheritance allows a derived class to inherit instance variables and methods from a base class. A derived class automatically has all the properties of the base class and can define additional methods and variables. Derived classes inherit code from base classes, allowing code reuse. When overriding a method, the derived class method must have the same parameters as the base class method but can change the returned type if it is more specific. Protected and package access allow instance variables and methods to be accessed by derived classes.

Uploaded by

Mattheus Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Absolute Java Chapter 7

Inheritance allows a derived class to inherit instance variables and methods from a base class. A derived class automatically has all the properties of the base class and can define additional methods and variables. Derived classes inherit code from base classes, allowing code reuse. When overriding a method, the derived class method must have the same parameters as the base class method but can change the returned type if it is more specific. Protected and package access allow instance variables and methods to be accessed by derived classes.

Uploaded by

Mattheus Lim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

7.

1 Inheritance Basics

- Inheritance is the process by which a new class


—known as a derived class—is created from
another class, called the base class.

- A derived class automatically has all the instance


variables and all the methods that the base class
has, and can have additional methods and/or
additional instance variables.

- A derived class is a class defined by adding


instance variables and methods to an existing
class.

Example : public class HourlyEmployee


extends Employee (base class)

- The existing class that the derived class is built


upon is called the base class.

- A derived class is also called a subclass, in


which case the base class is usually called a
superclass.

- Other derived class is said to inherit the instance


variables and methods of the base class that it
extends.
- Inheritance allows you to reuse code.

- As a general rule, when overriding a method


definition, you may not change the type returned
by the method
- The one exception to this rule is if the returned
type is a class type, then you may change the
returned type to that of any descendent class of
the returned type. This sort of changed return type
is known as a covariant return type.

- You can change the access permission of an


overridden method from private in the base class
to public in the derived class.

- You can use a public method anyplace that you


can use a private method, but it is not true that you
can use a private method anyplace that you can
use a public method.

- When you override a method definition, the new


method definition given in the derived class has
the exact same number and types of parameters.

- It is called overloading when a method in the


derived class were to have a different number of
parameters or a parameter of a different type from
the method in the base class, then the derived
class would have both methods.
7.2 Encapsulation and Inheritance

- An instance variable (or method) that is private in


a base class is not accessible by name in the
definition of a method in any other class, not even
in a method definition of a derived class.
- Note: Remember, accessor and mutator methods
can guard against inappropriate changes to
instance variables.

- Two classifications of instance variables and


methods that allow them to be accessed by name
in a derived class are protected access, which
always gives access, and package access, which
gives access if the derived class is in the same
package as the base class.
- If an instance variable or method has package
access, then that instance variable or method can
be accessed by name in the definition of any other
class in the default package.
- The class Object is in the package java.lang,
which is always imported automatically.

- The inherited methods equals and toString will


not work correctly for (almost) any class you
define. You need to override the inherited method
definitions with new, more appropriate definitions.

You might also like