Title: Extending Classes
1Extending Classes
2Introduction
- In the last lectures we studied encapsulation
(class) concept. - In this discussion we will look at extending
classes. - Inheritance by extending a concrete class,
abstract class and implementing an interface will
be examined.
3Inheritance
- Inheritance is the act of deriving a new class
from an existing one. - A primary purpose of inheritance is to reuse
existing software. - Original class used to derive a new class is
called super class and the derived class is
called sub class. - Inheritance represents is a relationship
between the superclass and the subclass.
4Syntax
- class subclass extends superclass
- class definition
-
- Example
- class Windstar extends FordCar // meaning it
inherits from class Fordcar .... - Windstar MyCar
- In this example, FordCar is the super-class and
Windstar is a sub-class and MyCar is an object
reference to Windstar class. - MyCar new Windstar(green) // object
instantiated
5Representing the Relationship
BankClass
has a
has a
has a
Account
MortgageSVC
BrokerageSVC
is a
is a
Savings
Checking
is a use inheritance has a use aggregation,
or membership
6Example Animal Hierarchy
is a
Animal
type
Dog
constructor
name breed
show
constructor (name) constructor (name , breed)
In the code, note the call to super-class construc
tor super
7Modifers
- Modifiers are special words that are added in
front of methods, data fields, classes to specify
their nature. - Visibility modifiers private, public, protected
- Protected modifier is used when the entity needs
to be available to the subclass but not to the
public.
8Need for protected modifier
Employee public name private wage_rate and hours
uses
super class
Application uses Employee class,
Paid_Employee class
Paid_Employee
uses
public compute_wages()
sub class
9Need for protected modifier
- Employee is a super class, Paid_Employee is a sub
class derived from it. - If we make wage_rate and hours public they are
available to the whole world. - If you make them private then they are not
accessible to the sub class unless there are set
and get functions. - Protected access control provides a mechanism for
hiding details from the world but making it
available to sub classes.
10Attributes (Data) Modifiers
- Data with no modifier is visible to sub-class
within the same package but not to sub-class
outside the package. - Private data is available only within the class
in which it is defined. - Protected is available to the class and all its
sub-classes. - Public is available to all class.
11Polymorphism
- Polymorphism is ability of a function to assume
several different forms and the automatic
resolution of one of the forms depending on the
(run time) type of the object.
12Abstract Class (ABC) and methods
- An abstract class is a class in which one or more
methods are declared but not defined. - Declaration of the methods can be omitted since
it make no sense to implement them in the
superclass. - Am abstract method has abstract modifier in
front of it. - ABC cannot be instantiated.
- Sole purpose of ABC is to provide an appropriate
super class from which classes may inherit. - Classes from which objects can be instantiated
are called concrete classes.
13Example
Food
Abstract super class
Pizza
Hamburger
HotDog
subclasses
Think of method eat of these classes.. eat() is
polymorphic.
14Example for Superclass Framework
- public abstract class EPA
- \\ different data
- public abstract void emission_control()
- class CA_EPA extends EPA
- void emission_control () //implementation
- class NY_EPA extends EPA
- void emission_control () // implementation
15Interface
- An interface essentially takes the abstract class
concept to the extreme. - When a class contains all abstract methods and/or
constants, it can be implemented using an
interface. - It contains just constants, abstract methods or
both. - Many different implementations can be realized
from a single interface. - The concept of interface-implementation is the
core of the JDK event model.
16Animal interface and implementations
implementation1
dog
spaniels
show()
sound().
Special details added on to the general dog
characteristics
interface
others
animal
Other implementations
show() sound()
17Example
EVENT LISTERNERS
UPDATE GUI
GUI
EVENT HANDLERS
18Summary
- Inheritance implements a very useful relationship
between classes. - Inheritance lets you extend and reuse existing
classes / library of classes. - Interface provides a framework for a system of
classes. - A class may implement more than one interface.