INF101: Object-Oriented Programming

Lecture 19: Immutable Objects, GUI, and other fun topics Welcome to INF101 – Lecture 19! • Previously: – Immutable objects Today • GUI fun with Yngve • Repetition of Immutable objects • More fun topics Questions? Immutable Objects Remember ? • When variables are not allowed to change, we use the final keyword • Can be used to define constants, like Pi:

• If we try to change it: You can use constants to explain numbers in your code! • Compare Other things that can’t change: Strings!

https://www.programcreek.com/2009/02/diagram-to-show-java-strings-immutability/ Garbage Collector • Cleans up unreferenced objects to free up memory Java Garbage Collector • Cleans up unreferenced objects to free up memory Java Garbage Collector • Cleans up unreferenced objects to free up memory In general: immutable objects! • An object is immutable if it’s state cannot change after creation • A String is an example of an immutable object

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html 1: Creates simple, reliable code • State cannot ‘unexpectedly’ change • Elimination of code needed to protect mutable objects from corruption – Only need to check class invariant after constructor 2: Concurrent applications • When executing instructions in parallel, immutable objects cannot be corrupted by thread interference https://www.geeksforgeeks.org/thread-interference-and-memory-consistency-errors-in-java/ Step 1: Remove all setters (set methods) • But how would we adjust the color then? • We would make a new object! Step 2: Make all fields final and private • Remember the final keyword is used to mark things that cannot be changed • Private ensures that other classes can’t even see them (good practice anyway) Step 3: Don’t allow subclasses to override methods • Simplest way: declare the class final • Sophisticated way: make the constructor private and construct using factory methods Step 4: Check if your instances include refs to mutable objects • If they do, don’t allow those to be changed: – Don’t provide methods that modify the mutable objects – Don’t share references to the mutable objects Example: Immutable • Color with Red, Green, Blue value, and a name General problem • You get a hold of a reference to object A which is a field variable in another object B (via B returning A via one of B’s methods) • You can change object A • Then you can make changes in B which are not upholding the class invariant: you can make invalid field variables in B Mutable object problems • If you: – Have more than one variable referring to the same object – Return a field variable which is a mutable object – Take an argument which is a mutable object and store it in a field variable Returning and accepting mutable objects • You have more ways to access data inside the object • If you can change the object’s data without using the object’s methods: cannot guarantee to uphold class invariance Immutable Objects Wrap-up • Use where reasonable • Simple to construct, test, and use • Thread-safe • Class invariant established when constructing, never needs to be checked again Questions? Break Exercise Make this class immutable Exercise Make this class immutable Exercise Make this class immutable Exercise Make this class immutable Exercise Make this class immutable Questions? Anonymous classes When you can’t think of a good name You can make anonymous entities! • When you cannot think of a good name for something (labeltext1, labeltext, or label1label, hmm) Can also make anonymous classes • If all you ever need is a single object of a class • Define an anonymous class right on the spot where you want to instantiate it (don’t have to give it a name Syntax • New keyword + name, class body

• Class body can have fields, methods, and even other classes or interfaces ☺ Example Anonymous classes rules • Cannot make a constructor since the class does not have a name (what would the constructor be called?) • You can’t pass parameters if the anon class is based on an interface (no interface constructor) • If used in assignment statement, don’t forget the semi- colon! • Anonymous class cannot be static • Anonymous class has access to fields and methods of outer class Related: Lambda expressions • In Yngve’s code • Will be discussed by Khalid in a couple of weeks! Questions? Factory Design Patterns Factory Design Patterns • Handle the details of object creation (encapsulate) • The defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. • The provides an interface for creating families of related or dependent objects without specifying their concrete classes. Abstract Factory • When you need an instance of one of several subclasses, and you do not know which one in advance • Has one or more methods that return subclasses of the abstract class Example Factory Method https://www.baeldung.com/java-abstract-factory-pattern https://www.baeldung.com/java-abstract-factory-pattern

Design patterns in general • Best practices from experienced object-oriented software developers • Names, motivates, explains a general design that addresses a recurring design problem in OOP

https://www.geeksforgeeks.org/software-design-patterns/ Recommended reading! • Head First Design Patterns! • For instance chapter on Factory design patterns: https://www.oreilly.com/library/vie w/head-first- design/0596007124/ch04.html Questions? You are here Kodekveld tomorrow! • Same time, same place as usual! Pro tips • Use our resources for working on the obligatory: – Group hours – Kodekvelder – Facebook group – Mitt discussion – Oracle hours – Wiki pages (like Oppsummering): https://retting.ii.uib.no/inf101/inf101.v19/wikis/home For UI • Yngve’s amazing GUI: https://retting.ii.uib.no/inf101.v19.oppgaver/inf101. v19.sem2gui • Anya tip: https://retting.ii.uib.no/inf101.v16.oppgaver/inf101. v16.sem1 (don’t do the exercise, just look at the GUI stuff) What can you do? • Work on second obligatory assignment in group sessions and kodekveld Wednesday! • Java all in one, Book 3, Chapter 7 on Anonymous classes: https://ebookcentral-proquest- com.pva.uib.no/lib/bergen- ebooks/reader.action?docID=1688016&ppg=351 • Factory design patterns: https://www.oreilly.com/library/view/head-first- design/0596007124/ch04.html What’s next? • Kodekveld, Easter break, no lectures for a while so you can work on Sem2! • After Sem2, we will do exam prep with recaps and practice exam ☺ Thanks!