0% found this document useful (0 votes)
53 views

Learning Module: Object-Oriented Programming (OOP101)

This learning module covers Object-Oriented Programming concepts like polymorphism, inheritance, encapsulation, and abstraction. It includes activities, assessments, and content on polymorphism which allows an object to take on multiple forms. Polymorphism is demonstrated using parent and child class references, and virtual methods which allow overridden methods in child classes to be called. Inheritance and how child classes extend parent classes is also discussed. The module aims to help students learn and apply OOP principles in Java.

Uploaded by

Ruben San Felipe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Learning Module: Object-Oriented Programming (OOP101)

This learning module covers Object-Oriented Programming concepts like polymorphism, inheritance, encapsulation, and abstraction. It includes activities, assessments, and content on polymorphism which allows an object to take on multiple forms. Polymorphism is demonstrated using parent and child class references, and virtual methods which allow overridden methods in child classes to be called. Inheritance and how child classes extend parent classes is also discussed. The module aims to help students learn and apply OOP principles in Java.

Uploaded by

Ruben San Felipe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

NORZAGARAY COLLEGE

Municipal Compound, Norzagaray, Bulacan

LEARNING MODULE
IN
OBJECT-ORIENTED
PROGRAMMING
(OOP101)

MIDTERM
1ST SEMESTER A.Y. 2020-2021

FELIPE G. ANTE JR
CS Instructor

COLLEGE OF COMPUTING STUDIES

This learning module is for teaching and learning purposes only.


No part of this learning module shall be printed, electronically or
mechanically reproduced, and shared publicly to any form of
social media/online platforms. Anyone who will be directly and
indirectly involved shall be administratively dealt with.
TABLE OF CONTENTS

LEARNING TASK 6 ........................................................................................................................ 2


 POLYMORPHISM............................................................................................................... 2
ACTIVITY 6 ..................................................................................................................................... 2
PRE-ASSESSMENT 6 .................................................................................................................... 2
CONTENT DEVELOPMENT 6 ....................................................................................................... 2
POST-ASSESSMENT 6 ................................................................................................................. 5
LEARNING TASK 7-8 ..................................................................................................................... 5
 INHERITANCE .................................................................................................................... 5
ACTIVITY 7-8.................................................................................................................................. 6
PRE-ASSESSMENT 7-8 ................................................................................................................ 6
CONTENT DEVELOPMENT 7-8 .................................................................................................... 6
POST-ASSESSMENT 7-8 ............................................................................................................ 12
LEARNING TASK 9-10 ................................................................................................................. 12
 ENCAPSULATION............................................................................................................ 12
 ABSTRACTION ................................................................................................................ 12
PRE-ASSESSMENT 9-10 ............................................................................................................ 12
CONTENT DEVELOPMENT 9-10 ................................................................................................ 13
POST-ASSESSMENT 9-10 .......................................................................................................... 17
MIDTERM EXAMINATION ........................................................................................................... 17

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 1
LEARNING TASK 6 (April 12-17,2021)

TOPIC
 POLYMORPHISM

TOPIC OVERVIEW

 Learning Task 6 will discuss the principles and methods of Polymorphism which is one
of the most important principle of Object-Oriented Programming Approach

DESIRED LEARNING OUTCOME

1. Analyze the Polymorphism approach in OOP


2. Identify the principles and methods of Polymorphism
-------------------------------------------------------------------------------------------------------------------------------

ACTIVITY 6

NAME: _________________________________ COURSE & SECTION: _____SCORE: _____

Answer the following questions. Use a yellow paper. 30 pts.

1. Read an article about OOP Polymorphism and write your reaction.


2. In your own opinion based on your activity in item 2 what are the advantages and
disadvantages of using Polymorphism. Explain.

-------------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 6

NAME: _______________________________ COURSE & SECTION: ______ SCORE: _____

Answer the following questions:


(Write your answer in a separate sheet of paper) 30 pts.

1. In your own words define Polymorphism


2. Describe Polymorphism by creating a scenario in the real world
3. In your own opinion why is Polymorphism important in Java OOP approach. Explain.

