Java: Object- Oriented Programming
What is Object-Oriented Programming?
Object-oriented programming (OOP) is a way of thinking when you begin to
write in Java language. As you start (or already have experienced) writing
in Java, you will find yourself saying oops more than you would like to,
which can lead to frustration. Do not worry; I will explain this concept
using real-world examples to get you to more ah-HA statements from a newbie's perspective!
Object
Why real-world examples? Because they are
the concrete foundation that OOP builds upon when creating classes from
objects!
An object is
anything you can see in front of you (physical) or what you can think of
(logical), such as your car. Your car's state and behavior is what makes it
an object. With objects, what matters here is an input and an
output.
What states does your car have? If you're
like me, maybe you occasionally forget where you parked after rushing into a
store. What are the physical characteristics you look for? More than likely,
you begin to look to see if it is a SUV, sedan, or sports car. Your brain
automatically begins removing color, size, and brand as well!
What behavior does your car have now? You
cannot find your car, so you pull your keys out, click the unlock button,
and click the alarm. Someone next to you is doing the same thing, and you
hear multiple alarms blaring. Now, your car (object) has an address and
begins pulling at your memory. You begin to notice how high or low the break
and clearance lights are on vehicles flashing to know if it is your car or
not.
Class
Your car model is a part of the
standard components for building a vehicle.
It has a steering wheel, tires, headlights, brakes, and a gas pedal. Well,
not everyone, but here we speak of when the car was initially built (many
dysfunctional functioning cars are still rolling). In this class, your vehicle
still has a state and a behavior when driving.
Inheritance
All vehicles are grouped together: a sports car, a pickup truck, or a hooptie. The different
types of vehicles are subclasses that can use the same defining states and
behaviors of vehicles.
Polymorphism
This is how inheritance subclasses connect to the superclass. Think of this as how many food items can be made with flour, sugar, or your most beloved lemon pepper seasoning! Polymorphism is the Frank's® RedHot® commercial.
Interface
Interface for our example of a vehicle can be seen as how you
interact with your car. The key fob can
lock, unlock, and start the vehicle. Once started, the windows can roll up
and down, the radio can be changed, and the temperature can be changed. You
also will manually shift the car into a new state, like from park to drive
or reverse. The interface is how you have the Midas touch interacting with
the complex inner workings of other objects.
Abstraction
This is how the interface works with the complex inner workings. You may not know why you should not push the red button or what it is connected to. It is through pushing the red button you realize what it does.
Package
In our example, a package (library) would be everything the standard vehicle build comes with. A package like
the stereo system and Bluetooth connectivity can change for hands-free
driving. For these changes to take effect in Java, each package must be
written as an interface in different classes set as public. Public is what
is known as a global variable that every class can have access to. Public is
what the name implies.
Encapsulation
This is, in fact, the entire class. Encapsulation holds everything together within the vehicle class, like the vehicle types, the different package upgrades you can get, the sounds, and how you can interact with the car and the responses.
Oracle: Java
Java can be installed on many operating systems like Microsoft Windows, Mac, and Linux by clicking here. The link also gives a short example of successfully downloading the application and running its "Hello World!" program. You will need an IDE (idle) to compose the code, and an easy,
straightforward IDE to use would be Notepad++. You may already have it installed! If not, please be sure to read the
page closely so you don't click the wrong link and to make sure you set it
up with a
%PATH%. Eventually, I downloaded Eclipse IDE, which was even better than the Microsoft Visual Studio application.
If you need more assistance with installation and coding, Mosh on YouTube helped me in two and a half hours. You can find his informative video here!
My Trial and Error Fixes
1. I did not attempt to change the folder Java downloaded to. I have a
D:/ drive, which can complicate the %PATH%. Leave it in the recommended
area. To update your Java system path, click here.
2. Download IDE first, then JDK 8 (don't forget this!).
3. To search a path (folder within a folder), you must use 'cd' to
switch folders. 'cd' is similar to your left (back out folder) and right
(into the following folder) arrow. When you reach the final folder, type
'dir' to see the file you just saved.
4. If you need to update your file in your IDE, make sure you delete the
old App.class file before you type 'dir' in the cmd box. This will
ensure that the update is changed without repeatedly opening and closing the window when using 'javac' and 'java -cp'.
Comments
Post a Comment