0% found this document useful (0 votes)
150 views28 pages

Cos 202

The document covers key concepts of Object-Oriented Programming (OOP) in Java, including polymorphism, abstract classes, and interfaces. It explains compile-time and runtime polymorphism, the advantages and disadvantages of these concepts, and the importance of class hierarchies and packages for organizing code. Additionally, it discusses the Java API and its core packages, along with examples of using collections and iterators.

Uploaded by

linusyadak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views28 pages

Cos 202

The document covers key concepts of Object-Oriented Programming (OOP) in Java, including polymorphism, abstract classes, and interfaces. It explains compile-time and runtime polymorphism, the advantages and disadvantages of these concepts, and the importance of class hierarchies and packages for organizing code. Additionally, it discusses the Java API and its core packages, along with examples of using collections and iterators.

Uploaded by

linusyadak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

COMPUTER PROGRAMMING II

COS 202

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 1


OBJECT ORIENTED PROGRAMMING
Polymorphism, abstract classes, and interfaces are the fundamental
concepts of object-oriented programming (OOP):
1. Polymorphism in Java:
Polymorphism in Java allows objects of different types to be treated as
objects of a common base type. There are two types of polymorphism
in Java: compile-time polymorphism (method overloading) and runtime
polymorphism (method overriding).
a. Compile-time Polymorphism:
Compile-time polymorphism occurs when multiple methods have the
same name but different parameter lists in the same class. The compiler
determines which method to invoke based on the method signature.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 2


OBJECT ORIENTED PROGRAMMING CONT.
Method overloading Examples
class MathOperationOne {
class MathOperations {
int divide(int a, int b) {
int add(int a, int b) {
return a / b;
return a + b;
}
}

double divide(double a, double b) {


double add(double a, double b) {
return a / b;
return a + b;
}
}
}
}

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 3


OBJECT ORIENTED PROGRAMMING CONT.
// Usage
MathOperations mathOps = new MathOperations();
MathOperationOne mathOne = new MathOperationOne();

int result1 = mathOps.add(2, 3); // Calls the first add method

double result2 = mathOps.add(2.5, 3.5); // Calls the second add method

int result3 = mathOne.divide(8, 2); // Calls the first divide method

double result4 = mathOne.divide(10.5, 2.5); // Calls the second divide method

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 4


OBJECT ORIENTED PROGRAMMING CONT.

b. Runtime Polymorphism:
Runtime polymorphism occurs when a subclass provides a specific
implementation for a method that is already defined in its superclass.
This is achieved through method overriding.
A source code example follow in the next slide

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 5


OBJECT ORIENTED PROGRAMMING CONT.

