My notes OOP exam notes
My notes OOP exam notes
1
Feature Abstract Class Interface
Instantiation Cannot be instantiated directly. Cannot be instantiated directly.
Supports single inheritance (a
class can extend only one abstract Supports multiple inheritance (a class can
Inheritance class). implement multiple interfaces).
Methods Can have: Can have:
- Abstract methods (no body). - Abstract methods (implicitly public abstract).
- Default methods (instance methods with a
- Concrete methods (with body). body, introduced in Java 8).
- Static methods (with body, introduced in Java
- Static methods (with body). 8).
- Private methods (helper methods, introduced
in Java 9).
Can have instance fields and static Can only have public static final fields
Fields fields. (constants).
Can have constructors, but only to
initialize fields or setup logic for
Constructors subclasses. Cannot have constructors.
Access Methods can have any access
Modifiers modifier (public, protected, Methods are implicitly public unless explicitly
(Methods) private). private (allowed for helper methods in Java 9).
Access
Modifiers Fields can have any access
(Fields) modifier. Fields are implicitly public static final.
Inheritance Use the extends keyword for Use the implements keyword for
Keyword inheritance. implementation.
Static Methods Allowed, with a body. Allowed, with a body (introduced in Java 8).
Default
Methods Not allowed. Allowed (introduced in Java 8).
Best for base classes that provide
partial or complete Best for defining behavior contracts that
Use Case implementation. unrelated classes can implement.
Multiple Not supported (class can extend Supported (class can implement multiple
Inheritance only one abstract class). interfaces).
Type of
Relationships Defines an "is-a" relationship. Defines a "can-do" or "capable-of" relationship.
// Interface definition
interface Measurable {
double getMeasure(); // Interface method
}
2
// Class implementing the interface
class Coin implements Measurable {
private double value;
private String name;
// Constructor
public Coin(double value, String name) {
this.value = value;
this.name = name;
}
public DataSet() {
data = new Measurable[10];
size = 0;
}
3
}
}
return max;
}
}
// Main class
public class Main {
public static void main(String[] args) {
// Create a DataSet object
DataSet coinData = new DataSet();
4
public class Meal implements Measurable{
private String name;
private int price;
5
private String name;
private int price;
private int barCode;
import java.util.ArrayList;
6
System.out.println(maxPrice instanceof Meal);
ArrayList<Object> a = new ArrayList<>();
a.add(maxPrice);
System.out.println(a.get(0).toString());
}
}
import java.util.*;
public ShoppingBag(){
this.shoppingList = new ArrayList<Measurable>();
}
Lab 6
1) Reading file to find number of words, lines and chars
import java.io.*;
7
public static void processFile(String fileName) {
// reset counters
chars = 0;
words = 0;
lines = 0;
try {
// open file stream
BufferedReader br = new BufferedReader(new FileReader(fileName));
// count words
String[] wordsInLine = line.split(" ");
words += wordsInLine.length;
8
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
try {
// open file stream
BufferedReader br = new BufferedReader(new FileReader(file));
9
br.close();
} catch (IOException e) {
System.out.println(e);
}
if (newBalance < 0) {
10
throw new IllegalArgumentException("Cannot withdraw more than the current
balance.");
}
balance = newBalance;
}
public double getBalance() {
return balance;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// Constructor
public MouseListenerExample() {
// Set up the JFrame
setTitle("MouseListener Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a label
label = new JLabel("Mouse Event Tracker");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 18));
add(label);
setVisible(true);
}
@Override
public void mouseClicked(MouseEvent e) {
label.setText("Mouse clicked at: (" + e.getX() + ", " + e.getY() + ")");
}
11
@Override
public void mousePressed(MouseEvent e) {
label.setText("Mouse pressed at: (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseReleased(MouseEvent e) {
label.setText("Mouse released at: (" + e.getX() + ", " + e.getY() + ")");
}
@Override
public void mouseEntered(MouseEvent e) {
label.setText("Mouse entered the frame.");
}
@Override
public void mouseExited(MouseEvent e) {
label.setText("Mouse exited the frame.");
}
FLOWER DRAWING
import javax.swing.*;
import java.awt.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
12
// Draw the flower
drawFlower(g2d, getWidth() / 2, getHeight() / 2, 100);
}
// Draw petals
g2d.setColor(Color.PINK);
for (int i = 0; i < petalCount; i++) {
double angle = Math.toRadians((360.0 / petalCount) * i);
int petalX = (int) (x + Math.cos(angle) * size / 2) - petalWidth / 2;
int petalY = (int) (y + Math.sin(angle) * size / 2) - petalHeight / 2;
frame.setVisible(true);
}
}
13
14