10- Object Oriented Programming Java(Lecture-10) ~500 11
10- Object Oriented Programming Java(Lecture-10) ~500 11
3
• Procedural programming is about writing procedures or methods that
perform operations on the data, while object-oriented programming is
about creating objects that contain both data and methods.
4
OOP advantages over procedural programming
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY "Don't Repeat Yourself", and 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
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the
repetition of code. You should extract out the codes that are common for
the application, and place them at a single place and reuse them instead of
repeating it.
5
What are Classes and Objects?
• Classes and objects are the two main aspects of object-oriented
programming.
• A class is a template for objects, and an object is an instance of a class
6
7
Class
• A class is a blueprint or template of an object.
• It is a user-defined data type.
• Inside a class, we define variables, constants, member functions, and other
functionality.
• it binds data and functions together in a single unit.
• It does not consume memory at run time.
• Note that classes are not considered as a data structure.
• It is a logical entity.
• It is the best example of data binding.
• Note that a class can exist without an object but vice-versa is not possible.
8
Object
• An object is a real-world entity that has attributes, behavior, and
properties.
9
10
• Everything in Java is associated with classes and objects, along with its
attributes and methods.
• For example: in real life, a car is an object.
• The car has attributes, such as weight and color, and methods, such as
drive and brake.
11
Create a Class
12
Create an Object
• To create an object of Main, specify the class name, followed by the
object name, and use the keyword new:
13
Multiple Objects
• You can create multiple objects of one class:
14
Using Multiple Classes
• You can also create an object of a class and access it in another class.
• This is often used for better organization of classes (one class has all the
attributes and methods, while the other class holds the 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:
• Main.java
• Second.java
15
Using Multiple Classes
16
Java Class Attributes
• Class attributes are basically variables within a class.
• Another term for class attributes is fields.
17
Accessing Attributes
• You can access attributes by creating an object of the class, and by using
the dot syntax (.):
18
Modify Attributes
• You can also modify attribute values:
19
• Or override existing values:
20
Final Keyword
• Final keyword will not allow to override existing values.
• The final keyword is useful when you want a variable to always store the
same value, like PI (3.14159...).
21
Multiple Objects
• If you create multiple objects of one class, you can change the attribute
values in one object, without affecting the attribute values in the other:
22
Multiple Attributes
• You can specify as many attributes as you want:
23
Java Class Methods
• Methods are declared within a class
• They are used to perform certain actions
24
Static vs. Public
• You will often see Java programs that have either static or public
attributes and methods.
25
26
Access Methods With an Object
27
Java Constructors
• A constructor in Java is a special method that is used to initialize objects.
28
29
Access Modifiers
1) public The code is accessible for all classes
30
Thank You…!