class Animal { class Cat extends Animal {


void makeSound() { void makeSound() {
System.out.println(“Call animals"); System.out.println(“Call a
} Cat");
}
}

class Dog extends Animal { }


void makeSound() {
System.out.println(“Call Dogs");
}
}

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 6


OBJECT ORIENTED PROGRAMMING

// Usage
Animal myDog = new Dog(); // object of Dog class
Animal myCat = new Cat(); // object of Cat class

myDog.makeSound(); // Outputs “Call Dog"


myCat.makeSound(); // Outputs “Call Cat"

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 7


OBJECT ORIENTED PROGRAMMING
Advantages of polymorphism
It helps programmers reuse the code and classes once written, tested and implemented.
They can be reused in many ways.
Single variable name can be used to store variables of multiple data types(Float, double, Long,
Int etc).
Polymorphism helps in reducing the coupling between different functionalities.

Disadvantages of Polymorphism
One of the disadvantages of polymorphism is that developers find it difficult to implement
polymorphism in codes.
Run time polymorphism can lead to the performance issue as machine needs to decide
which method or variable to invoke so it basically degrades the performances as decisions
are taken at run time.
Polymorphism reduces the readability of the program. One needs to identify the runtime
behavior of the program to identify actual execution time.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 8


OBJECT ORIENTED PROGRAMMING
2. Abstract Classes in Java:
An abstract class in Java is a class that cannot be instantiated and is
meant to be subclassed. It may contain abstract methods (methods
without its implementation) that must be implemented by concrete
subclasses.

abstract class Shape {

abstract double area();

abstract double perimeter();

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 9


OBJECT ORIENTED PROGRAMMING
double area() {
class Rectangle extends Shape { return width * height;
double width; }
double height;
double perimeter() {
Rectangle(double width, double return 2 * (width + height);
height) {
}
this.width = width;
}
this.height = height;
}

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 10


OBJECT ORIENTED PROGRAMMING

// Usage
Shape rectangle = new Rectangle(4, 5);

System.out.println(rectangle.area()); // Outputs 20.0

System.out.println(rectangle.perimeter()); // Outputs 18.0

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 11


OBJECT ORIENTED PROGRAMMING CONT.
Advantages of Abstraction
Simplifies the complexity of code by hiding the implementation details and makes code
easier to update and maintain.
Improves code reusability and readability and provides a clear and concise view of the
system architecture.
Facilitates easier testing and debugging of the code.
Helps in enforcing guidelines and coding standards.
Disadvantage of Abstraction
Using abstraction in a program too much can reduce performance.
It can make it more difficult for developers to understand how different components of the
system interact with each other.
Most software does not require Abstraction, as sometimes it is more straightforward to write
code without Abstraction.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 12


OBJECT ORIENTED PROGRAMMING CONT.

3. Interfaces in Java:
An interface in Java is a collection of abstract methods that can be
implemented by any class. It provides a way to achieve multiple
inheritance in Java.
Example:
public interface Shape {
public double area();
public double perimeter();
}

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 13


OBJECT ORIENTED PROGRAMMING
class Circle implements Shape public double perimeter() {
{
return 2 * Math.PI * radius;
double radius;
}
}
Circle(double radius) {
this.radius = radius;
// Usage
}
Shape circle = new Circle(3);
public double area() {
System.out.println(circle.area());
return Math.PI * radius *
radius; System.out.println(circle.perimeter());

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 14


CLASS HIERARCHIES AND PROGRAM
ORGANIZATION
Class Hierarchies:
All object-oriented languages (e.g., JAVA) allow you to organize your
classes in a way that allows you to take advantage of the commonality
between classes.

That is, we can define a class with certain attributes (and/or behaviors)
and then specify which other classes share those same attributes
(and/or behaviors).

As a result, we can greatly reduce the amount of duplicate code that


we would be writing by not having to re-define the common attributes
and/or behaviors for all of the classes that share such common
features. 

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 15


CLASS HIERARCHIES AND PROGRAM
ORGANIZATION CONT.

JAVA accomplishes this task by arranging all of its classes in a "family-


tree”- like ordering called a class hierarchy. 
A class hierarchy is often represented as an upside down tree (i.e., the
root of the tree at the top). 


COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 16


CLASS HIERARCHIES AND PROGRAM
ORGANIZATION CONT.
Class Hierarchies: are concept we used in structuring and organizing
our source codes in a modular and more maintainable manner.
Class hierarchies involve organizing classes in a hierarchical structure
where classes inherit from other classes. This forms the basis for
polymorphism and code reuse through inheritance.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 17


CLASS HIERARCHIES AND PROGRAM ORGANIZATION CONT.

class Mammal extends Animal {


Example of a Class Hierarchy: void giveBirth() {
System.out.println("Mammal gives
class Animal { birth");
void makeSound() { }
}
System.out.println("Animal
makes a sound"); class Dog extends Mammal {
} void makeSound() {
System.out.println("Dog barks");
}
}
}
In this example, Dog is a subclass of Mammal, and Mammal is a subclass of Animal. This
hierarchy allows for common behavior in the Animal class to be inherited and overridden as
needed in subclasses.
COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 18
CLASS HIERARCHIES AND PROGRAM ORGANIZATION CONT.

Program Organization Using Packages:


Packages are used in Java to organize classes into namespaces,
providing a way to group related classes and avoid naming conflicts.
They also help in creating modular and scalable applications.
Example of Package Usage:
Consider a simple package structure for managing input classes:
java.util.Scanner;

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 19


CLASS HIERARCHIES AND PROGRAM ORGANIZATION CONT.
Importing Classes from Packages:
Classes from other packages need to be imported to be used. Import
statements are used to bring in classes from specific packages.
Syntax:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
Example of Import Statements:
import javax.swing.JOptionPane;
import java.awt.JButton;
Import java.awt.*;

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 20


CLASS HIERARCHIES AND PROGRAM ORGANIZATION CONT.
Advantages of Packages and Class Hierarchies
Modularity:
Packages provide a way to organize code into modular units, making it easier to
understand and maintain over a long period of time.
Namespace Management:
Packages help avoid naming conflicts by providing a namespace for classes.
Code Reusability:
Class hierarchies enable code reuse through inheritance, allowing common
behavior to be shared among related classes.
Scalability:
As projects grow, packages allow for the organization of code into logical units,
making it easier to scale and manage.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 21


USE OF API (APPLICATION PROGRAMMING INTERFACE)

The Java API (Application Programming Interface) is a vast collection


of classes, interfaces, methods and packages that form the core
foundation for Java programming.
It provides a set of rules and tools for building Java applications,
allowing developers to interact with various aspects of the Java
platform. The Java API is organized into different packages, each
serving a specific purpose.
The Java API is organized into packages, which are containers for
related classes and interfaces.
Common packages include java.lang, java.util, java.io, java.net,
java.awt, javax.swing, and many more.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 22


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.
Core Packages:
java.lang:
Contains fundamental classes and is automatically imported into every Java
program.
Includes classes such as Object, String, Math, System, and primitive data
types.
java.util:
Provides utility classes for data structures (e.g., collections, arrays), date and
time handling, and more. Includes classes like ArrayList, HashMap, Date,
and Random.
java.io:
Deals with input and output operations, including reading and writing to files
and streams.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 23


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.

java.awt and javax.swing:


Deal with Abstract Window Toolkit (AWT) and Swing for creating
graphical user interfaces.
Include classes for components (buttons, panels), layout managers,
events, and more.
Some commonly used interfaces and classes include
iterators/enumerators, List, Stack, and Queue.

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 24


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.
Iterators/Enumerators:
Iterators and enumerators provide a way to traverse elements in a
collection sequentially. They allow you to access elements of a
collection without exposing its underlying structure.
Iterator is a classic design pattern for walking through a data structure
without having to expose the details of how data is stored in the data
structure.
Also used for-each loops:
for(String element: collection){
System.out.print(element + " ");
}

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 25


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.

The ArrayList Class


A collection is a group of related objects, or elements, that are stored
together as a single unit. An array is an example of a collection. Java
also
contains a collections framework, which provides classes for
implementing
collections. One such class is the ArrayList class, which includes
methods
for adding and deleting elements and finding an element. 

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 26


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.
The ArrayList Class Example:

myStrings.add(“CCC");
import java.util.ArrayList;
myStrings.add(“DDD");
public class TestArrayList {
myStrings.add(“NSS");
public static void main(String[] args) {
myStrings.remove(3);
ArrayList<String> myStrings = new
ArrayList<String>(); for (Object name : myStrings) {
System.out.println(name);
myStrings.add(“AAA"); }
}
}
myStrings.add(“BBB");

// Output AAA, BBB, CCC and
NSS

COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 27


USE OF API (APPLICATION PROGRAMMING
INTERFACE) CONT.
Example of Iterator:
import java.util.ArrayList;
fruits.add("Orange");
import java.util.Iterator;
import java.util.List;
// Using Iterator
Iterator<String> iterator =
public class IteratorExample { fruits.iterator();
public static void main(String[] while (iterator.hasNext()) {
args) {
System.out.println(iterator.
ArrayList<String> fruits = new next());
ArrayList<String>();
}
fruits.add("Apple");
}
fruits.add("Banana");
}
// output Apple, Banana and Orange.
COS 202 [COMPUTER PROGRAMMING II] 4/15/2025 28

You might also like