Professional Documents
Culture Documents
Lucian Bredean
Class versus Object
Class Object
o ABSTRACTION
- The ability to design class structure in such way that only essential details are displayed to the user,
while the irrelevant details are ignored.
- In OOP, abstraction also means hiding implementation details from the client code.
o ENCAPSULATION
₋ The principle according which all the related information (attributes) and the operations which can
be performed on this data (methods) into the same structure (class).
₋ In addition, another purpose is to control the access to all these items.
Task 1:
- Create a new maven project. Create two different packages within “src/main/java” path.
- Create two classes with the same name, each in different package
- Add in each of the class four field using the four access modifiers
- Create a third class with the main function; instantiate the previously created classes and try to access
their attributes using “.”
OOP principles
o ENCAPSULATION
₋ The instruments we can use to obtain encapsulation are:
• private attributes
• getter methods to read the attributes
• setter methods to change/set the attributes
• Constructor (with/without parameters)
Task 2:
- Create a class “Person” which should model a Person from the municipality point of view
- Create getters and setters for the attributes
- Create a method “getPersonDetails” which would print the information related to that person
- Create an instance of the class and an application which would call the “getPersonDetails” and print the
return value
OOP principles – Abstraction & Encapsulation exercises
OOP principles
o INHERITANCE
- It is a powerful mechanism which allows achieving code reuse
- Facilitates the creation of hierarchical models -> easy to understand: e.g. vehicles can be categorized as
two-wheelers, three-wheelers, four-wheelers etc. each having their own subclasses and particular
characteristics
- Extended class <-> parent class; the class extending a class <-> child class/derived class
- Inheritance can be referred to as an “IS-A” relationship
OOP principles - Inheritance
Task 3:
- Create a “Shape” class with a single integer colorCode attribute which should be made accessible only
for derived classes
- Define a “Circle” subclass
- It should contain a radius field visible only inside the class, a area method to compute the area and a
fillColor method which should print the color code
public class Shape {
protected int colorCode;
}
void printFillColor() {
System.out.println(colorCode);
}
}
OOP principles - Inheritance
Task 4:
- Create a “TestNumber” class containing a utility method which computes the sum for an array of
numbers
- Create a number array holding a Byte, an Integer, a Float and a Double and call the method to compute
the sum
public class TestNumber {
public static double sum(Number []nums) {
double sum = 0.0;
for(Number num : nums) {
sum += num.doubleValue();
}
return sum;
}
Task 5:
- Create a “Vehicle” class having a single attribute called maxSpeed visible only inside the class
- Make sure that it will be impossible to create an instance of the class without specifying a value for
maxSpeed
- Allow reading access for the defined maxSpeed attribute
Task 6:
- Create a “Car” class; it should use the code already written for “Vehicle” class
- It should also have the isConvertible attribute, visible only inside the class with a default value of false
- Allow reading access for the defined isConvertible attribute
What would you change to make the maxSpeed field directly visible inside Car class?
protected int maxSpeed; //inside class Vehicle
What do you think would happen if we would declare a double private field called the same inside Car
class?
private double maxSpeed;
Inside a new runnable class, create a method capable of printing the maxSpeed value of any Vehicle.
Is it possible to use it in order to print the maxSpeed for a Car?
Task 7:
Define a method which computes the sum of integer numbers passed as an array.
public static int sum(int[] numbers) {
Integer sum = 0;
for (Integer number : numbers) {
sum += number;
}
return sum;
}
OOP principles – Inheritance. Static Polymorphism.
We might need to compute the sum for a set of numbers passed as a single string where the numbers are
separated using comma.
public static int sum(String numbersAsString) {
String[] numbers = numbersAsString.split(",");
int sum = 0;
for (String number : numbers) {
sum += Integer.parseInt(number);
}
return sum;
}
- Both can be used with initialize blocks, variables, methods and classes
- A static variable is associated with the enclosing class and it is called a “class variable” and it shares
the same state with all class instances; other variables are called “instance variables” and have a
state per instance
- A static method can only access static variables/call static methods
public class Counter {
private static int count;
public Counter() {
count++;
}
- The main method, where the program execution starts, is always declared static
- A static method cannot be overridden -> “this”/“super” cannot be used inside a static method
- A final class is a non-inheritable class, so it cannot be extended; all methods of a final class are
implicitly final
- A final variable can be assigned only once: at declaration or within the constructor
Small exercise: Try, in turn, to change to final the method “public double area() ” in class “Shape” and
then the entire class.
OOP concepts – Abstract classes and methods.
Abstract classes are used when you want to define an abstraction with some common functionality
but you also want to prevent instantiation of that abstraction.
abstract class Shape {
public double area() { return 0; }
}
If we do not want to provide a default implementation but we want to make sure that any subclass
will provide one, we can declare the method abstract as well.
abstract class Shape {
public abstract double area();
}
To Remember: The “abstract” keyword can be used with classes and non-static methods of a class.
OOP concepts – Interfaces.
An interface can be seen as a set of abstract methods that defines a protocol.
It is said that a class implements a interface, whenever the class adheres to the protocol defined by
the interface -> all methods defined by the interface have to be implemented in the class
public interface Colourable {
int DEFAULT_COLOR_CODE = 0;
void render();
}
To remember:
- an interface cannot be instantiated but the interface type can be used as variable/parameter type
- an interface can extend another interface
- all fields declared inside an interface are “public static final”
- an interface can have abstract/default/static methods
- all methods declared in an interface are by default considered to be abstract
- default methods must have an implementation and are instance methods
- static methods will be class methods for the classes implementing the interface
OOP concepts – Nested classes.
public Color() {
this(0, 0, 0);
}
To remember:
- An inner nested class (or interface) declared inside an interface is implicitly public and static
- Static nested classes can access (static) members of the outer class
- The outer class can access members (even private ones) of the nested static class
Inner Classes
You can define a class (or interface) as a non-static member inside another class.
Task 9:
Add the “Point” class as an inner class for the Circle class. Add a constructor receiving three parameters:
radius, x coordinate, y-coordinate and use the inner Point class to initialize the “center” field for the Circle
instances.
Try to create an instance of the inner Point class somewhere outside the Circle class.
System.out.println(Shape.Color.getDescriptiveColor(shapeColor).toString());
OOP concepts – Nested classes.
Task 11:
What point do you think it has the “final” keyword for the Shape.Color color parameter? What if that “final”
would be removed?
To remember:
- the variables passed to a local inner class have to be final; the compiler treats them as effectively
final, so they cannot be changed once passed to the local inner class
- interfaces cannot have local classes and local interfaces cannot be created
- a local inner class can access all the variables available in the enclosing code block
OOP concepts – Nested classes.
- An exception is an unwanted or unexpected event which occurs during the program execution and
disrupts the normal flow of the program
- Whenever such situation occurs, the enclosing method creates an Exception object and hands it
over to JVM -> throwing an exception
- the list of the methods which had been called if known as “Call Stack”
OOP concepts – Exceptions
- Errors: indicate a serious problem that should not be handled by the application; it is not checked at
compile time
- Exceptions:
• Checked Exceptions: checked at compile time; when such exception is thrown, then the
exception must be either handled or thrown again
• Unchecked Exceptions: not checked at compile time -> not mandatory to be handled
- try-catch-finally mechanism allows the handling of multiple exceptions in the same catch clause
try {
int[] intArray = new int[]{1, 4};
int y = 0;
int x = intArray[5] / y;
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println("ArithmeticException or ArrayIndexOutOfBoundsException caught!");
} catch (Exception e) {
System.out.println("Another exception caught!");
}
OOP concepts – Exceptions
Let’s define an enum type, containing constants which describe printer types:
public enum PrinterType {
DOTMATRIX, INKJET, LASER;
}
- name() -> returns the name of the enum constant, exactly as declared in its enum declaration;
cannot be overridden
- toString() -> returns the name of this enum constant, as contained in the declaration; this method
may be overridden
- ordinal() -> returns the ordinal of this enumeration constant (its position in its enum declaration,
where the initial constant is assigned an ordinal of zero)
OOP concepts – Enum data type.
Task 14:
Create method which would return/print a description based on the printer type.
Task 15:
Could the enum be enriched with a page print capacity information?
To remember:
- enums are implicitly public, static and final -> cannot be extended
- equals() method can be called to compare enumeration constants; if the constants are defined in
different enumerations, then the method call returns false – the check is made only at runtime
- the comparison of enum constants can also be made using == operator; in this case, type safety is
assured: if enum constants belonging to different enums are compared, this is detected at compile
time
Java Collections
- In Java, the collections are structured under different interfaces related based on inheritance.
Java Collections
List interface:
- any list can contain duplicates
- the elements are ordered
- implementations: ArrayList, LinkedList
- operations like add(), remove(), get(), set() are allowed
• ArrayList – resizable array, fast for search but slow for inserts/deletes
• LinkedList – doubly linked list, fast for inserts/deletes but slow for search
• ArrayDeque – resizable array with double-ended queue functionality
• PriorityQueue – the elements can be ordered according to their natural order or according to a
Task 16: specified comparator
Create an ArrayList containing Point objects. Add several Point instances to the ArrayList. Try to remove one
of the Point instances from the list.
Java Collections
Task 16:
Create an ArrayList containing Point objects. Add several Point instances to the ArrayList.
Try to remove one of the Point instances from the list.
Task 17:
Create a priority queue and add several Point objects inside.
Set interface:
- does not contain allow duplicates
- implementations: TreeSet, HashSet, LinkedHashSet
• TreeSet – it stores the elements in a sorted order inside a tree data structure; the order of
elements is in the natural order or according to a comparator
• HashSet – internally it uses a hash table; it is fast for searching and retrieving elements
• LinkedHashSet – similar to the HashSet; the difference relies in the fact that it is used when the
iteration order matters: it iterates through the elements in the order they were inserted
Java Collections
Task 18:
Create a TreeSet Point objects. Add several Point instances to it; try to add at least one duplicate.
Should it work if the Point class does not implement equals?
Try to remove one of the Point instances from the Set.
Task 19:
Create a HashSet Point objects. Add several Point instances to it; try to add at least one duplicate.
Should it work if the Point class does not implement equals?
Try to remove one of the Point instances from the Set.
Java Collections
Map interface:
- it’s also know as a dictionary: stores key-value pairs
- does not contain allow duplicate keys
- implementations: TreeMap, HashMap, HashLinkedMap, HashTable
• TreeMap - it stores the key-value pairs in a sorted order inside a tree data structure; the order
of elements is in the natural order or according to a comparator
• HashMap - internally it uses a hash table to store the key-value pairs; it is fast for searching and
retrieving elements; it does not maintain the insertion order of the elements
• Hashtable – very similar to the HashMap; compared to that, it is synchronized and does not
allow null keys/values
• HashLinkedMap –– similar to the HashMap; the difference relies in the fact that it is used when
the iteration order matters: it iterates through the elements in the order they were inserted
Java Collections
Task 20:
Create a TreeMap Point associating Point objects to their String representation. Add several Point-String
pairs to it; try to add at least one duplicate.
Should it work if the Point class does not implement equals?
Try to remove one of the Point-String pairs from the Map.
Task 21:
Create a HashMap Point associating Point objects to their String representation. Add several Point-String
pairs to it; try to add at least one duplicate.
Should it work if the Point class does not implement equals?
Try to remove one of the Point-String pairs from the Map.