OOP Review
Object Oriented Programming
Let's make a game!
- The best way to learn is by doing
- Completely voluntary
- Review code at the end, dicuss
- No right answer, I haven't done it yet either
Instructions
- Pick one class
- Implement It
- Share it with me
- We'll review implementations anonymously
Basic Rules of Super Great Minimonopoly
- 2-4 Players
- You lose if you have less than $10
- Money only comes in units of $10
- If you pass/land on GO, you get $200
- Free parking is just that, free parking
- You have to pay $10 to get out of jail, you can't wait
- Goto jail, sends you to jail
-
Consider all cards of the same color identical with regard to
rent, price, etc.
Onto the Review!
(content taken from previous lectures)
Object Oriented Programming
- Allows us to model a problem using objects
- Objects contain both data and function
-
Objects of same class have the
same data and functions
- Data: Fields
- Functions: Methods
Class
- Contains the definition for a family of objects
- Like a blueprint that all objects of that type follow
public class Cat{
String name;
String color;
public Cat(String name, String color){
this.name = name;
this.color = color;
}
public void meow(){
System.out.println("MEOW!");
}
}
Using a Class
-
Make new objects using new
keyword
-
Interact with a particular instance by using its variable.
public class TestCat{
public static void main(String[] args){
Cat mrWhiskers = new Cat("Mr.Whiskers",
"Gray");
Cat fuzzles = new Cat("Fuzzles", "Black");
mrWhiskers.meow();
}
}
Inheritance
-
Define a heirarchy of functionality
-
A subclass
extends a
superclass
- A subclass may:
- Add new functionality
- Use inherited functionality
- Override inherited functionality
How Access Modifiers Affect Inheritance
public
accessible to subclass and everyone else
private
not accessible to subclass or anyone else
-
protected
accessible to subclass
and other classes in the package
this
vs super
this
- Refers to the current instance of the class that we're working with
super
- Refers to the immediate superclass to the current class
Static
- Work at the class level, not instance
-
Fields that describe the class regardless of
any particular instance that you are working with.
-
Methods that perform a function or action separate from any particular
instance.
Initialization Blocks
- Initialization blocks - per instance
public class SimpleClass{
{
System.out.println("Instance!");
}
}
Static Initialization - once per class
public class SimpleClass{
static {
System.out.println("Static!");
}
}
Order of Initialization
- Once per class
- Static variable declaration
- Static block
- Once per instance
- Variable declaration
- Initialization block
- Constructor
Final
- Final classes cannot be extended. No subclasses
final class CantBeExtended {}
- Final methods cannot be overridden
final void NoOverride() {}
- Final variables cannot have their values changed
final int one = 1;
Abstract
- Allows us to design the behaviors of a class without implementing it
-
Any class that extends a class that is abstract must either:
- Implement all of the abstract methods from the abstract class
- Also be declared abstract
Interfaces
- No method implementation
- All methods are implicitly public and abstract
- Can have variables (very rare)
- Variables are implicitly final and static
- Interfaces can extend other interfaces
- Classes can implement multiple interfaces
Interfaces
interface CanSee { void see(); }
interface CanMove { void move(); }
interface CanFly extends CanSee, CanMove {
void fly();
}
public class Bird implements CanFly{
public void see() { };
public void move() { };
public void fly() { };
}