25
More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Embed Size (px)

Citation preview

Page 1: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

More on InheritanceDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Overview

• Abstraction

• Polymorphism

• Interface

• Package

• Shadows

• Object Serialization

Page 3: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Quiz

• What is inheritance in Java?

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

Page 4: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

Page 5: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Abstraction

abstract class MyClass

{

public abstract void myMethod(int in);

public abstract int myOtherMethod();

public void float yetAnotherMethod(){return 1.0;}

}

Page 6: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Abstraction

class MySubClass extends MyClass

{

public void myMethod(int in)

{

}

public int myOtherMethod()

{

}

}

Page 7: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Quiz

• What is an abstract class?

• How does an abstract class differ from an interface?

• What is the difference between overloaded and overridden methods?

Page 8: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Polymorphism

• Take on many differ forms

• Using parent class to refer to a child• … is a …

• All cats are mammals therefore …

• All mammals are cats?

Page 9: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Polymorphism

class Animal

class Dinosaur extends Animal

class Stegosaurus extends Dinosaur

Animal

Dinosaur

Stegosaurus

Page 10: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Polymorphism

Dinosaur d = new Stegosaurus();

Stegosaurus s = new Dinosaur();

Page 11: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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?

Page 12: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Polymorphismpublic 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 walkingDynamic binding

Page 13: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Polymorphismpublic 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();

Page 14: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Quiz

• How is polymorphism implemented in Java?

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

Page 15: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Interface

• Polymorphism works for interfaces as for abstract

• Classes can implement multiple interfaces• class MyClass implements Interface1, Interface2, Interface3

• Interfaces can extend other interfaces• interface Interface2 extends Interface1

• Interfaces cannot implement other Interfaces• As they don’t implement any code!

Page 16: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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

Page 17: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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• package se.umu.ce.mypacket.mysubpacket;

Page 18: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Package

• To use a package

• import packagename.classname;

• import packagename.*;

import se.umu.ce.mypacket.mysubpacket.*;

• Packages map to directory structure

• Classpath

• Eleclips – new package

Page 19: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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);

}

}

Page 20: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Shadowspackage MyPackage;

public class MyClass { … }

package MyOtherPackage;

Import MyPackage;

public class MyClass

{

public void MyMethod()

{

MyClass m = new MyClass();

MyPackage.MyClass pm = new MyPackage.MyClass();

}

}

Page 21: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Object Serialization

• Preserve the state of an object between runs

• Save an object to a file and the recreate it in the same state

• Transmit the object form one machine to another

Page 22: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

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• readObject

Page 23: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Object Serialization

class MyClass implements Serializable {}

MyClass m = new MyClass();

try

{

FileOutputStream f = new FileOutputStream(“file.ser”);

ObjectOutputStream out = new OutputObjectStrea(f);

out.writeObject(m);

Out.close();

f.close();

}

Page 24: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Quiz

• What is a package in Java?

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

Page 25: More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se

Questions?