Everything is an Object (Java)
Translated from German using DeepL.
Contains some German code sections and pictures.
Date: November 2020
Reading time: 4 minutes
After reading this blog post, you will understand the basics of object-oriented programming.
OOP
There are different ways of programming. The functional approach is one of them. You write as much code as possible in functions. This has advantages. One disadvantage, however, is that the code can become extremely complex and confusing.
Object-oriented programming (Object-oriented programming) solves this problem.
Basics
In OOP, the aim is to implement the conditions as realistically as possible.
Class
The focus is on classes. A class is comparable to a blueprint. An object can then be instantiated from it.
Object
An object represents an object or concept. It contains the properties defined in the class.
Attributes and methods
In Java, we speak of methods (functions) and attributes (variables).
Attribute
These are the properties of an object. An attribute can therefore be used to describe an object. Example: The object Person has an attribute ‘name’.
Method
Methods can be used to perform operations. We will describe in more detail later what they are used for.
UML
The Unified Modeling Language is used to visualize a system. It is divided into two parts.
Structure diagrams
- Class diagram
- Object diagram
- Component diagram
- Development diagram
- Package diagram
- Profile diagram
- Composite structure diagram
Behavior diagrams
- Sequence diagram
- Use case diagram
- Activity diagram
- State machine diagram
- Sequence diagram
- Communication diagram
- Interaction overview diagram
- Timing diagram
This blog post uses the class and object diagram.
Example
I have received an order to write a program. I receive a UML class diagram for this. This shows me the name, the attributes and the methods of the class. This is what the diagram looks like:
In addition to attributes and methods, class diagrams can also contain the following:
- Visibility
- Type
- Multiplicity
- Default value
- Property value
I will not go into this any further, as the focus should be on OOP. However, this section (opens in a new tab) of the Wikipedia article is recommended.
Several objects can now be created from this blueprint. All of them have the attributes name
and age
. These are the corresponding object diagrams:
Class Programming
public class Person {
private String name;
private int alter;
}
Object Instantiation
public class MainProgramm {
public static void main(String[] args) {
Person kwild;
kwild = new Person();
}
}
Advantages
Inheritance
I need two types of people in my program:
- seller
- buyer
This is what my class diagrams look like:
The seller and the buyer have different tasks (methods). However, the methods also overlap. Both people eat, drink and sleep.
There are also similarities in the attributes. Both people have a name, first name and birthday. Redundancies can therefore be recognized here. These can be removed by inheritance.
A person is created. This person has all the attributes and methods that a human possesses. Now all people can inherit from it: buyer, seller, warehouse clerk, footballer, dentist, ...
Data encapsulation
In OOP, the principle of secrecy comes into play. Everything in a class is programmed in such a way that the data is protected from external access.
private int kontostand = 1200;
public int getKontostand() {
return kontostand;
}
The kontostand
attribute was protected from the outside with private. It can only be accessed via the getter method, as this has been set to public.
Abstraction
Outside of a class, only what a method does is important. How it does this is not important and remains hidden. This offers the following advantages:
- The program is easier to use
- The program can be changed more easily
- Only important information is disclosed
Real example
If I press "Play" on a TV remote control, the movie is played. This is important to me. What happens when I tap the button inside the remote control is not obvious to me.
Code example
The Customer object has a getMail()
method. This requires a name and returns an e-mail address. I do not need to know how the method can do this in order to use it.
System.out.println(Kunde.getMail("john"));
Polymorphism
"In the field of genetics, polymorphism (Greek πολυμορφισμός polymorphismos "multiformity") refers to the occurrence of several gene variants within a population." - Wikipedia
But what does this have to do with programming?
In the case of inheritance, we have seen that classes can inherit from each other. The human inherits its attributes and methods from the buyer. However, the buyer can also kaufen()
and korbFassen()
. So I can add these methods to him.
I use extends
to show which class the buyer should inherit from. It receives all attributes and methods of this class. Nevertheless, more can be added to this class.
public class Kaeufer extends Mensch {
public void kaufen() {}
public void korbFassen() {}
}
I can now instantiate a buyer with the name Karl in the main program. He can eat and buy. He inherits the food from the person, he has a method for buying himself.
public class Programm {
public static void main(String[] args) {
Kaeufer karl = new Kaeufer();
karl.essen();
karl.kaufen();
}
}
Overwriting methods
The seller is also inherited. As it is always very hungry, essen()
must be adapted.
Inherited methods can be overwritten with @Override
. This is not overwritten on the human class and therefore only applies to the seller.
public class Verkaeufer extends Mensch {
public void verkaufen() {}
public void rueckgeldGeben() {}
@Override
public void essen(){
System.out.println("Ich esse viel, da ich den ganzen Tag an der Kasse sitze.");
}
}
Conclusion
If a program has to represent reality, you should program object-oriented. This improves the:
- maintainability
- reusability
- security of methods and attributes
I find OOP exciting and have learned a lot while writing this post.