Вы находитесь на странице: 1из 4

Lab 8.8.

2 Interface and Abstract Class


Estimated Time
60 minutes
Learning Objective
In this lab activity, the student will implement abstraction through the use of the interfaces.
Description/cenario
Java does not allow for multiple inheritances. However, the Java language provides Interface
classes for implementing inheritance from two unrelated sources.
Interfaces are abstract classes that define abstract methods and constants. The
purpose of an interface is to serve as a design document for external features of a class. ll of
the methods in an interface must be public. Interfaces provide a mechanism for a subclass to
define behaviors from sources other than the direct and indirect super classes.
The syntax for implementing an interface includes the use of the !eyword implements in the
class definition statement. However, implementing interfaces is more than the declaration in the
class definition statement. This declaration is viewed as a contract between the class that
implements the interface and the interface itself. The compiler enforces this contract by ensuring
that all methods declared in the interface are also implemented in the class. In some cases, this
may include a null implementation. null implementation is an implementation of a method
definition without any procedural code.
The guidelines for implementing interfaces are"
o public interface is a contract between the client code #that is the code in
the interface class$ and the class that implements the interface.
o Java interface is a formal declaration of a contract in which all methods contain no
implementation. That is all methods are abstract.
o %any unrelated classes can implement the same interface.
o class can implement many interfaces.
o class that implements an interface is an instance of the interface class. This
relationship that exists between super and sub&classes also applies to classes that
implement interfaces.
o ll methods of an interface are declared public and are automatically abstract.
The !eyword abstract is not re'uired to define the methods as such.
o n interface cannot include methods with 'ualifiers such as native, static,
synchronized, or final. These !eywords imply implementation code, and
methods cannot be implemented in an interface.
o n interface can declare constants and they are public, static and final.
If the 'ualifiers are omitted, the Java compiler provides them.
o Interfaces can extend other interfaces.
1 (undamentals of Java )rogramming *ab +opyright ,00-, +isco .ystems,
/sing the /%* diagram provided create the following classes and interfaces
o 0efine an abstract class called Animal that has two boolean attributes,
one non&abstract method, and two abstract methods.
o 0efine three interfaces (lying and 1on(lying that have one or more abstract
methods each.
o +lasses named Bat and Hawk that extend Animal and implement (lying
o +lasses named Snake and Monkey that extend Animal and implement 1on(lying
o In order to demonstrate behavior in each of the methods, include a print statement
that displays as follows 23at flies4, 23at eats4 etc5
!ile "anagement
(or this lab, launch 3lueJ. +lic! on #roject from the 3lueJ main menu and select $e%. In the 1ew
pro6ect window, in the *oo! in *ist box navigate to chap8 folder and create a new lab folder
lab8.8.2.
Tas&s
tep ' Create Abstract class
The abstract class Animal will provide some data and methods that a subclass can
inherit and have two methods that the subclass must override. 7ach subclass that
extends Animal class will inherit a sound() method and two attributes. The
attributes are wings and legs. The abstract methods hair() and eat() must
be implemented specific to each animal.
a. +reate the abstract class called nimal with
Two protected boolean attributes wings and legs intiali8ed to the value false9
protected constructor that accepts two boolean arguments wings and legs.
The constructor code assigns these values to the attributes wings and legs.
Two protected abstract methods eat() and hair() with return value of
oid!
:ne protected non abstract method sound() that prints out the sound the animal
ma!es.
tep 2 Create Interfaces
a. 0efine two interfaces. 7ach interface defines behavior uni'ue to a type of nimal. The actual
details of the behavior will be implemented in a specific animal class.
b. +reate an interface (lyer with abstract methods land() and
takeo""(). c. +reate the interface 1on(lyer with the abstract method
moement().
tep ( Create concrete classes t)at implement interfaces.
a. In each of the classes implement code that will demonstrate specific behavior uni'ue to each
type of animal. /se the table provided to print statements describing different animal behaviors.
b. +reate Snake and Monkey classes that extend Animal and implement
1on(lying. c. +reate Bat and Hawk classes that extend Animal and implement
(lying.
d. In the constructor of each of the classes, calls to the super constructor should include data for
legs and wings that are appropriate for each type of nimal. (or example the sna!e does not
have legs or wings, the mon!ey has legs, and a haw! has both legs and wings.
Animal eat ()
Snake rats
Monkey fruit
Bat fruit and insects
Hawk small animals
tep * Create test class
a. +reate a test class named Animal#est that will demonstrate calling super methods,
different implementations of the interfaces, and the use of extended methods.
b. /se this data" mon!ey ; 2spider4, haw! ; 2redtail4, sna!e ; 2cobra4, bat ; 2fox4
In the main method of Animal#est include the following"
Snake s = new Snake(cobra);
Bat b = new Bat(fox);
Hawk h = new Hawk(redtail);
Monke ! = new Monke(s"ider);
#ni!al a = new #ni!al(true$false)
#ni!al % = new Hawk();
&on'ling f = new Monke();
s(!ove!ent();
Sste!(out("rintln()Snakes have legs* )+s(legs+) Snakes
have wings* )+s(wings);
b(takeoff();
%(eat();
%(hair();
%(sound();
f(!ove!ent();
a. <hat happens if you try to instantiate an abstract class or an interface=
Interface ++ ,"L diagram
-Lab 8.8.2.
Animal
<interface>
Flyer
# wings boolean : = false
# legs: boolean : = false
<interface>
NonFlyer
+ takeOff():void
+ land():void
# Animal(wings: boolean, legs : boolean)
# eat():void
# hair() : void
# sound() : void
+ movement() : void
Bat
Hawk Snake Monkey
+ Bat()
+ eat():void
+ hair() : void
+ sound() : void
+ takeOff() : void
+ land() : void

Вам также может понравиться