Title: CS1020
1- Inheritance and Polymorphism
2Classes with Family ties
- Sometimes we find that two (or more) instantiable
classes could be related to each other in the
sense that - They are not the exactly the same, but they are
not different either - One of the classes is a special case of the
other, and needs to behave a little differently - Example Class Beagle is a special case of
Class Dog - and maybe Class Rottweiler is also a special
case of Class Dog
3The Java mechanism for defining relationships
between related instantiable classes is called
Inheritance
- public abstract class Pet // generic housepet
- public class Dog // may or may not extend Pet
- public class Rottweiler extends Dog //
specific type of Dog - public class Beagle extends Dog // specific
type of Dog - The extends keyword establishes the inheritance
relationship - extends means is a kind of
- Note this is similar to extending an abstract
class (like Pet), but remember an abstract class
is itself not instantiable - Each class gets its own .java file for
implementation - unless we want to use inner classes
4UML and Inheritance
Superclass/Parent Class/Base Class
Subclasses/Child Classes/Derived Classes
5Implements vs. Extends
- A class or abstract class can implement more than
one interface - Even if multiple interfaces declare the same
behavior - A class or abstract class can extend only one
other class or abstract class - Because attributes or behaviors cannot be
inherited more than once
6Rules of inheritance
- All attributes and methods defined in a
superclass are inherited by all subclasses - But Only protected and public attributes and
methods defined in the superclass are accessible
in the subclasses - meaning only those superclass attributes are
visible to the subclasses - And only those superclass methods are callable by
the subclasses - Package attributes and methods are accessible
only if the subclass is in the same package as
the superclass - Private methods and attributes defined in a
superclass are only accessible within the
superclass itself
methodA() can onlyaccess these attributes and
methodsof the superclass
7Visibility from other classes
- doSomething() can only access the public
attributes and methods of Superclass or
SubClass - publicAttr
- publicMethod()
- methodA() (of Subclass)
- Package attributes and methods are accessible if
SomeOtherClass is in the same package as
Superclass and Subclass
8A subclass method can redefine a superclass method
- This is called overriding a method
- Do not confuse with method overloading
- A subclass overrides a superclass method to
provide a specialized behavior
Beagle provides its own custom implementation of
speak()
9Polymorphism allows a single variable to refer to
objects from different subclasses in the same
inheritance hierarchy
- For example, if Beagle is a subclass of Dog, then
the following statements are valid
10The instanceof operator can help us learn the
class of an object
- The following code counts the number of Beagles
in a collection
11Superclasses can keep secrets
- If a superclass declares some data members and
methods private, they can be inherited but cannot
be seen by subclasses - Protected makes a data member or method of a
superclass accessible to descendant classes - In Java, protected also means package visible
- Public data members and methods are accessible to
everyone
12Inheritance and Member Accessibility
13The Effect of Three Visibility Modifiers
In this example, Client is not in the same
package as Super and Sub
14Everything except the private members of the
Super class is visible from a method of the Sub
class
method
15Data members accessible from an instance are also
accessible from other instances of the same
class.
16Every Java class has a common ancestor the
Object class
- If a class declaration does not explicitly
designate the superclass with the extends clause,
then the classs superclass is the Object class. - Every Java class ultimately extends Object
- The Object class is the source of the following
methods which every Java class inherits - clone() makes a copy of the object
- equals() compares an object to another object
- toString() converts an object to its String
representation - and several others (see the Javadoc for the
Object class)
17Inheritance and Constructors
- Unlike members of a superclass, constructors of a
superclass are not inherited by its subclasses. - You must define a constructor for a class or use
the default constructor added by the compiler. - The statement
- super()
- calls the superclasss constructor.