C for Java Programmers - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

C for Java Programmers

Description:

A pure virtual method must be overridden in subclasses. ... it is the root of its own hierarchy and provides only the behavior defined ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 35
Provided by: seungs
Category:
Tags: java | my | own | pet | programmers | virtual

less

Transcript and Presenter's Notes

Title: C for Java Programmers


1
C for Java Programmers
  • Chapter 6
  • Polymorphism

2
Introduction
  • A static type is associated with a declaration.
  • A dynamic type is associated with a value.

3
We need a class hierarchy
Animal
Mammal
Bird
Cat
Dog
4
Figure 6.1 Animal Kingdom in Java
  • abstract class Animal abstract public void
    speak()
  • class Bird extends Animal public void speak()
    System.out.println("twitter")
  • class Mammal extends Animal public void speak()
    System.out.println("can't speak") public
    void bark() System.out.println("can't bark")
  • class Cat extends Mammal public void speak()
    System.out.println("meow") public void purr()
    System.out.println("purrrrr")
  • class Dog extends Mammal public void speak()
    System.out.println("wouf") public void bark()
    System.out.println("wouf")

5
Figure 6.2 Animal Kingdom in C
  • class Animal public virtual void speak() 0
  • class Bird public Animal public virtual
    void speak() printf("twitter")
  • class Mammal public Animal public virtual
    void speak() printf("can't speak") void
    bark() printf("can't bark")
  • class Cat public Mammal public void speak()
    printf("meow") virtual void purr()
    printf("purrrrr")
  • class Dog public Mammal public virtual void
    speak() printf("wouf") void bark()
    printf("wouf")

6
Virtual Non-virtual Overriding
  • Overriding a method in a parent class is
    replaced in a child class by a method having
    exact same type signature.
  • In C, overriding uses the keyword virtual.

7
Example of overriding
  • Dog d new Dog() Mammal m d d-gtbark()
    // wouf m-gtbark() // can't bark
  • d-gtspeak() // wouf m-gtspeak() // also
    wouf Animal a d a-gtspeak() // and more
    wouf
  • d-gtbark() // wouf a-gtbark() // compile error,
    not allowed
  • Mammal mm mm d mm.speak() // can't
    speak d-gtspeak() // although dog will wouf

8
Impact of Virtual on Size
  • When a class contains a virtual method, an
    internal table called the virtual method table is
    created.
  • This table is used in the implementation of the
    dynamic binding of message to method required by
    the virtual method.
  • Each instance of a class will contain an
    additional hidden pointer value, which references
    the virtual method table.

9
Obtaining Type Information from a Dynamic Value
  • getClass ( ) in Java
  • Animal a new Dog() // following will print
    class Dog System.out.println("class is "
    a.getClass().getName())
  • typeid ( ) in C
  • Animal a new Dog() // will print the
    class Dog println("class is s\n",
    typeid(a).name())

10
Abstract Classes
  • A pure virtual method must be overridden in
    subclasses.
  • An interface can be simulated by pure virtual
    methods.

