This guide introduces object-oriented programming (OOP) in Java. OOP is a useful foundation before writing larger programs, including 2D games made with C# or Java. It covers the main concepts and building blocks used to get started.
What is object-oriented programming?
OOP models parts of the world as objects with attributes and behaviors. This can help organize a problem into manageable pieces. Procedural programming groups instructions into procedures or modules. That approach can become harder to manage as a program grows, while OOP organizes data and behavior around objects and can make code easier to reuse.
From machine code to OOP
Programming has evolved from working directly with machines, to assembly language that controls hardware, to procedural programming, and then to object-oriented approaches such as the one described here.
Classes and objects
A class is a blueprint used to create objects. An object is an instance created from that class. For example, an Employee class might define attributes such as name and salary, along with a behavior such as calculateSalary. You can create multiple employee objects, each with its own values.
Attributes, methods, and constructors
After creating an object, you assign values to its attributes. Class methods are functions that let an object perform actions or provide access to its data. Java’s this keyword refers to the current object inside a class.
- Setter: a method used to assign a value to an attribute.
- Getter: a method used to retrieve an attribute’s value.
- Constructor: a special method called when an object is created; it can set initial values.
A default constructor takes no parameters. If a class has no constructor defined, Java provides a default constructor. You can also write your own constructor to initialize an object.
Core OOP concepts
- Classes and objects: a class is the blueprint; an object is an instance.
- Attributes and behaviors: objects hold data and perform actions.
- Access modifiers:
public,private, andprotectedcontrol access to class members. - Encapsulation: keeps implementation details inside a class and exposes only what is needed.
- Inheritance: lets a child class reuse attributes and behaviors from a parent class.
- Polymorphism: lets objects behave differently in different contexts.
Examples of classes
A Pokemon class could define a name, type, and attack power, with behaviors such as attacking or using a move. A Smartphone class could define a model, screen size, and operating system, with behaviors such as making calls or taking pictures.
Overloading and overriding
Overloading means a class has multiple methods with the same name but different parameters. Overriding means a child class replaces a parent class method with one that has the same name and parameters but different behavior.
Summary
Understanding classes, objects, encapsulation, inheritance, and polymorphism helps you design programs in Java and other OOP languages. These concepts provide a foundation for building and maintaining larger applications.
💬 Looking for a free financial consultation? Message us on LINE.
