More on Inheritance PowerPoint Presentation

More on Inheritance PowerPoint Presentation

2016-07-19 67K 67 0 0

Description

Dr. Andrew Wallace PhD . BEng. (hons) . EurIng. andrew@cs.umu.se. O. verview. Abstraction. Polymorphism. Interface. Package. Shadows . Object Serialization. Quiz. What is inheritance in Java?. How do you organise code between classes and their sub classes?. ID: 411644

Embed code:

Download this presentation



DownloadNote - The PPT/PDF document "More on Inheritance" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentations text content in More on Inheritance

Slide1

More on Inheritance

Dr. Andrew Wallace PhD

BEng

(hons)

EurIng

andrew@cs.umu.se

Slide2

Overview

Abstraction

Polymorphism

Interface

Package

Shadows

Object Serialization

Slide3

Quiz

What is inheritance in Java?

How do you organise code between classes and their sub classes?

Slide4

Abstraction

Some times it is advantageous to partly implement a superclass

Functionality that changes among sub classes

Abstraction is when a class is partly defined

Interface and abstraction

Interface – 100% abstraction

Abstraction - <= 100% abstraction

You can’t instantiate an abstract class

Slide5

Abstraction

abstract class

MyClass

{

public abstract void

myMethod

(

int

in);

public abstract

int

myOtherMethod

();

public void float

yetAnotherMethod

(){return 1.0;}

}

Slide6

Abstraction

class

MySubClass

extends

MyClass

{

public void

myMethod

(

int

in)

{

}

public

int

myOtherMethod

()

{

}

}

Slide7

Quiz

What is an abstract class?

How does an abstract class differ from an interface?

What is the difference between overloaded and overridden methods?

Slide8

Polymorphism

Take on many differ formsUsing parent class to refer to a child… is a … All cats are mammals therefore …All mammals are cats?

Slide9

Polymorphism

class Animalclass Dinosaur extends Animalclass Stegosaurus extends Dinosaur

Animal

Dinosaur

Stegosaurus

Slide10

Polymorphism

Dinosaur d = new Stegosaurus();Stegosaurus s = new Dinosaur();

Slide11

Polymorphism

Object type dictates the methods available

Not it’s reference

Dinosaur d = new Stegosaurus();

d can access all Animal methods and Stegosaurus methods

But what about overridden methods?

Slide12

Polymorphism

public class Dinosaur { public void walk(){System.out.println(“Dinosaur walking”);}} public class Stegosaurus extends Dinosaur { public void walk(){System.out.println(“Stegosaurus walking”);}}Dinosaur d = new Stegosaurus();d.walk();Stegosaurus walking

Dynamic binding

Slide13

Polymorphism

public class Dinosaur { public void walk(){System.out.println(“Dinosaur walking”);}} public class Stegosaurus extends Dinosaur { public void walk(){System.out.println(“Stegosaurus walking”);} public void eatGrass(){ … };}Dinosaur d = new Stegosaurus();d.eatGrass();

Slide14

Quiz

How is polymorphism implemented in Java?

Why can a superclass reference not call a method defined in a subclass?

Slide15

Interface

Polymorphism works for interfaces as for abstract

Classes can implement multiple interfaces

class

MyClass

implements Interface1, Interface2, Interface3

Interfaces can extend other interfaces

i

nterface Interface2 extends Interface1

Interfaces cannot implement other Interfaces

As they don’t implement any code!

Slide16

Package

Collection of classes

The classes in a packet can have a related function and depend on each other (same project or sub project)

No need for inheritance within the packet

Java has many packets

Util

Awt

Swing

Io

Lang

Slide17

Package

Create your own package

Naming convention

Hierarchical

Domain name of organisation that created it then organisation name

Subpackets

separated by dots

se.umu.ce.mypacket.mysubpacket

Package keyword

Start of file

p

ackage

se.umu.ce.mypacket.mysubpacket

;

Slide18

Package

To use a package

i

mport

packagename.classname

;

import

packagename

.*;

import

se.umu.ce.mypacket.mysubpacket

.*;

Packages map to directory structure

Classpath

Eleclips

– new package

Slide19

Shadows

Variables or classes in differ blocks but with the same name

Public class

MyClass

{

int

x;

public void

MyMethod

()

{

x = 1;

int

x = 5;

System.out.println

(x);

}

}

Slide20

Shadows

p

ackage

M

yPackage

;

p

ublic class

MyClass

{ … }

p

ackage

MyOtherPackage

;

Import

M

yPackage

;

p

ublic class

MyClass

{

public void

MyMethod

()

{

MyClass

m = new

MyClass

();

MyPackage.MyClass

pm = new

MyPackage.MyClass

();

}

}

Slide21

Object Serialization

Preserve the state of an object between runsSave an object to a file and the recreate it in the same stateTransmit the object form one machine to another

Slide22

Object Serialization

Implement the

Serializable

interface (in java.io)

All class members must be serializable else marked

transient

transient private String

strString

;

Transient are derived at run time

Use the

ObjectOutputStream

to write the object to a file

writeObject

Use the

ObjectInputStream

to

deserialize

the object

r

eadObject

Slide23

Object Serialization

c

lass

MyClass

implements

Serializable

{}

MyClass

m = new

MyClass

();

t

ry

{

FileOutputStream

f = new

FileOutputStream

(“

file.ser

”);

ObjectOutputStream

out = new

OutputObjectStrea

(f);

out.writeObject

(m);

Out.close

();

f.close

();

}

Slide24

Quiz

What is a package in Java?

How can you save the state of a object in Java?

Slide25

Questions?

Slide26

Slide27

Slide28

Slide29

Slide30


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.