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
1public 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 methods35 // that are in the same class36
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
1public 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 other19 * 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 objects30
31 public Dog(){32
33 // Executes the parents constructor34 // Every class has a constructor whether you make it or not35
36 super();37
38 // Sets bark for all Dog objects by default39
40 setSound("Bark");41
42 }43
44}
Cat.java
1public class Cat extends Animal{2
3 // The constructor initializes all objects4
5 public Cat(){6
7 // Executes the parents constructor8 // Every class has a constructor whether you make it or not9
10 super();11
12 // Sets bark for all Dog objects by default13
14 setSound("Meow");15
16 }17
18 // If you want to make sure a method isn't overridden mark it as Final19
20 final void attack(){21 // Do stuff that can never change22 }23
24 // A field marked with final can't be changed25
26 public static final double FAVNUMBER = 3.14;27
28 // A class labeled as final can't be extended29
30}
WorkWithAnimals.java
1public 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 value17 // The original is not effected by changes in methods18
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 object25 // Changes in methods do effect the object26
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 class34 // but the Dog and Cat reference type35
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 works43
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 method52
53 speakAnimal(doggy);54
55 // Polymorphism allows you to write methods that don't need to56 // change if new subclasses are created.57
58 // You can't reference methods, or fields that aren't in Animal59 // if you do, you'll have to cast to the required object60
61 ((Dog) doggy).digHole();62
63 // You can't use non-static variables or methods in a static function64
65 // System.out.println(justANum);66
67 // sayHello();68
69 // You can't call a private method even if you define it in70 // the subclass71
72 // fido.bePrivate();73
74 // You can execute a private method by using another public75 // method in the class76
77 fido.accessPrivate();78
79 // Creating a Giraffe from an abstract class80
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 must90 // be labeled static. Every object created by this class will91 // share just one static method92
93 public static void changeObjectName(Dog fido){94
95 fido.setName("Marcus");96
97 }98
99 // Receives Animal objects and makes them speak100
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't108 // call a non-static method inside a static method109
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 from2//a class mark it as abstract.3//Subclasses can still extend it4
5abstract public class Creature{6
7 // protected fields are like private fields except8 // subclasses can inherit them9
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, but18 // there are abstract methods. Every method19 // marked abstract must be overridden20 // Not all methods must be abstract and you21 // can also use static methods22
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
1public class Giraffe extends Creature{2
3 private String name;4
5 @Override6 public void setName(String newName) {7 name = newName;8
9 }10
11 @Override12 public String getName() {13 // TODO Auto-generated method stub14 return name;15 }16
17 @Override18 public void setWeight(double newWeight) {19 // TODO Auto-generated method stub20
21 }22
23 @Override24 public double getWeight() {25 // TODO Auto-generated method stub26 return 0;27 }28}