Interfaces and Abstract classes - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Interfaces and Abstract classes

Description:

Another class has to inherit and override each method in order to create an instance. ... highJumpChamp = (Animal) cj; barHeight ; else //this animal cannot jump! ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 10
Provided by: uga66
Category:

less

Transcript and Presenter's Notes

Title: Interfaces and Abstract classes


1
Interfaces and Abstract classes
2
Interface
What is an interface?
Defn
Cannot instantiated an instance of an interface
Another class has to inherit and override each
method in order to create an instance.
How is an interface used?
To describe properties. Ex. Can Jump, Can
Read, Pink, Spotted, Can Compare, Can Order,
Properties are not classes - instead a single
property may be shared amongst many unrelated
classes. Ex. Humans can Jump, and so can
Frogs. Humans can speak, but so can
Parrots
Example
public interface CanJump public int jump()
3
Abstract Class
What is an abstract class?
Defn
Cannot be instantiated
Another class has to
How is it used?
To describe a common ancestor (super) class where
abstract behavior is known, but some or all
specific behavior is unknown.
Example Animal
communicate sex (f/m) consume nutrients
Example
public abstract class Animal protected char
sex protected String type protected
Animal(String type)this.type type
protected void setSex(char s)sex s
public char getSex()return sex public
abstract void communicate() public abstract
void consumeNutrients(Nutrient n) throws
4
Example
Nutrient
CanFly
Animal
CanJump
CanSpontaneouslyChangeSex
Fly
Frog
Frogasauras
public abstract class Animal implements Nutrient
protected char sex protected String
type protected Animal(String type)this.type
type protected void setSex(char s)sex
s public char getSex()return sex
public abstract void communicate() public
abstract void consumeNutrients(Nutrient n) throws

Polymorphism Every animal can be thought of as
a Nutrient
5
Example
public interface Nutrient public int
caloricValue()
public interface CanFly //returns the max
altitude public int altitude()
public interface CanSpontaneouslyChangeSex
//toggles the sex of the animal public void
changeSex()
public interface CanJump //returns the
height of the jump public int jump()
6
Example
public class Frog extends Animal
implements CanSpontaneouslyChangeSex,
CanJump This class must implement 1. All
methods inherited from CanSpontaneouslyChangeSex
2. All methods inherited from CanJump 3. All
unimplemented methods inherited from Animal, 4.
All unimplemented methods Animal inherited from
Nutrient
7
Example
public class Frog extends Animal
implements CanSpontaneouslyChangeSex,
CanJump public void changesSex()
if (sex m) setSex(f)
else setSex(m) public int jump()
return 5 public void communicate()
System.out.println(Croak Croak)
public void consumeNutrients(Nutrient n)
throws BadStomachException if (n
instanceof Fly) calorieStore
n.caloricValue() communicate()
else throw new
BadStomachException() public int
caloricValue() return 350
8
Its all still inheritance
Frog f new Frog() Animal a f CanJump jp
f CanSpontaneouslyChangeSex css f
What methods are accessible using the reference
f? What methods are accessible using the
reference a? What methods are accessible using
the reference jp? What methods are accessible
using the reference css?
9
Example - usage
public class Interrogator public static void
interrogate(Animal a) a.speak()
public class AnimalOlympics public int
barHight 0 public Animal highJumpChamp
public void highJumpEvent(Animal a)
//only those who can jump need tryout! if (cj
instanceof CanJump) CanJump cj
(CanJump) a int jumpHeight
cj.jump() if (jumpHeight gt
barHeight) maxHeight
jumpHeight highJumpChamp
(Animal) cj barHeight
else //this animal
cannot jump! System.out.println(Sorry you
do not qualify)
Write a Comment
User Comments (0)
About PowerShow.com