Download presentation
Presentation is loading. Please wait.
1
Java Interfaces & Abstract Classes
CIS265/506 Java Interfaces & Abstract Classes
2
Abstract Classes To create a class that can not be instantiated, declare it “abstract”
4
Error!! Can not instantiate an abstract class.
5
Uses Abstract classes are used to define common methods and variables for their subclasses This gives our classes a common base, but different implementations for each method.
6
Abstract Methods An abstract class can include zero or more abstract methods An abstract method consists of a method declaration – without any implementation code or braces – followed by a semicolon
7
Data We can not declare data members (variables) as abstract
8
Implementation A class that inherits from a subclass must provide implementation code for all abstract methods.
9
Example Let us say we need to set up a “petting zoo day” for the neighborhood kids Somebody, somewhere, decided you were good at organizing these things. So you were “volunteered”
10
Continued We decide to create a small Java program to keep track of what animals we have Because we are clever, we think *ah-ha* we can use inheritance. But inherit from what? There is no one “über-class” that all animals come from No religion or philosophical discussions here!
11
Continued… The solution is an abstract class.
We can make a class called “Animal” that contains some things we need to keep track of, and leave the details for the sub-classes
12
? Yes. This is a very weird petting zoo. But, as the bumper sticker says, “Why Be Normal?”
13
public abstract class Animal {
private String nameOfAnimal; private boolean hasWings; private int numberOfLegs; public String getName() { return nameOfAnimal; } public void setName(String theName) { nameOfAnimal = theName; public void setTransport(boolean hW, int legs) { hasWings = hW; numberOfLegs = legs; public boolean getWings() { return hasWings; public int getLegs() { return numberOfLegs;
14
public String toString() {
StringBuffer s = new StringBuffer(); s.append("My Name is: " + nameOfAnimal + "\n"); s.append("I have " + numberOfLegs + " legs\n"); if (hasWings) { s.append("I have wings!\n"); } else { s.append("I don't have wings\n"); } return s.toString(); public abstract void speak();
15
package iA; public class Cow extends Animal { @Override public void speak() { System.out.println("Mooooooooooooo"); }
16
package iA; public class Snake extends Animal { @Override public void speak() { System.out.println("SSSSSSSSSSSSSSSSSSSSSSS"); }
17
package iA; public class Cat extends Animal { @Override public void speak() { System.out.println("Meeeeeoooooowww"); }
18
package iA; public class Duck extends Animal { @Override public void speak() { System.out.println("Quuaaaaaccckkk"); }
19
package iA; public class PettingZoo { public static void main(String[] args) { Cow MyCow = new Cow(); Snake MySnake = new Snake(); Cat MyCat = new Cat(); Duck MyDuck = new Duck(); MyCow.setName("Cow-y Cow"); MyCow.setTransport(false, 4); System.out.print(MyCow.toString()); MyCow.speak(); System.out.println("\n");
20
MySnake.setName("Sammy Snake");
MySnake.setTransport(false, 0); System.out.print(MySnake.toString()); MySnake.speak(); System.out.println("\n"); MyCat.setName("Kitty Cat"); MyCat.setTransport(false, 4); System.out.print(MyCat.toString()); MyCat.speak(); MyDuck.setName("Daffy Duck"); MyDuck.setTransport(true, 2); System.out.print(MyDuck.toString()); MyDuck.speak(); }
21
Output My Name is: Cow-y Cow I have 4 legs I don't have wings
Mooooooooooooo My Name is: Sammy Snake I have 0 legs SSSSSSSSSSSSSSSSSSSSSSS My Name is: Kitty Cat Meeeeeoooooowww My Name is: Daffy Duck I have 2 legs I have wings! Quuaaaaaccckkk Output
22
Abstract as Inheritance
Using abstract classes to specify common behavior is limiting because it requires that the subclasses become part of the superclass inheritance tree The superclass and subclasses must share an “is a” relationship – you may not want this Java does not support multiple inheritance, so these subclasses can not inherit from anything else.
23
Interfaces We will need a different mechanism to describe the general behavior of some objects Java lets us do this with interfaces An interface defines protocols of behavior without limiting the classes that provide the implementation We can implement multiple interfaces An interface can not be instantiated
24
Interfaces We use an interface to enforce a rule – this means all methods in an interface are mandatory We declare interfaces using the interface keyword
25
Methods All methods in an interface are implicitly abstract (you don’t say so – they just are) The methods are also implicitly public Methods can not be declared protected and/or final
26
Variables All variables must have an initialization expression (int MAX = 10; for example) They are implicitly static and final (like constants) They can not be modified at all.
27
Interfaces An interface can extend an interface!
This would allow one interface to inherit abstract methods and static final variables from another interface.
28
Implementing To create a class that implements an interface’s methods, use the implements keyword A class that implements an interface must provide implementation code for all the interface’s methods. Any subclasses automatically implement the interface by extension
29
Implementing A class can extend one or more interfaces and can extend a single class
30
The remote “controls” these devices
31
package iA; public interface UniversalRemote { public void FF(); public void Rew(); public void Power(); public void Eject(); public void Volume(int upOrDown); }
32
package iA; public class VCR implements UniversalRemote { private int thePower = 0; // 0 is off, 1 is on private int theVolume = 0; // 0 is mute public void Eject() { System.out.println("VCR Ejected"); } public void FF() { System.out.println("VCR Zoom passed the boring parts");
33
public void Power() { if (thePower == 0) { thePower = 1; System.out.println("Power On"); } else if (thePower == 1){ thePower = 0; System.out.println("Power Off"); public void Rew() { System.out.println("VCR - What was that again?"); public void Volume(int upOrDown) { theVolume += upOrDown; if (theVolume < 0) theVolume = 0;
34
public String toString() {
StringBuffer s = new StringBuffer(); s.append("VCR\n"); s.append("\tPower is: "); if (thePower == 0) s.append("off\n"); else { s.append("on\n"); s.append("and the volume is: " + theVolume +"\n"); } return s.toString();
35
package iA; public class TestTheRemote { public static void main(String[] args) { VCR v = new VCR(); DVR d = new DVR(); CheapCassettePlayer c = new CheapCassettePlayer(); System.out.print(v.toString()); v.Power(); v.Volume(6); v.FF(); v.Rew(); v.Volume(-3); v.Eject(); System.out.print("\n\n"); // etc }
36
Etc… VCR Power is: off Power On Power is: on and the volume is: 6
VCR Zoom passed the boring parts VCR - What was that again? and the volume is: 3 VCR Ejected Power Off Etc…
37
The remote “controls” these devices
These things “power” our devices
38
package iA; public interface PowerSource { public void thePowerFrom(String source); }
39
public class VCR implements UniversalRemote, PowerSource {
package iA; public class VCR implements UniversalRemote, PowerSource { private int thePower = 0; // 0 is off, 1 is on private int theVolume = 0; // 0 is mute private String theSource = "AC Adaptor"; ……. public void thePowerFrom(String source) { theSource = source; }
40
VCR Power is: off The Power Source is AC Adaptor Power On Power is: on and the volume is: 6 VCR Zoom passed the boring parts VCR - What was that again? and the volume is: 3 The Power Source is Batteries VCR Ejected Power Off
41
What’s missing? There are many important functionalities missing.
Can you program them?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.