4 pillars of OOP

Watch this video as support 👇

https://youtu.be/GpjpqhthnnU

Java has 4 pillars it is built upon 👇

4 pillars of OOP

Lucky for you we have already talked about 2 out of the 4 pillars. That is:

  • Abstraction - Abstraction is about abstracting code away. This can be achieved by creating methods, creating an Interface or an Abstract class.

  • Inheritance - The inheritance pillar is obvioulsy about inheritance and how objects can inherit properties from another object

The 2 pillars we have not talked about are

  • Encapsulation - How we can control what objects can see and interact with data inside another object.

  • Polymorphism - How methods and classes can act in different ways. It means one name many forms.

Let's first talk about Encapsulation!

Encapsulation

Let's again use the User class from earlier!

You have learned about most of creating a class but we have not talked about the public keyword.

public is a access modifier that tells Java who have access to the attribute. In this example username, password and UserInheritance (the constructor) and generateNewPassword are public. That means that all those attributes and methods can be called on the object.

Accessing the attributes username and password is not good because all the data that is inside the mieParker object can be accessed by everone that has the object.

Let's improve this using encapsulation

Now we have used the access modifier private instead. This means that username and password now only can be accessed within the class!

So what if we still want to access or change username or password from outside the class using an object. Then we use getter's and setter's

Getters and setters

Getter method

Getter methods are methods that give access to the some private attribute.

It's simply a method that returns the private attribute.

Setter method

Setter methods are methods that give access to set/change some private attribute.

It is a method that changes the private attribute.

Let's use it

Encapsulation best practice

How should you then use this moving forward?

Here are some best practices 👇

  • Always set all attributes as private

  • Make getter and setter methods for the relevant attributes. Consider each one!

  • Think about access modifiers for methods. If a method should only be used within the class, make it private. Otherwise make it public

Another example: A User that has a cpr attribute should not be changed

Final user version

Polymorphism

Gå igennem en liste af objekter der overholder et interface. Kalde deres metode. Metoderne opfører sig forskelligt selv om de hedder det samme

Polymorphism means that entities behave differently based on the context. In Java it means a bit more specifically that we can create the same task in different ways!

Let's take an example:

We create a Geometry interface that has a method called getArea. All geometries have areas but how they implement that area depends on the geometry

Let's create a Rectangle and a Circle class that implements Geometry

Now we create two objects:

The loop first prints out 314.1592 then 100.0

Exercises

Igennem alle disse opgaver, husk at lave alle attributter private og kun at lave getter og setters for dem i har brug for!

Exercise 4

This exercise is part of the homework to next week!

https://github.com/behu-kea/first-semester-java/blob/9d5b9fc185978dad0bfdbc0b83fdc7937326db4d/assets/Bookingsystem%20(DK).pdf

Last updated

Was this helpful?