Recap and Expansion

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
Adriana Picoral

Encapsulation

  • Core principle of Object-Oriented Programming (OOP)
  • Objects are instances of a class, and an object is a unit with its data (variables) and the methods that operate on that data
  • Encapsulation restricts direct access to an object’s internal state and provides controlled access through well-defined methods

Encapsulation

  • private instance variables (data hiding)
  • Getters and Setters (controlled access)

Benefits: modularity, easier to test and maintain, data integrity and validation, flexibility in refactoring

Inheritance

Use keyword extends to extend a parent class and create a subclass

  • subclass inherits instances and methods from super (parent) class
  • subclass has a constructor obligation with parent class (parent constructor always runs)
  • use super(params) in subclass constructor (needs to be first line) to satisfy parent class constructor requirements
  • use super.method() or super.variableName to access public or protected members of parent class

A good parent class

careful use of privacy:

  • public “how outsiders use my family”
  • protected “provided to my children”
  • private “I’ll handle this myself”

Gives structure and design to children

Designed with polymorphism in-mind

Quiz 08

You have 10 minutes to complete the quiz.

  • Question A:
    • mark ALL that are true
    • how many to check off is up to you
  • Question B: write down what the code output is

Polymorphism

  • Method overloading – methods with same name but different signatures in the same class
  • Method overriding – methods with the same name and signature in parent-child classes (we often override toString() and equals(Object obj))

Important syntax

instanceof – evaluates to true if and only if reference is a member of Type

variableName instanceOf Type

We use instanceof together with type casting

(SubClass) parClassVariableName

Converts a parent-class-reference (storing a child class) to a child-class reference (no actual conversion) – purpose is to call methods in subclass

Abstract Classes

Used for: representing parent classes that exist only for being a parent class.

Syntax: abstract (class keyword)

public abstract class MyClass {...}

We cannot make instances of this class (cannot use “new” keyword)

Abstract Methods

Method that exist only to provide structure

Syntax: list modifier abstract with the method

public abstract int getSomething();
  • Only allowed on abstract classes
  • Method does not have a body (end line with semi-colon)
  • Child class inherits obligation to implement
  • Java knows method exists (you can call it)

Practice – Greeter Class

The greeter classes represent various ways to greet someone

  • public: getName, greet
  • private: name – should be handled well (set in constructor)

Things to note:

  • Parent class provides structure to child classes
  • in-class polymorphic methods

Practice – main function

Greet.java

public class Greet {

    public static void main(String[] args) {
        Greeter boss = new Formal("Ms.", "Silva");
        boss.greet();

        Greeter friend = new Informal("Joe");
        friend.greet();

    }
}
Good day, Ms. Silva.
What's up, Joe?

Solution Greeter.java

public abstract class Greeter {
    private String name;

    public Greeter(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public abstract void greet();

}

Solution Formal.java

public class Formal extends Greeter {
    private String title;

    public Formal(String title, String name) {
        super(name);
        this.title = title;
    }

    @Override
    public String getName() {
        return this.title + " " + super.getName();
    }

    public void greet() {
        System.out.println("Good day, " + getName() + ".");
    }
}

Solution Informal.java

public class Informal extends Greeter {

    public Informal(String name) {
        super(name);
    }

    public void greet() {
        System.out.println("What's up, " + super.getName() + "?");
    }
}

Practice – abstract class

Goal: Create a payment system, with an abstract class called Payment and subclasses CreditCard and PayPal

Practice Instructions