Introduction To Java Object Oriented Programming – In this article we are going to talk about Java Object Oriented Programming.
So Object-oriented programming (OOP) is one of the bigger programming buzzwords of recent
years, and you can spend years learning all about object-oriented programming methodologies
and how they can make your life easier than The Old Way of programming. It all comes down
to organizing your programs in ways that echo how things are put together in the real world.
What does this have to do with programming?
Everything. Object-oriented programming
works in exactly this same way. Using object-oriented programming, your overall program is
made up of lots of different self-contained components (objects), each of which has a specific
role in the program and all of which can talk to each other in predefined ways.
Objects and Classes
Object-oriented programming is modeled on how, in the real world, objects are often made up
of many kinds of smaller objects. This capability of combining objects, however, is only one very
general aspect of object-oriented programming. Object-oriented programming provides several
other concepts and features to make creating and using objects easier and more flexible, and the
most important of these features is that of classes.
So A class is a template for multiple objects with similar features. Classes embody all the
features of a particular set of objects.
When you write a program in an object-oriented language, you don’t define actual objects. You
define classes of objects.
Behavior and Attributes
Every class you write in Java is generally made up of two components: attributes and behavior.
In this section, you’ll learn about each one as it applies to a theoretical class called Motorcycle.
To finish up this section, you’ll create the Java code to implement a representation of a
motorcycle.
Attributes
Attributes are the individual things that differentiate one object from another and determine th
appearance, state, or other qualities of that object. Let’s create a theoretical class called
Motorcycle. The attributes of a motorcycle might include the following:
- Color: red, green, silver, brown
- Style: cruiser, sport bike, standard
- Make: Honda, BMW, Bultaco
Attributes of an object can also include information about its state; for example, you could have
features for engine condition (off or on) or current gear selected.
Attributes are defined by variables; in fact, you can consider them analogous to global variables
for the entire object. Because each instance of a class can have different values for its variables,
each variable is instance variable.
Instance Variable
Instance variables define the attributes of an object. The class defines the kind of attribute,
and each instance stores its own value for that attribute.
Behavior
A class’s behavior determines what instances of that class do when their internal state changes
or when that instance is asked to do something by another class or object. Behavior is the way
objects can do anything to themselves or have anything done to them. For example, to go back
to the theoretical Motorcycle class, here are some behaviors that the Motorcycle class might have:
- Start the engine
- Stop the engine
- Speed up
- Change gear
- Stall
How To Create Class In Java ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class Motorcycle { String make; String color; boolean engineState; void startEngine() { if(engineState == true) { System.out.println("The engine is already on."); } else { engineState = true; System.out.println("The engine is now on."); } } } |
So in the above code we have created a Motorcycle class. this class has attributes and behaviors, let me complete the code and run the code for the class.
Also i have add a new method in this class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class Motorcycle { String make; String color; boolean engineState; void startEngine() { if(engineState == true) { System.out.println("The engine is already on."); } else { engineState = true; System.out.println("The engine is now on."); } } void showAtts() { System.out.println("This motorcycle is a " + color + "" + make); if (engineState == true) System.out.println("The engine is on."); else System.out.println("The engine is off."); } public static void main(String[] args) { Motorcycle m = new Motorcycle(); m.make = "Yamaha"; m.color = "Yellow"; m.showAtts(); m.startEngine(); } } |
Run the code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email