COP 3331 Object Oriented Analysis and Design Chapter 5 Classes and Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

COP 3331 Object Oriented Analysis and Design Chapter 5 Classes and Inheritance

Description:

... can tell if two methods with the same name are different by the 'signature'. The signature consists of the name of the method and the data type of the parameters. ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 19
Provided by: DavidAG
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: COP 3331 Object Oriented Analysis and Design Chapter 5 Classes and Inheritance


1
COP 3331 Object Oriented Analysis and
DesignChapter 5 Classes and Inheritance
  • Jean Muhammad

2
Overview
  • Overloading
  • Extending Classes
  • Hiding Fields and Class Methods

3
Overloading
  • Overloading refers to the ability to allow
    different methods or constructors of a class to
    share the same name. The name is said to be
    overloaded with multiple implementations.
    Commonly know as the feature of polymorphism in
    the object oriented paradigm.

4
Overloading
  • The language can tell if two methods with the
    same name are different by the signature. The
    signature consists of the name of the method and
    the data type of the parameters. Note the return
    type, name of parameters, and final designations
    of parameters are not part of the signature.

5
Overloading
  • Rules for overloading Two methods in the same
    class my have the same name provided that they
  • Have different number of parameters, or
  • Same number of parameters but of different type.

6
Overloading
  • Example
  • Public class Point
  • protected double x,y
  • public Point()
  • x0.0 y0.0
  • public double distance (Point other)
  • double dx this.x other.x
  • double dy this.y other.y
  • return Math.sqrt(dx dy dy dy)

7
Overloading
  • public double distance (double x, double y)
  • double dx this.x x
  • double dy this.y y
  • return Math.sqrt(dx dx dy dy)
  • public double distance (int x, int y)
  • double dx this.x (double) x
  • double dy this.y (double) y
  • return Math.sqrt (dx dx dy dy)
  • public double distance ()
  • return Math.sqrt (xx y y)

8
Overloading
  • Two situations to overload
  • When there is a general, nondiscriminative
    description of the functionality that fits all
    the overloaded methods.
  • When all the overloaded methods offer the same
    functionality, with some of them providing
    default arguments.

9
Extending Classes
  • Inheritance (example) When class
    Hourly_employee inherits from, or extends the
    class Employee, then Hourly_employee is called a
    subclass of the class Employee. In Java, each
    class my only inherit properties from another
    class.

10
Extending Classes
  • Syntax of the extend clauseClassModifiers
    class ClassName
  • extends SuperClass
  • implements Interace1, Interface2, etc
  • ClassMemberDeclarations

11
Extending Classes
  • import java.awt.Color
  • public class ColoredPoint extends Point
  • public Color color
  • public ColorPoint (final double x, final
    double y,
  • final Color color)
  • super (x,y)
  • this.color color
  • public ColoredPoint (final double x, final double
    y)
  • this (x,y, Color.black) // default
    color
  • public ColoredPoint ()
  • color Color.black

12
Extending Classes
  • Note in the constructor. The invocation of super
    () must be the first statement in the
    constructor of the extended class
  • Note The second constructor of the ColoredPoint
    class supplies a default value for the color
    field.
  • Note The third constructor has not explicit
    invocation using super or this is made.
  • This is also an example of overloading.

13
Extending Classes
  • Subtype
  • Subtypes are a specialized version of a specific
    type.
  • For instance, let us say that you are storing
    integers for test scores. Since your test scores
    do not go below 0 or above 100, you could have a
    specialized subtype of integers that only had a
    valid range of 0 to 100.

14
Extending Classes
  • Overriding
  • Overriding is different from overloading. In
    overriding, methods share the same name,
    signature, and return type in contrast to
    overloading.
  • class A
  • public void m1() method ..
  • public void m1( int i) method . .
  • // Note class A contains two overaloaded methods.

15
Extending Classes
  • Now assumer we have the two classes
  • class B
  • public void m2() . .
  • class C extends B
  • public coid m2 () . .
  • B b new B()
  • C c new C()
  • b.m2() // invoke the m2() in class B
  • c.m2() // invoke the m2() in class C

16
Hiding Fields and Class Methods
  • Hiding refers to the introduction of a field or a
    class method in a subclass that has the same name
    as a field or class method in a superclass.
  • Overriding and hiding are different concepts
  • Instance methods can only be overriden.
  • Class methods and fields can only be hiden.

17
Hiding Fields and Class Methods
  • Example
  • public class A
  • int x
  • void y() method . .
  • static void z() method . .
  • public class B extends A
  • float x // hiding
  • void y () method . . // overriding
  • static int z () method . . // hiding

18
Hiding Fields and Class Methods
  • There is a crucial distinction between overriding
    and hiding.
  • When an overridden method is invoked, the
    implementation that will be executed is chosen at
    run time.
  • When the hidden method or field is invoked or
    accessed, the copy that will be used is
    determined at compile time. Class methods and
    fields are statically bound.
Write a Comment
User Comments (0)
About PowerShow.com