-------------------------------------------------------------------------------------------------------------------------------

CONTENT DEVELOPMENT 6

I. POLYMORPHISM

Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered to be polymorphic.
In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type
and for the class Object.
It is important to know that the only possible way to access an object is through a reference
variable. A reference variable can be of only one type. Once declared, the type of a reference
variable cannot be changed.
The reference variable can be reassigned to other objects provided that it is not declared
final. The type of the reference variable would determine the methods that it can invoke on the
object.
A reference variable can refer to any object of its declared type or any subtype of its
declared type. A reference variable can be declared as a class or interface type.
Example:
Let us look at an example.

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 2
public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}
Now, the Deer class is considered to be polymorphic since this has multiple inheritance.
Following are true for the above examples −
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object
When we apply the reference variable facts to a Deer object reference, the following
declarations are legal −
Example
Deer d = new Deer();
Animal a = d;
Vegetarian v = d;
Object o = d;
All the reference variables d, a, v, o refer to the same Deer object in the heap.

Virtual Methods
In this section, I will show you how the behavior of overridden methods in Java allows you
to take advantage of polymorphism when designing your classes.
We already have discussed method overriding, where a child class can override a method
in its parent. An overridden method is essentially hidden in the parent class, and is not invoked
unless the child class uses the super keyword within the overriding method.

Example

/* File name : Employee.java */


public class Employee {
private String name;
private String address;
private int number;

public Employee(String name, String address, int number) {


System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}

public void mailCheck() {


System.out.println("Mailing a check to " + this.name + " " + this.address);
}

public String toString() {


return name + " " + address + " " + number;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public void setAddress(String newAddress) {


address = newAddress;
}

public int getNumber() {


return number;
}
}

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 3
Now suppose we extend Employee class as follows −
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary

public Salary(String name, String address, int number, double salary) {


super(name, address, number);
setSalary(salary);
}

public void mailCheck() {


System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName()
+ " with salary " + salary);
}

public double getSalary() {


return salary;
}

public void setSalary(double newSalary) {


if(newSalary >= 0.0) {
salary = newSalary;
}
}

public double computePay() {


System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}

Now, you study the following program carefully and try to determine its output −

/* File name : VirtualDemo.java */


public class VirtualDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}

This will produce the following result −

Output
Constructing an Employee
Constructing an Employee

Call mailCheck using Salary reference --


Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--


Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 4
Here, we instantiate two Salary objects. One using a Salary reference s, and the other using an
Employee reference e.

While invoking s.mailCheck(), the compiler sees mailCheck() in the Salary class at compile
time, and the JVM invokes mailCheck() in the Salary class at run time.
mailCheck() on e is quite different because e is an Employee reference. When the compiler
sees e.mailCheck(), the compiler sees the mailCheck() method in the Employee class.
Here, at compile time, the compiler used mailCheck() in Employee to validate this statement. At
run time, however, the JVM invokes mailCheck() in the Salary class.

This behavior is referred to as virtual method invocation, and these methods are referred to as
virtual methods. An overridden method is invoked at run time, no matter what data type the
reference is that was used in the source code at compile time.

-------------------------------------------------------------------------------------------------------------------------------

POST-ASSESSMENT 6

NAME: _______________________________ COURSE & SECTION: ______ SCORE: _____

Answer the following questions:


(Write your answer in a separate sheet of paper)

1. Given the program below. Show the output and identify the polymorphism part in this code.
(15 pts)

Animal.java

public class Animal{


public void sound(){
System.out.println("Animal is making a sound");
}
}

Horse.java

class Horse extends Animal{


@Override
public void sound(){
System.out.println("Neigh");
}
public static void main(String args[]){
Animal obj = new Horse();
obj.sound();
}
}

2. Based on your output in item 1 explain the code with Polymorphism method. (15 pts)

3. Create a program based on item 1. Create a new java files named it as Human.java and
Student.java. Show the output of your program. (20 pts)

------------------------------------------------------------------------------------------------------------------------------

LEARNING TASK 7-8 (April 19-May 1, 2021)

TOPIC
 INHERITANCE

