SIM For Week 7
SIM For Week 7
In week 6, we already discussed the concepts of OOP. Now, for week 7, we will learn
how the encapsulation, inheritance and polymorphism is applied into programming
problems.
1. Encapsulation
Looking at the example of a power steering mechanism of a car. Power steering of a car
is a complex system, which internally have lots of components tightly coupled together,
they work synchronously to turn the car in the desired direction. It even controls the
power delivered by the engine to the steering wheel. But to the external world there is
only one interface is available and rest of the complexity is hidden. Moreover, the
steering unit in itself is complete and independent. It does not affect the functioning of
any other mechanism.
The basic foundation of encapsulation is to keep classes separated and prevent them
from having tightly coupled with each other.
A live example of encapsulation is the class of java.util.Hashtable. User only knows that
he can store data in the form of key/value pair in a Hashtable and that he can retrieve
that data in the various ways. But the actual implementation like, how and where this
data is actually stored, is hidden from the user. User can simply use Hashtable wherever
he wants to store Key/Value pairs without bothering about its implementation.
2. Inheritance
For example: Car is a classification of Four Wheelers. Here Car acquires the properties of
a four-wheeler. Other classifications could be a jeep, tempo, van etc. Four Wheeler defines
a class of vehicles that have four wheels, and specific range of engine power, load carrying
capacity etc. Car (termed as a sub-class) acquires these properties from Four Wheeler
(termed as a super-class), and has some specific properties, which are different from other
classifications of Four Wheeler, such as luxury, comfort, shape, size, usage etc.
A car can have further classification such as an open car, small car, big car etc, which will
acquire the properties from both Four Wheeler and Car, but will still have some specific
properties. This way the level of hierarchy can be extended to any level.
Java Swing and Awt classes represent best examples for inheritance.
Inheritance is the capability of a class to use the properties and methods of another class
while adding its own functionality. An example of where this could be useful is with an
employee records system. You could create a generic employee class with states and
actions that are common to all employees. Then more specific classes could be defined
for salaried, commissioned and hourly employees. The generic class is known as the
parent (or superclassor base class) and the specific classes as children
(or subclasses or derived classes). The concept of inheritance greatly enhances the
ability to reuse code as well as making design a much simpler and cleaner process.
The Object class is the highest superclass (ie. root class) of Java. All other classes are
subclasses (children or descendants) inherited from the Object class. The Object class
includes methods such as:
clone() equals() copy(Object finalize() getClass()
src)
hashCode() notify() notifyAll() toString() wait()
Java uses the extends keyword to set the relationship between a parent class and a
child class. As an example using the previously defined Box class:
public class GraphicsBox extends Box
The GraphicsBox class assumes or inherits all the properties of the Box class and can
now add its own properties and methods as well as override existing
methods. Overriding means creating a new set of method statements for
thesame method signature (name, number of parameters and parameter types). For
example:
//define position locations
private int left, top;
//override a superclass method
public int displayVolume()
{
System.out.println(length*w idth*height);
System.out.println("Location: "+left+", "+top);
}
2
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
When extending a class constructor you can reuse the superclass constructor and
overridden superclass methods by using the reserved word super. Note that this reference
must come first in the subclass constructor. The reserved wordthis is used to distinguish
between the object's property and the passed in parameter.
The reserved word this can also be used to reference private constructors which are
useful in initializing properties.
Special Note:You cannot override final methods, methods in final classes, private
methods or static methods.
3. Polymorphism
• Polymorphism means to process objects differently based on their data type.
• In other words it means, one method with multiple implementation, for a certain
class of action. And which implementation to be used is decided at runtime
depending upon the situation (i.e., data type of the object)
• This can be implemented by designing a generic interface, which provides generic
methods for a certain class of action and there can be multiple classes, which
provides the implementation of these generic methods.
•
Lets us look at same example of a car. A car have a gear transmission system. It has
four front gears and one backward gear. When the engine is accelerated then depending
upon which gear is engaged different amount power and movement is delivered to the
car.
Polymorphism could be static and dynamic both. Overloading is static polymorphism
while, overriding is dynamic polymorphism.
Overloading in simple words means two methods having same method name but
takes different input parameters. This called static because, which method to be
invoked will be decided at the time of compilation
Overriding means a derived class is implementing a method of its super class
3
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
Overloaded methods are methods with the same name signature but either a different
number of parameters or different types in the parameter list. For example 'spinning' a
number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By
defining a method for handling each type of parameter you control the desired effect.
Overridden methods are methods that are redefined within an inherited or subclass.
They have the same signature and the subclass definition is used.
Dynamic (or late) method binding is the ability of a program to resolve references to
subclass methods at runtime. For example assume that three subclasses (Cow, Dog and
Snake) have been created based on the Animal abstract class, each having their own
speak() method. Although each method reference is to an Animal (but no animal objects
exist), the program is will resolve the correct method reference at runtime.
public class AnimalReference
{
public static void main(String args[])
Animal ref // set up var for an Animal
Cow aCow = new Cow ("Bossy"); // makes specific objects
Dog aDog = new Dog("Rover");
Snake aSnake = new Snake("Ernie");
// now reference each as an Animal
ref = aCow ; ref.speak();
ref = aDog; ref.speak();
ref = aSnake; ref.speak();
}
4
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
Self-Help: You can refer to the sources below to help you further
understand the lesson.
1. Schildt, Herbert. (2019) Java : a beginner's guide. McGraw-Hill/Osborne
2. Farrell, Joyce (2019) Java™ programming. Cengage Learning
3. 3G E-Learning, LLC. (2018) Theory, practice and techniques in Java programming. 3G
E-learning
4. 3G E-Learning, LLC. (2018) Theory, practice and techniques in object oriented
programming. 3G E-learning
5. Padre, Nilo et .al. (2016). Programming Concepts: Logic Formulation
6. Berkovic, et. al (2016). Computers and Programming: Theory and Problems
7. Pomperada, Jake et. al (2016). Intro to Java Programming, Mindshapers Co., Inc,
Great Books
8. Smith, Jo ann (2015). Programming Logic and Design, Cengage
1.
2.
3.
KEYWORDS INDEX
5
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
Let’s Check: Showing the output of programming segments given: Try to apply this
programming segments in Java and show the result upon running the code. Submit the
screenshot of the result in word or pdf format to our BB LMS.
Performance Task: Understand the game scenario where it has three Defenders.
These defenders differ in actions and mechanics.
Game Scenario:
Let’s look at Williams Electronics classic arcade game, Defender, and examine three
of the units in this game: the lander, the mutant and the bomber. By examining the
ways in which these three units differ within the game's framework of actions and
mechanics, you can see some of the ingredients necessary for intriguing enemy
behavior.
• The lander flew slowly and occasionally fired a shot at the player. Given
the chance, the lander would descend to the surface below to pick up
one of the humanoids that the player was supposed to be protecting.
When a lander grabbed a humanoid, it would rise toward the upper levels
of the atmosphere, suddenly firing like mad; when it reached the top of
the screen, the humanoid was destroyed and the lander would become
a mutant.
6
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
• The mutant was faster than the lander and much more aggressive – it
always made a headlong attack when it spotted the player. The mutant
also had an erratic flight pattern, making it’s movements less predictable
and harder to evade.
• The bomber was slow and peaceful for the most part, but it left a trail of
bombs that hung in space; if the player contacted one of the bombs, his
ship was destroyed.
Requirements:
a. You create your main method, assign a super class, and subclasses of the
scenario above. Apply appropriate OOP techniques display the actions and
mechanics of every Defender.
b. Submit the zipped file containing all the java files (Main, superclass and
subclasses).
In a Nutshell
Briefly discuss the difference of the Principles of OOP.
Mechanics:
A.) Any program that applies the concept of (1) add, (2) display, (3) edit, (4) delete
(5) exit. The data added must be stored in a file, display from the file, edit from
the file and can be deleted from that file. The delete must also have an option
to delete one-by-one or delete all.
B.) You can use GUI Swing, or console mode to display your output.
C.) Presentation of output must on the Final week from March 8 - March 12, 2021.
D.) No video presentation. Individual presentation will be scheduled from March 8-
12.
E.) You need to prepare a documentation, as shown template below:
Submitted to:
Prof. Fe B. Yara
7
College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116
Describe the data types, Discuss the importance Discuss what principle
why you use such data of this method in your of OOP applies in your
types to a certain program program.
variable
FileReader
Array
Discuss the importance
Discuss why you use of this method in your
array, if you use program
Arraylist, linkedlist or
vector discuss it also