OOP Concepts

Updated: 03 September 2023

Class

  • A blueprint for an object
  • Fields
  • Parameters
  • Methods

Inheritance

  • What classes have in common
  • Abstracted features
  • Override or extend methods
  • Avoid duplicate
  • Anything in superclass will be reflected in all subclasses

Main

  • Create objects
  • Allow them to interact

Encapsulation

Allows us to validate or perform input information for a field and use Setters and Getters

Instance vs Local variables

  • Instance - Defined inside of a class
  • Local - Defined in a method

Polymorphism

  • Allow the subclass to redifine methods in the parents
  • Refer to subclasses by their superclass type
  • Can treat the objects as if they’re of the superclass type
  • Cannot access methods that only exist in the subclass

Abstract classes

  • Cannot be an object in itself
  • May contain state and/or implementation
  • Subclasses cannot inherit protected fields

Interface

  • Does not contain an implementation
  • Only abstract methods
  • Allows classes of different inheritance trees can still communicate
  • Avoid interfaces that just force redefinition by subclasses

Code

Animal.java

1
public class Animal {
2
3
private String name;
4
private double height;
5
private int weight;
6
private String favFood;
7
private double speed;
8
private String sound;
9
10
public void setName(String newName){ name = newName; }
11
public String getName(){ return name; }
12
13
public void setHeight(double newHeight){ height = newHeight; }
14
public double getHeight(){ return height; }
15
16
public void setWeight(int newWeight){
17
if (newWeight > 0){
18
weight = newWeight;
19
} else {
20
System.out.println("Weight must be bigger than 0");
21
}
22
}
23
public double getWeight(){ return weight; }
24
25
public void setFavFood(String newFavFood){ favFood = newFavFood; }
26
public String getFavFood(){ return favFood; }
27
28
public void setSpeed(double newSpeed){ speed = newSpeed; }
29
public double getSpeed(){ return speed; }
30
31
public void setSound(String newSound){ sound = newSound; }
32
public String getSound(){ return sound; }
33
34
// A private method can only be accessed by other public methods
35
// that are in the same class
36
37
private void bePrivate(){
38
System.out.println("I'm a private method");
39
}
40
41
public static void main(String[] args){
42
43
Animal dog = new Animal();
44
45
dog.setName("Grover");
46
47
System.out.println(dog.getName());
48
49
}
50
51
}

Dog.java

1
public class Dog extends Animal{
2
3
public void digHole(){
4
5
System.out.println("Dug a hole");
6
7
}
8
9
public void changeVar(int randNum){
10
11
randNum = 12;
12
13
System.out.println("randNum in method value: " + randNum);
14
15
}
16
17
18
/* This private method can only be accessed through using other
19
* methods in the class */
20
21
private void bePrivate(){
22
System.out.println("In a private method");
23
}
24
25
public void accessPrivate(){
26
bePrivate();
27
}
28
29
// The constructor initializes all objects
30
31
public Dog(){
32
33
// Executes the parents constructor
34
// Every class has a constructor whether you make it or not
35
36
super();
37
38
// Sets bark for all Dog objects by default
39
40
setSound("Bark");
41
42
}
43
44
}

Cat.java

1
public class Cat extends Animal{
2
3
// The constructor initializes all objects
4
5
public Cat(){
6
7
// Executes the parents constructor
8
// Every class has a constructor whether you make it or not
9
10
super();
11
12
// Sets bark for all Dog objects by default
13
14
setSound("Meow");
15
16
}
17
18
// If you want to make sure a method isn't overridden mark it as Final
19
20
final void attack(){
21
// Do stuff that can never change
22
}
23
24
// A field marked with final can't be changed
25
26
public static final double FAVNUMBER = 3.14;
27
28
// A class labeled as final can't be extended
29
30
}

WorkWithAnimals.java