TOPIC OVERVIEW

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 5
Learning Task 7-8 focuses on other important OOP method which is the Inheritance.

DESIRED LEARNING OUTCOMES:

1. Identify the problems and methods applied in OOP Inheritance


2. Analyze the different programming strategies in Inheritance.
-------------------------------------------------------------------------------------------------------------------------------

ACTIVITY 7-8

NAME: _________________________________ COURSE & SECTION: _____SCORE: _____

Directions: Answer the questions on a separate sheet(s). 30 pts


Your Tasks:
1. Create an algorithm for sorting numbers from 100 to 1000
2. Only Even numbers should be sort, example 100,102, 104,106…
3. Show the flowchart.

-----------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 7-8

NAME: _________________________________________ COURSE & SECTION: _________

Answer the following questions on a separate sheet of paper. 15pts each.

1. In your own words what is Binary Search? Explain.


2. In your own words what is Greedy Method? Explain.
-------------------------------------------------------------------------------------------------------------------------------

CONTENT DEVELOPMENT 7-8

I. INHERITANCE

Inheritance can be defined as the process where one class acquires the properties (methods
and fields) of another. With the use of inheritance the information is made manageable in a
hierarchical order.
The class which inherits the properties of other is known as subclass (derived class, child class)
and the class whose properties are inherited is known as superclass (base class, parent class).

extends Keyword
extends is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Sample Code
Following is an example demonstrating Java inheritance. In this example, you can observe two
classes namely Calculation and My_Calculation.
Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of
Calculation class.

Example
class Calculation {
int z;

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 6
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}

public void Subtraction(int x, int y) {


z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}

public static void main(String args[]) {


int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}
Compile and execute the above code as shown below.
javac My_Calculation.java
java My_Calculation
After executing the program, it will produce the following result −

Output
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200

In the given program, when an object to My_Calculation class is created, a copy of the contents
of the superclass is made within it. That is why, using the object of the subclass you can access
the members of a superclass.

The Superclass reference variable can hold the subclass object, but using that variable you can
access only the members of the superclass, so to access the members of both classes it is
recommended to always create reference variable to the subclass.
If you consider the above program, you can instantiate the class as given below. But using the
superclass reference variable ( cal in this case) you cannot call the method multiplication(),
which belongs to the subclass My_Calculation.
Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and nested classes) from its
superclass. Constructors are not members, so they are not inherited by subclasses, but the
constructor of the superclass can be invoked from the subclass.

The super keyword


The super keyword is similar to this keyword. Following are the scenarios where the super
keyword is used.

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 7
It is used to differentiate the members of superclass from the members of subclass, if they have
same names.

It is used to invoke the superclass constructor from subclass.


Differentiating the Members
If a class is inheriting the properties of another class. And if the members of the superclass have
the names same as the sub class, to differentiate these variables we use super keyword as
shown below.
super.variable
super.method();

Sample Code
This section provides you a program that demonstrates the usage of the super keyword.
In the given program, you have two classes namely Sub_class and Super_class, both have a
method named display() with different implementations, and a variable named num with
different values. We are invoking display() method of both classes and printing the value of the
variable num of both classes. Here you can observe that we have used super keyword to
differentiate the members of superclass from subclass.

class Super_class {
int num = 20;

// display method of superclass

public void display() {


System.out.println("This is the display method of superclass");
}
}

public class Sub_class extends Super_class {


int num = 10;

// display method of sub class


public void display() {
System.out.println("This is the display method of subclass");
}

public void my_method() {


// Instantiating subclass
Sub_class sub = new Sub_class();

// Invoking the display() method of sub class


sub.display();

// Invoking the display() method of superclass


super.display();

// printing the value of variable num of subclass


System.out.println("value of the variable named num in sub class:"+ sub.num);

// printing the value of variable num of superclass


System.out.println("value of the variable named num in super class:"+ super.num);
}

public static void main(String args[]) {


Sub_class obj = new Sub_class();
obj.my_method();
}
}
On executing the program, you will get the following result −

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 8
Output
This is the display method of subclass
This is the display method of superclass
value of the variable named num in sub class:10
value of the variable named num in super class:20

