Download presentation
Presentation is loading. Please wait.
Published byRoy Rich Modified over 9 years ago
1
More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng andrew@cs.umu.se
2
Overview Abstraction Polymorphism Interface Package Shadows Object Serialization
3
Quiz What is inheritance in Java? How do you organise code between classes and their sub classes?
4
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
5
Abstraction abstract class MyClass { public abstract void myMethod(int in); public abstract int myOtherMethod(); public void float yetAnotherMethod(){return 1.0;} }
6
Abstraction class MySubClass extends MyClass { public void myMethod(int in) { … } public int myOtherMethod() { … }
7
Quiz What is an abstract class? How does an abstract class differ from an interface? What is the difference between overloaded and overridden methods?
8
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?
9
Polymorphism class Animal class Dinosaur extends Animal class Stegosaurus extends Dinosaur Animal Dinosaur Stegosaurus
10
Polymorphism Dinosaur d = new Stegosaurus(); Stegosaurus s = new Dinosaur();
11
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?
12
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
13
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();
14
Quiz How is polymorphism implemented in Java? Why can a superclass reference not call a method defined in a subclass?
15
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!
16
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
17
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;
18
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
19
Shadows Variables or classes in differ blocks but with the same name Public class MyClass { intx; public void MyMethod() { x = 1; int x = 5; System.out.println(x); } }
20
Shadows package MyPackage; public class MyClass { … } package MyOtherPackage; Import MyPackage; public class MyClass { public void MyMethod() { MyClass m = new MyClass(); MyPackage.MyClass pm = new MyPackage.MyClass(); } }
21
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
22
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
23
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(); }
24
Quiz What is a package in Java? How can you save the state of a object in Java?
25
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.