Extending Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Extending Classes

Description:

In the last lectures we studied encapsulation (class) concept. ... Inheritance is the act of deriving a new class from an existing one. ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 19
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Extending Classes


1
Extending Classes
  • B.Ramamurthy

2
Introduction
  • 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.

3
Inheritance
  • 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.

4
Syntax
  • 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

5
Representing 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
6
Example 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
7
Modifers
  • 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.

8
Need 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
9
Need 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.

10
Attributes (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.

11
Polymorphism
  • 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.

12
Abstract 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.

13
Example
Food
Abstract super class
Pizza
Hamburger
HotDog
subclasses
Think of method eat of these classes.. eat() is
polymorphic.
14
Example 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

15
Interface
  • 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.

16
Animal interface and implementations
implementation1
dog
spaniels
show()
sound().
Special details added on to the general dog
characteristics
interface
others
animal
Other implementations
show() sound()
17
Example
  • GUI and listerners

EVENT LISTERNERS
UPDATE GUI
GUI
EVENT HANDLERS
18
Summary
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com