7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
1/22
Software Engineering 1The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Implementation
ImplementingControllers
TOPIC THREE
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
2/22
Software Engineering 2The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Controllers
Controllers are responsible for the implementation of thebehavior or functional requirements of the system.
Implementing controllers is similar to writing codes in other
programming language courses. It is better to implement controllers using abstract classes
and interface.
NOTE: The following slides is a basic review on abstractclasses and interfaces which came from the JEDIIntroduction to Programming I.
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
3/22
Software Engineering 3The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Abstract Classes
Abstract class
a class that cannot be instantiated.
often appears at the top of an object-oriented programming classhierarchy, defining the broad types of actions possible with objectsof all subclasses of the class.
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
4/22
Software Engineering 4The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Abstract Classes
abstract methods
methods in the abstract classes that do not have implementation
To create an abstract method, just write the method declarationwithout the body and use the abstract keyword
For example,
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
5/22
Software Engineering 5The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Sample Abstract Classpbli Sys em. u.prin ln( Lving hin ating .." ;}/***abstractmethodwalk*Ww atth is et hdtob eov erdd en ysbla ss eo f*Liv nTig*/pub iabstratvoidwalk()}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
6/22
Software Engineering 6The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Abstract Classes
When a class extends the LivingThing abstract class, it isrequired to override the abstract method walk(), or else, thatsubclass will also become an abstract class, and thereforecannot be instantiated.
For example,p bicvo dwak({ S sem.ou .ritl("Hmn walks. .");}}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
7/22
Software Engineering 7The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Coding Guidelines
Use abstract classes to define broad types of behaviors atthe top of an object-oriented programming class hierarchy,and use its subclasses to provide implementation details ofthe abstract class.
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
8/22
Software Engineering 8The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Interfaces
An interface
is a special kind of block containing method signatures (and possiblyconstants) only.
defines the signatures of a set of methods, without the body.
defines a standard and public way of specifying the behavior ofclasses.
allows classes, regardless of their locations in the class hierarchy, toimplement common behaviors.
NOTE: interfaces exhibit polymorphism as well, since program maycall an interface method, and the proper version of that method willbe executed depending on the type of object passed to the interfacemethod call.
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
9/22
Software Engineering 9The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Why do we use Interfaces?
To have unrelated classes implement similar methods
Example:
Class Line and MyInteger
Not related Both implements comparison methods
isGreater isLess
isEqual
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
10/22
Software Engineering 10The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Why do we use Interfaces?
To reveal an object's programming interface withoutrevealing its class
To model multiple inheritance which allows a class to havemore than one superclass
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
11/22
Software Engineering 11The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Interface vs. Abstract Class
Interface methods have no body
An interface can only define constants
Interfaces have no direct inherited relationship with anyparticular class, they are defined independently
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
12/22
Software Engineering 12The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Interface vs. Class
Common:
Interfaces and classes are both types
This means that an interface can be used in places where a classcan be used
For example:PersonInterfacepi = new Person();Prson p=newPerson()
Difference:
You cannot create an instance from an interface
For example:P ro nI trace p ne eson In efce( );//E RR!
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
13/22
Software Engineering 13The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Interface vs. Class
Common:
Interface and Class can both define methods
Difference: Interface does not have any implementation of the methods
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
14/22
Software Engineering 14The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Creating Interfaces
To create an interface, we write:
public interface [InterfaceName] {
//some methods without the body}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
15/22
Software Engineering 15The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Creating Interfaces
As an example, let's create an interface that definesrelationships between two objects according to the naturalorder of the objects.
pblicinterface elation{ pblicol asGrae(Objecta,Ob jectb);pblicol asLesbecta,Objec tb);pblicol asEqa(Ojecta,Obje ctb);}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
16/22
Software Engineering 16The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Creating Interfaces To use an interface, we use the implements keyword.
For example,* Tilseieiesge*p b
p riat edoub l x 1;p riat edoub l x 2;p riat edoub l y 1;p riat edoub l y 2;
p ubl i c Li ne( do u ble x1, d ou b le x2,d
this.x 1=x;this.x 2=x;this.y1 =y;this.y2 =y;
}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
17/22
Software Engineering 17The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Creating Interfaces
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
18/22
Software Engineering 18The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Creating Interfaces
When your class tries to implement an interface, alwaysmake sure that you implement all the methods of thatinterface, or else, you would encounter this error,
L ne.java:4: Line is no t abstract and does not override abstractpbcsLi eime entRen1e
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
19/22
Software Engineering 19The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Relationship of an Interface
to a Class A class can only EXTEND ONE super class, but it canIMPLEMENT MANY interfaces.
For example:
public class Person imple ments PersonInterface,LivingThi ngWhateverI ter ae {//someco de hr}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
20/22
Software Engineering 20The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Relationship of an Interface
to a Class Another example:public class ComputerScie nceStudent extends Student ip en ers nIntf ce, Liv//somecode hr}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
21/22
Software Engineering 21The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Inheritance among Interfaces
Interfaces are not part of the class hierarchy. However,interfaces can have inheritance relationship amongthemselves
For example:
p ...}
7/29/2019 MELJUN CORTES JEDI Slides-5.3 Implementing Controllers
22/22
Software Engineering 22The use of the slide has prior approval from the orignal author (Ms. Florence Balagtas) from the JEDI Introduction to Programming I Course Materials.
Summary
Controllers
Review on Abstract Classes and Interfaces
Top Related