Upload
roy-rich
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
More on InheritanceDr. Andrew Wallace PhD BEng(hons) EurIng
Overview
• Abstraction
• Polymorphism
• Interface
• Package
• Shadows
• Object Serialization
Quiz
• What is inheritance in Java?
• How do you organise code between classes and their sub classes?
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
Abstraction
abstract class MyClass
{
public abstract void myMethod(int in);
public abstract int myOtherMethod();
public void float yetAnotherMethod(){return 1.0;}
}
Abstraction
class MySubClass extends MyClass
{
public void myMethod(int in)
{
…
}
public int myOtherMethod()
{
…
}
}
Quiz
• What is an abstract class?
• How does an abstract class differ from an interface?
• What is the difference between overloaded and overridden methods?
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?
Polymorphism
class Animal
class Dinosaur extends Animal
class Stegosaurus extends Dinosaur
Animal
Dinosaur
Stegosaurus
Polymorphism
Dinosaur d = new Stegosaurus();
Stegosaurus s = new Dinosaur();
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?
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
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();
Quiz
• How is polymorphism implemented in Java?
• Why can a superclass reference not call a method defined in a subclass?
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!
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
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;
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
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);
}
}
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();
}
}
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
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
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();
}
Quiz
• What is a package in Java?
• How can you save the state of a object in Java?
Questions?