11
Example of Abstract Classes
  • class KeyPressHandler // specification for key
    press event handlerpublic virtual void keyDown
    (char c) 0
  • class MouseDownHandler // specification for
    mouse down event handlerpublic virtual void
    mouseDown (int x, int y) 0 virtual void
    mouseUp (int x, int y) 0
  • class EventHandler public KeyPressHandler,
    public MouseDownHandler public void keyDown
    (char c) ... void mouseDown (int x, int y)
    ... void mouseUp (int x, int y)( ...

12
Downcasting (Reverse Polymorphism)
  • Downcasting reverses the assignment to a
    polymorphic variable.
  • Used to undo the effect of a polymorphic
    assignment (go from a higher class to a lower one)

13
Downcasting (Reverse Polymorphism)
  • C does not perform a run-time check to ensure
    the validity of cast conversion.
  • To resolve the problem, dynamic cast, a part of
    run-time type information system (RTTI).

14
Example of Downcasting
  • Cat c dynamic_cast ltCat gt (a) if
    (c) printf("variable was a cat") else printf
    ("variable was not a cat")
  • void v ...
  • // we know, from elsewhere, that v is really a
    cat
  • Cat c static_castltCat gt(v)
  • Whenever possible, use the RTTI instead of
    standard unchecked cast conversions.

15
Name Resolution
  • Name resolution is matching a function body to a
    function name.
  • class Parent public void test (int i)
    printf("parent test") class Child
    public Parent public void test (int i, int
    i) printf("child two arg test") void test
    (Parent p) printf("child object test")

16
Different techniques for NR
  • Java Find the method with the best fit to the
    arguments, regardless of scope
  • C Find the closest name scope that contains
    the name, then find the best fit for the
    arguments within that name scope

17
Example of error
  • Child c new Child()
  • c-gttest(3) // will generate compiler error
  • Will generate a compiler error in C, similar
    code would not generate an error in Java
  • Because the closest scope is not the one with the
    correct function argument signatures

18
Getting around this problem
  • Redefine any inherited names that are overloaded
    with different type signatures.
  • class Child public Parent public void test
    (int i) Parenttest(i) // redefine
    inherited method void test (int i, int i)
    printf("child two arg test") void test
    (Parent p) printf("child object test")

19
A Forest, Not a Tree
  • Classes in C are not part of a single
    hierarchy.
  • If a class is not defined as inheriting from
    another class, it is the root of its own
    hierarchy and provides only the behavior defined
    by the class description.
  • A typical C program contains a number of
    different class hierarchies, each independent of
    the others.

20
Virtual Destructors
  • A destructor is a method that is invoked
    immediately before a variable it to be deleted.
  • Declare a virtual destructor if a class has any
    virtual methods.

21
Example of Destructors
  • class Animal virtual Animal ()
  • printf("goodbye animal") ... ...
    class Cat public Mammal Cat ()
    printf("goodbye cat") ...

22
Private Inheritance
  • If inheritance is protected, fields declared as
    public in the parent class become protected in
    the child class.
  • If inheritance is private, fields declared either
    as public or protected in the parent become
    private in the child class.

23
Private Inheritance
Parent
Parent
public
public
Child
Child
Private inheritance
Public inheritance
24
Example of Private Inheritance
  • class Stack public List // assume elements
    are integerspublic push (int val)
    addToFront(val) pop () removeFirstElement()
    top () return firstElement()
  • class Stack private List public push (int
    val) addToFront(val) pop ()
    removeFirstElement() top () return
    firstElement()
  • class Stack private List public push (int
    val) addToFront(val) pop ()
    removeFirstElement() top () return
    firstElement() using isEmpty() using int
    size()

25
Inheritance and Arrays
  • Java permits array to be assigned to a variable
    that is declared as an array of the parent class
  • Dog dogs new Dog10 // an array of dog
    values
  • Animal pets dogs // legal
  • pets2 aCat // is this legal?
  • To prevent, Java actually performs a run-time
    check on assignments to arrays of objects.

26
C Gets this right
  • This is one place where C seems to do the more
    intelligent thing than does Java
  • Dog dogs new Dog10 // an array of dog
    values
  • Animal pets dogs // Not legal

27
Overloading
  • Overloaded when two or more function bodies are
    associated with a single function name.
  • class Child public Parent public void test
    (int i) Parenttest(i) // redefine
    inherited method void test (int i, int j)
    printf("child two arg test") void test
    (Parent p) printf("child object test")

28
Overloading
  • Functions are distinguished by the compiler by
    the number and type of arguments used in the
    function invocation.
  • c-gttest(4)
  • c-gttest(2, 4)

29
Operator Overloading
  • Operators are just functions, and they can be
    overloaded as well
  • Dog operator (Dog left, Dog right)
  • // return a new Dog value that is the sum of the
    parents return new Dog()
  • Cat operator (Cat left, Cat right)
    return new Cat()
  • More on this next chapter

30
Test your Understanding
  • What is a polymorphic variable?
  • What is the difference between the static and
    dynamic type of a polymorphic variable? How is
    each determined?

31
Test your understanding
  • Using the idea of static and dynamic type,
    explain the meaning of the keyword virtual
  • Explain the rules for matching a message name to
    a method

32
Test your understanding
  • What is a pure virtual method?
  • How is it defined?

33
Test your understanding
  • What is a dynamic cast? How is it different from
    a static cast?

34
Test your understanding
  • What is an overloaded function name?
  • How does C resolve the use of an overloaded
    function name? How is this different from the
    way Java works?
Write a Comment
User Comments (0)
About PowerShow.com