- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Objects
The previous module discussed the basics of programming, but it didn't go into details of how code is structured. In the early days of programming, people wrote programs which were executed linearly from top-to-bottom, and the most structure they had was separation of code into different tasks.
You saw simple examples of this in the algorithm flowcharts, and in the code you've programmed. This type of linear programming can work well for small pieces of code, but it becomes more difficult when projects grow in size. Complex applications cannot be modeled well with long linear code, so a more-structured approach to programming is needed.
Objects
Java enforces a programming methodology known as Object-Oriented Programming (OOP). OOP is inspired by the real world, where people interact with different objects to do different tasks. An Object has behaviors that it does, and it has a current state that its in. For example, a car might have the following behaviors:
- accelerate
- brake
- brighten Lights
while its current state might be:
- speed = 60mph
- lights = high beams
Similarly, Objects in software have methods for their behaviors and variables for their state. If you were creating a racing game, you would probably create an Object Car that had methods like accelerate()
and brake()
and had variables like speed
and lightBrightness
. By placing the state and behaviors into Objects, one can organize code effectively.
Encapsulation
Let's say you're driving a car and decide you want to go faster (or slower). To change the speed, you probably won't stick your hand into the engine to change its combustion rate (even if this is a more direct way to change the speed). Instead you use the car's provided "methods" to change the speed, such as the the accelerator and brake. You don't even need to worry about how the engine works, and can probably switch to a diesel or electric engine and continue to drive as you did before.
When programming, you will code various Objects and have them interact with each other. One Object should not access the variables and internal methods of another object, but instead it should use the public methods that the Object provides.
OOP organizes the code into objects which interact with each other through public methods
Separating code into different objects that are not tangled together makes it easier to:
- re-use objects within one or many projects.
- fix, update or replace one object without messing up the whole code.
- work on one part of a project while another developer works on another part.
- use objects more easily and flexibly...
Challenge
Which of the following seem like good variables for a Bicycle Object?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
calo
Jan 14, 4:31 PMI think it would be good if pictures with schemes could be zoomed
Learneroo
Sep 24, 1:18 PMOK you can click on the pictures for larger ones. Code can be disorganized without objects, objects let code be organized into components that communicate with each other.