Inheritance - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Inheritance

Description:

Aristotle was probably the first to begin a careful study of the ... an object of the base class, only to upcast to it so that its interface can be used. ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 23
Provided by: IDE66
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Lecture 1
  • Inheritance
  • Files
  • Exceptions
  • Collections

2
Class and Inheritance
  • Aristotle was probably the first to begin a
    careful study of the concept of type he spoke of
    the class of fishes and the class of birds.
  • The idea is all objects, while being unique,
    are also part of a class and have characteristics
    (data)and behaviors (methods) in common
  • So, what we do in object-oriented programming is
    create new data types.
  • But object-oriented programming languages use the
    class keyword. When you see the word type
    think class and vice versa

3
In the top of java class hierarhy
Classes can be derived from classes that are
derived from classes that are derived from
classes, and so on
4
An object have an interface
  • Once a class is established, you can make as many
    objects of that class as you like, and then
    manipulate those objects to solve the problem.

Light lt new Light() lt.on() lt.dim()
5
  • An other way to reuse a class is to place an
    object of that class inside a new class. We call
    this creating a member object.
  • public class Lamp
  • Light lt
  • Color color
  • // other

UML
6
Or by Inheritance
  • A type does more than describe the constraints on
    a set of objects it also has a relationship with
    other types.
  • Two types can have characteristics and behaviors
    in common, but one type may contain more
    characteristics than another

7
Abstract classes
  • Often you want the base class to present only an
    interface for its derived classes. You dont want
    anyone to actually create an object of the base
    class, only to upcast to it so that its interface
    can be used.
  • This is accomplished by making that class
    abstract using the abstract keyword.
  • If anyone tries to make an object of an abstract
    class, the compiler prevents them. This is a tool
    to enforce a particular design.
  • To override a function, you simply create a new
    definition for the function in the derived class.
    Youre saying, Im using the same interface
    function here, but I want it to do something
    different for my new type.

8
Interface
  • In an interface is a reference type, similar to a
    class, that can contain only constants, method
    signature. !
  • ! There are no method bodies.
  • ! Interfaces cannot be instantiatedthey can
    only be implemented by classes or extended by
    other interface
  • public inteface Moveable
  • public void moveX()
  • public void moveY()
  • public void jump()

9
Implement interfaces
  • public class Boll implements Movable
  • // variables and methods to define a boll-object
    but also
  • public void moveX() your code to move boll X
  • public void moveY() your code to move boll Y
  • public void jump() your code to jump boll
  • public class Mask implements Movable
  • // variables and methods to define a mask-object
    and
  • public void moveX() your code to move mask X
  • public void moveY() your code to move maskY
  • public void jump() your code to jump mask

10
I/O Streams , java.io.
  • Byte Streams handle I/O of raw binary data.
  • Character Streams handle I/O of character data,
    automatically handling translation to and from
    the local character set.
  • Buffered Streams optimize input and output by
    reducing the number of calls to the native API.
  • Scanning and Formatting allows a program to read
    and write formatted text.
  • Data Streams handle binary I/O of primitive data
    type and String values.
  • Object Streams handle binary I/O of objects.

11
Byte Streams (returns -1 to in
  • Returns -1 to indicate the end of the stream
    (file)
  • Always Close Streams
  • Closing a stream when it's no longer needed
    is very important so important that CopyBytes
    uses a finally block to guarantee that both
    streams will be closed even if an error occurs.
  • When Not to Use Byte Streams CopyBytes seems like
    a normal program, but it actually represents a
    kind of low-level I/O that you should avoid.
    Since data.txt contains character data, the best
    approach is to use character streams. There are
    also streams for more complicated data types.
    Byte streams should only be used for the most
    primitive I/O. So why talk about byte streams?
    Because all other stream types are built on byte
    streams.

12
Character streams
  • FileReader inputStream null
  • FileWriter outputStream null
  • try
  • inputStream new FileReader(data.txt")
  • outputStream new FileWriter(chardata.txt")
  • Returns -1 to indicate the end of the stream
    (file)

13
Line I/O use character streams
  • BufferedReader inputStream null
  • PrintWriter outputStream null
  • try inputStream new BufferedReader( new
    FileReader(filnamn"))
  • outputStream new PrintWriter(new
    FileWriter(filnamn"))
  • Returns null to indicate the end of the file (EOF)

Se examples http//javaalmanac.com/egs/java.io/pk
g.html
14
Write och read object
  • If the class implements the interface
    Serializable.
  • class Account implements Serializable
    Person holder double balance
  • ObjectOutputStream oos new ObjectOutputStream(
    new FileOutputStream("Objects.dat
    "))
  • Account general new Account( fred, 110.0) //
    create obj.
  • oos.writeObject( general ) // write the obj.

15
What is exception ?
  • Exceptions provide the means to separate the
    details of what to do when something out of the
    ordinary happens from the main logic of a
    program.
  • In traditional programming, error detection,
    reporting, and handling often lead to confusing
    spaghetti code.
  • Example pseudocode method here that reads an
    entire file into memory.

readFile open the file determine its size
allocate that much memory read the file into
memory close the file
What happens if the file can't be opened? What
happens if the length of the file can't be
determined? What happens if enough memory can't
be allocated? What happens if the read fails?
What happens if the file can't be closed?
16
What happend ?
  • When an error occurs, the method creates an
    object, an exception object and throw it to the
    runtime system.
  • The runtime searches the call stack for a method
    that contains a block of code that can handle the
    exception

Method call stack
17
What happend?
  • If the runtime system exhaustively searches all
    the methods on the call stack without finding an
    appropriate exception handler, catch statement,
    the runtime system (and, consequently, the
    program) terminates in a wrong way.

18
In practice
  • public void printArray(int lista)
  • if (lista.length 0)
  • throw new EmptyArrayException() ????
  • else
  • do print

19
Tre types of exceptions
  • unchecked Runtime exception
  • NullPointerException, ArithemticException,
    NumberFormatException
  • checked must be handled by the programmer
  • errors a programmer can not anticipate this
    type of exceptions

Se http//java.sun.com/docs/books/tutorial/essent
ial/exceptions/advantages.html
20
In practice
  • try
  • to do this or
  • catch (ExceptionType1 name)
  • handle this type on ex.
  • catch (ExceptionType2 name)
  • handle this type on ex.
  • finally
  • The runtime system always executes the
    statements within the finally block regardless of
    what happens within the try block

21
Creating your own exceptions
public class EmptyArrayException
extends Exception public
EmptyArrayException (String s) super(
s)
22
Propagate exceptions
  • public void method ( ) throws IOExceptions,
    AnyExceptionType
  • // do not handle exception just propagete it
Write a Comment
User Comments (0)
About PowerShow.com