Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Inheritance

Description:

Inheritance allows a software developer to derive a new class from an existing one ... We could define a parent class Person that can then be extended to create ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 33
Provided by: aseR1
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
2
Extending Classes
  • Its possible to create a class by using another
    as a starting point
  • i.e. Start with the original class then add
    methods, change what methods do, etc.
  • Allows reuse
  • Can extend a class in several ways for different
    purposes
  • based on the same original code

3
Inheritance
  • Inheritance allows a software developer to derive
    a new class from an existing one
  • The existing class is called the parent class, or
    superclass, or base class
  • The derived class is called the child class or
    subclass
  • As the name implies, the child inherits
    characteristics of the parent
  • That is, the child class inherits the methods and
    data defined by the parent class

4
Inheritance
  • Inheritance relationships are shown in a UML
    class diagram using a solid arrow with an
    unfilled triangular arrowhead pointing to the
    parent class
  • Proper inheritance creates an is-a relationship,
    meaning the child is a more specific version of
    the parent

5
Example
  • In the Blackjack design example, we had a Player
    class and a Dealer class
  • similar classes
  • each has a hand, takes turns,
  • We could define a parent class Person that can
    then be extended to create appropriate subclasses

6
Inheriting
  • Suppose we have a simple class A
  • class A
  • private int count
  • public int method()
  • return 1
  • And we want to define an extended class B with
    another method

7
Inheriting
  • Now B can inherit A and add a new method
  • class B extends A
  • public int newMethod()
  • return 2
  • B now has everything from the definitions of both
    A and B

8
Inheriting
  • Using A and B
  • A myA new A()
  • B myB new B()
  • System.out.println(myA.method())
  • System.out.println(myB.method())
  • System.out.println(myB.newMethod())
  • Output 112
  • An instance of B also has all members from A

9
Parents and Privacy
  • Subclasses cannot access private members
  • class B extends A
  • .
  • public void setCount(int count)
  • this.count count
  • ERROR count is private in A this is not A
  • Just like any other code cant access private
    members
  • We dont want count to be public

10
The protected Modifier
  • Encapsulation says that members should not be
    public
  • But we want access to inherited members
  • New visibibility modifer protected

11
The protected Modifier
  • A protected variable can be accessed from all
    code in the same package
  • It provides more encapsulation than public
    visibility, but is not as tightly encapsulated as
    private visibility
  • Key point Protected variables can be accessed
    from all subclasses of a given class
  • In UML, use symbol to mean protected

12
Example from Text
13
Constructors
  • Constructors are not inherited, but can be added
    in the usual way
  • class A
  • public A()
  • count 1
  • class B
  • public B(int n)
  • setCount(n)

14
The super Reference
  • Sometimes, we want to access something in the
    super class
  • For this, we use the super identifier
  • Similar to this, the meaning of super depends on
    the context
  • Using super gives access to the methods of the
    super class
  • This is particularly useful in constructors

15
The super Reference
  • A childs constructor is responsible for calling
    the parents constructor
  • The first line of a childs constructor should
    use the super reference to call the parents
    constructor
  • Why? To make sure the parent-part is
    initialized
  • This happens whether you like it or not
  • By default, super() is called automatically

16
Constructors (again)
  • class A
  • public A()
  • count 1
  • class B
  • public B(int n)
  • ltsuper() is automatically called heregt
  • setCount(n)

17
Constructors (a more useful example)
  • class A
  • public A(int n)
  • count n
  • class B
  • public B()
  • super(1)

18
Overriding Methods
  • A child class can override the definition of an
    inherited method in favor of its own
  • The new method must have the same signature as
    the parent's method, but can have a different
    body
  • The type of the object executing the method
    determines which version of the method is invoked

19
Overriding Example
  • class Bird
  • boolean flying
  • void flapWings()
  • flying true

20
Overriding Example
  • class Penguin extends Bird
  • void flapWings()
  • return

21
Accessing Parent Methods
  • Sometimes you may want to access the definition
    of a method from the parent class
  • You can use super to do this
  • public void method(int x)
  • // can put extra stuff here
  • super.method(x)

22
Overloading vs. Overriding
  • Overloading deals with multiple methods with the
    same name in the same class, but with different
    signatures
  • Overriding deals with two methods, one in a
    parent class and one in a child class, that have
    the same signature
  • Overloading lets you define a similar operation
    in different ways for different parameters
  • Overriding lets you define a similar operation in
    different ways for different object types

23
Single Inheritance
  • Java supports single inheritance, meaning that a
    derived class can have only one parent class
  • Java does not support multiple inheritance
  • e.g. a motorhome is a vehicle and a house
  • We use interfaces to fake multiple inheritance

24
The Object Class
  • Single inheritance means that we can think of all
    classes as a single family tree
  • The built-in class Object is the root of this
    tree
  • A class that doesnt explicitly extend anything
    has Object as its parent class
  • i.e. these are equivalent
  • class Foo class Foo extends Object
  • Object contains some methods (toString, equals)
    that are used unless overridden

25
Built-in Classes
  • Its also possible to inherit from built-in
    classes
  • Even if you dont know the original
    implementation, can still add new methods
  • e.g. list with extra data (ArrayList with
    additional instance variables) Scanner than can
    also read in a custom type .

26
When to Inherit
  • Inheritance is a powerful tool in OO design
  • Easy reuse of code works naturally with problems
    that have a hierarchy of objects
  • Dont confuse inheritance with aggregation
  • A should extend B only if A is-a B or A is a
    more specific version of B or A can be
    subsituted for B
  • not just because you need to use the parts of B

27
Example Shapes
  • Suppose we want to represent a collection of
    shapes (in a drawing program)
  • square, rectangle, circle (maybe others later)
  • All of these have some things in common
  • position the x-y coordinates of the shape
  • getters and setters for the position
  • translate(x,y) move the shape by this much
  • Create a class Shape that implements these

28
Example Shapes
  • Each of these will inherit Shape
  • Rectangle
  • add instance variables height and width
  • Square
  • inherit Rectangle and ensure heightwidth
  • Circle
  • add radius, interpret position as the centre

29
Example Shapes
  • The class definitions
  • class Shape
  • class Rectangle extends Shape
  • class Square extends Rectangle
  • class Circle extends Shape
  • Note this is not right
  • class Rectangle extends Square
  • A square is a certain kind of rectangle, and this
    should be evident in your code

30
Example Shapes (partial) UML
31
Abstract Classes
  • Not every class can be instantiated
  • use reserved word abstract to indicate that a
    class cannot be instantiated
  • used for place-holders in hierarchy
  • make contain abstract methods
  • e.g. Shape
  • Abstract classes are just for code reuse, and for
    designing a nice class hierarchy

32
Using final
  • Methods may be marked final
  • public final myMethod()
  • cannot be overridden
  • forces a method to have fixed behaviour
  • Classes may be marked final
  • public final class MyClass
  • cannot be inherited
  • forces all methods to have fixed behaviour
Write a Comment
User Comments (0)
About PowerShow.com