You are on page 1of 2

10/13/2020

Java - What is OOP?

• OOP stands for Object-Oriented Programming.


• Procedural programming is about writing procedures or methods
that perform operations on the data, while object-oriented
Introduction to OOP programming is about creating objects that contain both data and
methods.
Chapter 1

Classes and objects are the two main


aspects of object-oriented
programming.

Object-oriented programming has several advantages over


procedural programming: Java - What Example:

• OOP is faster and easier to execute are Classes


• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY "Don't Repeat Yourself", and
and Objects? Class – fruit
Objects – Apple, Banana, Mango
makes the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with
less code and shorter development time Class – Car
Objects – Honda, Toyota, Mitsubishi

Java Classes and Objects Create a Class

To create a class, use the keyword class:


Java Classes/Objects MyClass.java
• Java is an object-oriented programming language. Create a class named "MyClass" with a variable x:
• Everything in Java is associated with classes and objects, along
with its attributes and methods. For example: in real life, a car is public class MyClass {
a class. The car has attributes, such as weight and color, int x = 5;
and methods, such as drive and brake. }
• A Class is like an object constructor, or a "blueprint" for creating
objects. Remember that a class should always start with an uppercase first letter,
and that the name of the java file should match the class name.

1
10/13/2020

Create an Object

In Java, an object is created from a class. We have already created Create an object called "myObj" and print the value of x:
the class named MyClass, so now we can use this to create objects.

To create an object of MyClass, specify the class name, followed by


the object name, and use the keyword new:

Multiple Objects Using Multiple Classes

You can create multiple objects of one class: You can also create an object of a class and access it in another
Example class. This is often used for better organization of classes (one class
has all the attributes and methods, while the other class holds the
Create two objects of MyClass: main() method (code to be executed)).

Remember that the name of the java file should match the class
name. In this example, we have created two files in the same
directory/folder:

MyClass.java OtherClass.java

You might also like