1
public class WorkWithAnimals{
2
3
int justANum = 10;
4
5
public static void main(String[] args){
6
7
Dog fido = new Dog();
8
9
fido.setName("Fido");
10
System.out.println(fido.getName());
11
12
fido.digHole();
13
14
fido.setWeight(-1);
15
16
// Everything is pass by value
17
// The original is not effected by changes in methods
18
19
int randNum = 10;
20
fido.changeVar(randNum);
21
22
System.out.println("randNum after method call: " + randNum);
23
24
// Objects are passed by reference to the original object
25
// Changes in methods do effect the object
26
27
changeObjectName(fido);
28
29
System.out.println("Dog name after method call: " + fido.getName());
30
31
System.out.println("Animal Sound: " + fido.getSound());
32
33
// Create a Dog and Cat object with the super class
34
// but the Dog and Cat reference type
35
36
Animal doggy = new Dog();
37
Animal kitty = new Cat();
38
39
System.out.println("Doggy says: " + doggy.getSound());
40
System.out.println("Kitty says: " + kitty.getSound() + "\n");
41
42
// Now you can make arrays of Animals and everything just works
43
44
Animal[] animals = new Animal[4];
45
animals[0] = doggy;
46
animals[1] = kitty;
47
48
System.out.println("Doggy says: " +animals[0].getSound());
49
System.out.println("Kitty says: " +animals[1].getSound() + "\n");
50
51
// Sends Animal objects for processing in a method
52
53
speakAnimal(doggy);
54
55
// Polymorphism allows you to write methods that don't need to
56
// change if new subclasses are created.
57
58
// You can't reference methods, or fields that aren't in Animal
59
// if you do, you'll have to cast to the required object
60
61
((Dog) doggy).digHole();
62
63
// You can't use non-static variables or methods in a static function
64
65
// System.out.println(justANum);
66
67
// sayHello();
68
69
// You can't call a private method even if you define it in
70
// the subclass
71
72
// fido.bePrivate();
73
74
// You can execute a private method by using another public
75
// method in the class
76
77
fido.accessPrivate();
78
79
// Creating a Giraffe from an abstract class
80
81
Giraffe giraffe = new Giraffe();
82
83
giraffe.setName("Frank");
84
85
System.out.println(giraffe.getName());
86
87
}
88
89
// Any methods that are in a class and not tied to an object must
90
// be labeled static. Every object created by this class will
91
// share just one static method
92
93
public static void changeObjectName(Dog fido){
94
95
fido.setName("Marcus");
96
97
}
98
99
// Receives Animal objects and makes them speak
100
101
public static void speakAnimal(Animal randAnimal){
102
103
System.out.println("Animal says: " + randAnimal.getSound());
104
105
}
106
107
// This is a non-static method used to demonstrate that you can't
108
// call a non-static method inside a static method
109
110
public void sayHello(){
111
112
System.out.println("Hello");
113
114
}
115
116
}

Creature.java

1
//If you don't want the user to create objects from
2
//a class mark it as abstract.
3
//Subclasses can still extend it
4
5
abstract public class Creature{
6
7
// protected fields are like private fields except
8
// subclasses can inherit them
9
10
protected String name;
11
protected double height;
12
protected int weight;
13
protected String favFood;
14
protected double speed;
15
protected String sound;
16
17
// There are no abstract fields in Java, but
18
// there are abstract methods. Every method
19
// marked abstract must be overridden
20
// Not all methods must be abstract and you
21
// can also use static methods
22
23
public abstract void setName(String newName);
24
public abstract String getName();
25
26
public abstract void setHeight(double newheight);
27
public abstract double getHeight();
28
29
public abstract void setWeight(double newWeight);
30
public abstract double getWeight();
31
32
public abstract void setFavFood(String newFood);
33
public abstract String getFavFood();
34
35
public abstract void setSpeed(double newSpeed);
36
public abstract double getSpeed();
37
38
public abstract void setSound(String newSound);
39
public abstract String getSound();
40
41
}

Giraffe.java

1
public class Giraffe extends Creature{
2
3
private String name;
4
5
@Override
6
public void setName(String newName) {
7
name = newName;
8
9
}
10
11
@Override
12
public String getName() {
13
// TODO Auto-generated method stub
14
return name;
15
}
16
17
@Override
18
public void setWeight(double newWeight) {
19
// TODO Auto-generated method stub
20
21
}
22
23
@Override
24
public double getWeight() {
25
// TODO Auto-generated method stub
26
return 0;
27
}
28
}