This week, we worked on a few Java projects to better understand OOP concepts. The four pillars of object-oriented programming (OOP) are abstraction, encapsulation, inheritance, and polymorphism, which help to keep code modular, reusable, secure, and maintainable, among other benefits.
- Abstraction describes the process of hiding complex implementation details and exposing only the necessary features of an object. For example, we designed a
Shapeinterface that exposes an abstractgetAreafunction to be implemented by concrete classes. - Encapsulation allows an object to control and restrict access to its own internal state. For example, we developed a simple
Hangmangame, which used private fields to keep track of the state of the game, like thesecretWordthat the user tries to guess. - Inheritance is the process whereby a subclass can acquire properties and methods from a superclass. For example, we worked on a Pokémon-style game, where different types of subclasses inherit properties from a
Monstersuperclass. - Polymorphism means “many forms” and describes how a common interface can behave differently depending on the object to which it is being applied. For example, the different
Monstersubclasses have common methods that behave differently, depending on which kind ofMonsteris being used.
We also practiced some software development techniques this week, like test-driven development (TDD) and version control with Git. We used TDD to implement our Java projects according to tests and specifications included with the assignment, in addition to writing our own tests with JUnit. To keep track of changes we made to our repositories, we used the GitHub flow, which involves working on a new feature branch created from the main branch, opening a pull request to merge changes, and then merging the new changes back into the main branch.