Invoking Superclass Constructor


If a class is inheriting the properties of another class, the subclass automatically
acquires the default constructor of the superclass. But if you want to call a parameterized
constructor of the superclass, you need to use the super keyword as shown below.
super(values);
Sample Code
The program given in this section demonstrates how to use the super keyword to invoke the
parametrized constructor of the superclass. This program contains a superclass and a subclass,
where the superclass contains a parameterized constructor which accepts a integer value, and
we used the super keyword to invoke the parameterized constructor of the superclass.

class Superclass {
int age;

Superclass(int age) {
this.age = age;
}

public void getAge() {


System.out.println("The value of the variable named age in super class is: " +age);
}
}

public class Subclass extends Superclass {


Subclass(int age) {
super(age);
}

public static void main(String args[]) {


Subclass s = new Subclass(24);
s.getAge();
}
}

On executing the program, you will get the following result –


Output
The value of the variable named age in super class is: 24

IS-A Relationship
IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword
is used to achieve inheritance.
public class Animal {
}

public class Mammal extends Animal {


}

public class Reptile extends Animal {


}

public class Dog extends Mammal {


}

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 9
Now, based on the above example, in Object-Oriented terms, the following are true −
Animal is the superclass of Mammal class.
Animal is the superclass of Reptile class.
Mammal and Reptile are subclasses of Animal class.
Dog is the subclass of both Mammal and Animal classes.
Now, if we consider the IS-A relationship, we can say −
Mammal IS-A Animal
Reptile IS-A Animal
Dog IS-A Mammal
Hence: Dog IS-A Animal as well
With the use of the extends keyword, the subclasses will be able to inherit all the properties of
the superclass except for the private properties of the superclass.
We can assure that Mammal is actually an Animal with the use of the instance operator.
class Animal {
}

class Mammal extends Animal {


}

class Reptile extends Animal {


}

