inheritance, abstract & interface pepper with help from and palsetia/java/animal/index.shtm

13
Inheritance, Abstract & Interface Pepper With help from http:// docs.oracle.com/javase/tutorial/java/IandI /super.html and http://www.cis.upenn.edu/~palsetia/java/An imal/index.shtml

Upload: georgina-gilbert

Post on 01-Jan-2016

229 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Inheritance, Abstract & Interface

Pepper

With help from http://

docs.oracle.com/javase/tutorial/java/IandI/super.html

andhttp://www.cis.upenn.edu/~palsetia/java/Animal/index.shtml

Page 2: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Seeing Eye Dog is a dog Properties: In addition

to being a dog with a name, isAwake and position states, it has a tracking code

Actions: In addition to being able to makeNoise, sleep and wake, it can get its trackingcode and sit to warn for a curb.

Dog Class + Special type of Dog

Credit to: http://www.cis.upenn.edu/~palsetia/java/Animal/index.shtml

Page 3: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Make sub-classes (put "extends " classname after class)http://home.adelphi.edu/~pe16132/csc171/notes/fangstuff/Wackadot.java

Can use the entire extended class Can override methods Can see all protected and public methods and variables Can choose to execute methods from the super class

using super.methodnamehttp://docs.oracle.com/javase/tutorial/java/IandI/super.html

Class Extension

Page 4: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

public class Dog{ private String name; private Boolean isAwake; private String position; public Dog (String name){ this.name = name; this.isAwake = true; this.position = "standing"; }

public String makeNoise(){ return "bark"; }

public void sleep(){ this.isAwake = false;}

public void wake(){ this.isAwake = true;}

public void sit(){ this.position = "sit";}}

Dog Class + Special type of Dog

Credit to: http://www.cis.upenn.edu/~palsetia/java/Animal/index.shtml

public class SeeingEyeDog extends Dog{ private String trackingCode; public SeeingEyeDog(String name,

String trackingCode){ super(name); this.trackingCode = trackingCode; }

public String getTrackingCode(){ return this.trackingCode;}

public String sitForCurb(){ super.sit(); super.makeNoise();}}

Page 5: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

SeeingEyeDog can be placed into Dog or SeeingEyeDog variable type When in Dog type, it cannot get the tracking codepublic class DogDriver { public static void main(){ Dog pepperPet = new Dog("Olive"); Dog working1 = new SeeingEyeDog("Sarah","ABC"); SeeingEyeDog working2 = new SeeingEyeDog("Coco","ABCD"); System.out.println (pepperPet.makeNoise()); System.out.println (working1.makeNoise()); System.out.println (working2.getTrackingCode()); // System.out.println (working1.getTrackingCode()); wont work if (working1 instanceof SeeingEyeDog){ SeeingEyeDog temp = (SeeingEyeDog) working1; System.out.println(temp.getTrackingCode()); }}}

Extended Class Used by Driver

Page 6: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Make one player Constructor – starts on square 1 and has a name Roll dice Say he is joining the game

Extend to different player types Zorgon - some number of eyes as well Override a method: He has a different message

to say he is joining the game. He tells of his powers

Extra method: ability to jump ahead the same number of squares as eyes.

Example: http://home.adelphi.edu/~pe16132/csc171/

presentations.html

Player Extension

Page 7: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Can only extend one class A seeing Eye Dog is a dog, but it is also a

trainable being Use Interface of trainable beings

Both the super and subclass can have objects created. Problem: Animals share methods and

properties, but cannot exist without being a specific type of animal such as Dog or Cat. Use abstract class of Animals

Extension Limits

Page 8: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Cannot create its own object

Can contain abstract classes, which every extender must code - compiler complains if the lower class does not implement it.

http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Abstract Classes

Page 9: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

public class Dog extends Animal{ private String position; public Dog (String name){ super(name); this.position = "standing"; }

public String makeNoise(){ return "bark"; }

public void sit(){ this.position = "sit";}

public String eats(){ return "Meat and whatever else I find";}}

Abstract Class and its extensions

public abstract class Animal{ private String name; private Boolean isAwake; public Animal (String name){ this.name = name; this.isAwake = true; }

public void sleep(){ this.isAwake = false;}

public void wake(){ this.isAwake = true;}

public abstract String eats();}

public class Fish extends Animal{ private boolean isSwimming;

public Fish(String name) { super(name); this.isSwimming = true; }

public String eats(){ return "little fish and algae";}}

Page 10: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

public class AnimalDriver{ public static void main(){ Animal[] myAnimals = new Animal[2]; myAnimals[0] = new SeeingEyeDog("Sarah","ABC"); myAnimals[1] = new Fish ("James"); System.out.println (myAnimals[0].eats()); System.out.println (myAnimals[1].eats()); if (myAnimals[0] instanceof SeeingEyeDog){ SeeingEyeDog temp = (SeeingEyeDog) myAnimals[0]; System.out.println(temp.getTrackingCode()); } }}

Animal Driver uses Abstract Class

Page 11: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

More limits Cannot extend two classes

Trainable Animal

Interface

Interfaces

Page 12: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Can create a type that has only abstract methods, with no properties. Basically a contract to implement interfaces Contains method headers with no code below it. Lower classes can implement many interfaces

(but only extend one class) Implementing an interface means you promise

(and the compiler checks) that you will implement the methods listed.

For our pets, trainable could be an interface containing the methods sit and stand and stay.

http://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

Interface

Page 13: Inheritance, Abstract & Interface Pepper With help from  and palsetia/java/Animal/index.shtm

Extension: To have all properties and method of super class in the sub class Add extends <classname> to the subclass Can override methods inside a subclass Cannot access anything private in the class Use super to use superclass methods if overridden

Abstract: To hold common code you want to extend Cannot be used to create instances Can insist method names be implemented

Interface: To insist method names be implemented Cannot be used to create instances Cannot have properties Cannot code methods, only their headers

Summary - Class Types