public class Dog extends Mammal {

public static void main(String args[]) {


Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();

System.out.println(m instanceof Animal);


System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
This will produce the following result −
Output
true
true
true

Since we have a good understanding of the extends keyword, let us look into how
the implements keyword is used to get the IS-A relationship.
Generally, the implements keyword is used with classes to inherit the properties of an interface.
Interfaces can never be extended by a class.
Example
public interface Animal {
}

public class Mammal implements Animal {


}

public class Dog extends Mammal {


}

The instanceof Keyword


Let us use the instanceof operator to check determine whether Mammal is actually an Animal,
and dog is actually an Animal.
interface Animal{}
class Mammal implements Animal{}

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 10
public class Dog extends Mammal {

public static void main(String args[]) {


Mammal m = new Mammal();
Dog d = new Dog();

System.out.println(m instanceof Animal);


System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
This will produce the following result −
Output
true
true
true

HAS-A relationship
These relationships are mainly based on the usage. This determines whether a certain
class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.
Lets look into an example −
Example
public class Vehicle{}
public class Speed{}

public class Van extends Vehicle {


private Speed sp;
}
This shows that class Van HAS-A Speed. By having a separate class for Speed, we do
not have to put the entire code that belongs to speed inside the Van class, which makes it
possible to reuse the Speed class in multiple applications.
In Object-Oriented feature, the users do not need to bother about which object is doing
the real work. To achieve this, the Van class hides the implementation details from the users of
the Van class. So, basically what happens is the users would ask the Van class to do a certain
action and the Van class will either do the work by itself or ask another class to perform the
action.

Types of Inheritance
There are various types of inheritance as demonstrated below.

A very important fact to remember is that Java does not support multiple inheritance. This
means that a class cannot extend more than one class. Therefore following is illegal −
Example
public class extends Animal, Mammal{}
However, a class can implement one or more interfaces, which has helped Java get rid of the
impossibility of multiple inheritance.

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 11
POST-ASSESSMENT 7-8
NAME: ____________________________ COURSE & SECTION: ______ SCORE: ____

Answer the ff questions 15 pts each

1. Create a program that computes the area of rectangle using Superclass constructor. Show
the output.
2. Complete the code in IS-A relationship with extends keyword. Show the output.
public class Monster {
}

public class Tikbalang extends Monster {


}

public class Kapre extends Monster {


}

public class Tyanak extends Monster {


}

3. Create a Calculator program using Java inheritance. Show the output.

-------------------------------------------------------------------------------------------------------------------------------

LEARNING TASK 9-10 (April 26-May 8, 2021)

TOPICS

 ENCAPSULATION
 ABSTRACTION

TOPIC OVERVIEW

Learning Task 9-10 will discuss another set of OOP principles Encapsulation and
Abstraction

DESIRED LEARNING OUTCOME

1. Identify the techniques or methods in solving more complex OOP methods


2. Analyze all the programming strategies and apply it in creating a quality product such as
software or firmware.
-------------------------------------------------------------------------------------------------------------------------------

PRE-ASSESSMENT 9-10

NAME: _________________________________________ COURSE & SECTION: _________

Answer the following questions. Use a separate sheet of paper for your answer.

1. In your own words what is Encapsulation and Abstraction? 10 pts


2. Based on your answer in item 1 what are the use of Encapsulation and Abstraction in
OOP? 15 pts
3. Based on your answer in item 2 give at least 3 OOP Programming Language that use
the Encapsulation and Abstraction concept. Describe each. 15 pts

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 12
CONTENT DEVELOPMENT 9-10

I. ENCAPSULATION

Encapsulation is one of the four fundamental OOP concepts. The other three are
inheritance, polymorphism, and abstraction.
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting
on the data (methods) together as a single unit. In encapsulation, the variables of a class will be
hidden from other classes, and can be accessed only through the methods of their current
class. Therefore, it is also known as data hiding.

To achieve encapsulation in Java −


Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the variables values.

Example
Following is an example that demonstrates how to achieve Encapsulation in Java −

/* File name : EncapTest.java */


public class EncapTest {
private String name;
private String idNum;
private int age;

public int getAge() {


return age;
}

public String getName() {


return name;
}

public String getIdNum() {


return idNum;
}

public void setAge( int newAge) {


age = newAge;
}

public void setName(String newName) {


name = newName;
}

public void setIdNum( String newId) {


idNum = newId;
}
}
The public setXXX() and getXXX() methods are the access points of the instance variables of
the EncapTest class. Normally, these methods are referred as getters and setters. Therefore,
any class that wants to access the variables should access them through these getters and
setters.
The variables of the EncapTest class can be accessed using the following program −

/* File name : RunEncap.java */


public class RunEncap {
public static void main(String args[]) {
EncapTest encap = new EncapTest();
encap.setName("James");

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 13
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
}
}

This will produce the following result −


Output
Name : James Age : 20

Benefits of Encapsulation
 The fields of a class can be made read-only or write-only.
 A class can have total control over what is stored in its fields.

II. ABSTRACTION

As per dictionary, abstraction is the quality of dealing with ideas rather than events. For
example, when you consider the case of e-mail, complex details such as what happens as soon
as you send an e-mail, the protocol your e-mail server uses are hidden from the user. Therefore,
to send an e-mail you just need to type the content, mention the address of the receiver, and click
send.
Likewise in Object-oriented programming, abstraction is a process of hiding the
implementation details from the user, only the functionality will be provided to the user. In other
words, the user will have the information on what the object does instead of how it does it.
In Java, abstraction is achieved using Abstract classes and interfaces.

Abstract Class
A class which contains the abstract keyword in its declaration is known as abstract class.
Abstract classes may or may not contain abstract methods, i.e., methods without body ( public
void get(); )
But, if a class has at least one abstract method, then the class must be declared abstract.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, you have to inherit it from another class, provide implementations to the
abstract methods in it.
If you inherit an abstract class, you have to provide implementations to all the abstract methods
in it.

Example
This section provides you an example of the abstract class. To create an abstract class, just use
the abstract keyword before the class keyword, in the class declaration.

/* File name : Employee.java */


public abstract class Employee {
private String name;
private String address;
private int number;

public Employee(String name, String address, int number) {


System.out.println("Constructing an Employee");
this.name = name;
this.address = address;
this.number = number;
}

public double computePay() {


System.out.println("Inside Employee computePay");
return 0.0;
}

public void mailCheck() {


System.out.println("Mailing a check to " + this.name + " " + this.address);
}

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 14
public String toString() {
return name + " " + address + " " + number;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public void setAddress(String newAddress) {


address = newAddress;
}

public int getNumber() {


return number;
}
}
You can observe that except abstract methods the Employee class is same as normal class
in Java. The class is now abstract, but it still has three fields, seven methods, and one constructor.

Now you can try to instantiate the Employee class in the following way −

/* File name : AbstractDemo.java */


public class AbstractDemo {

public static void main(String [] args) {


/* Following is not allowed and would raise error */
Employee e = new Employee("George W.", "Houston, TX", 43);
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
When you compile the above class, it gives you the following error −
Employee.java:46: Employee is abstract; cannot be instantiated
Employee e = new Employee("George W.", "Houston, TX", 43);
^
1 error

Inheriting the Abstract Class


We can inherit the properties of Employee class just like concrete class in the following way −

Example
/* File name : Salary.java */
public class Salary extends Employee {
private double salary; // Annual salary

public Salary(String name, String address, int number, double salary) {


super(name, address, number);
setSalary(salary);
}

public void mailCheck() {


System.out.println("Within mailCheck of Salary class ");
System.out.println("Mailing check to " + getName() + " with salary " + salary);
}

public double getSalary() {


return salary;
}

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 15
public void setSalary(double newSalary) {
if(newSalary >= 0.0) {
salary = newSalary;
}
}

public double computePay() {


System.out.println("Computing salary pay for " + getName());
return salary/52;
}
}
Here, you cannot instantiate the Employee class, but you can instantiate the Salary Class, and
using this instance you can access all the three fields and seven methods of Employee class as
shown below.

/* File name : AbstractDemo.java */


public class AbstractDemo {
public static void main(String [] args) {
Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
System.out.println("Call mailCheck using Salary reference --");
s.mailCheck();
System.out.println("\n Call mailCheck using Employee reference--");
e.mailCheck();
}
}
This produces the following result −
Output
Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0

Call mailCheck using Employee reference--


Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.0

Abstract Methods
If you want a class to contain a particular method but you want the actual implementation of that
method to be determined by child classes, you can declare the method in the parent class as an
abstract.
abstract keyword is used to declare the method as abstract.
You have to place the abstract keyword before the method name in the method declaration.
An abstract method contains a method signature, but no method body.
Instead of curly braces, an abstract method will have a semoi colon (;) at the end.
Following is an example of the abstract method.

Example
public abstract class Employee {
private String name;
private String address;
private int number;

public abstract double computePay();


// Remainder of class definition
}
Declaring a method as abstract has two consequences −
The class containing it must be declared as abstract.
Any class inheriting the current class must either override the abstract method or declare itself as
abstract.
Note − Eventually, a descendant class has to implement the abstract method; otherwise, you
would have a hierarchy of abstract classes that cannot be instantiated.

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 16
Suppose Salary class inherits the Employee class, then it should implement
the computePay() method as shown below −

/* File name : Salary.java */


public class Salary extends Employee {
private double salary; // Annual salary

public double computePay() {


System.out.println("Computing salary pay for " + getName());
return salary/52;
}
// Remainder of class definition
}

POST-ASSESSMENT 9-10
NAME: ____________________________ COURSE & SECTION: ______ SCORE: ____

Programming and Analysis (20 pts each)

1, Create a program using Encapsulation to Compute for the Area of Square.


Your Tasks:
A. Identify the Encapsulation Code in your Program
B. Based on your code describe how do you use encapsulation.
C. Show the output of your program.
2. Create a program using Abstraction to compute for the grade of the students (prelim, midterm
and fin).
A. Identify the Abstraction Code in your Program
B. Based on your code describe how do you use abstraction.
C. Show the output of your program.
3. Create the flowchart based on your 2 programs.

OOP101 Property of Norzagaray College


Object Oriented Programming

Page | 17

You might also like