0% found this document useful (0 votes)
11 views

OOPs Concepts in Java

Chapter 5 discusses Object-Oriented Programming (OOP) concepts in Java, including key principles such as classes, objects, inheritance, polymorphism, abstraction, encapsulation, association, aggregation, and composition. It highlights the advantages of OOP, such as modularity and code reusability, and compares OOP with unstructured and structured programming paradigms. The chapter also provides examples to illustrate the definitions and differences between classes and objects in Java.

Uploaded by

westgodson66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OOPs Concepts in Java

Chapter 5 discusses Object-Oriented Programming (OOP) concepts in Java, including key principles such as classes, objects, inheritance, polymorphism, abstraction, encapsulation, association, aggregation, and composition. It highlights the advantages of OOP, such as modularity and code reusability, and compares OOP with unstructured and structured programming paradigms. The chapter also provides examples to illustrate the definitions and differences between classes and objects in Java.

Uploaded by

westgodson66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter 5

OOPs Concepts in Java

What is OOPS?
Object-Oriented Programming System (OOPs) is a programming
concept that works on the principles of abstraction, encapsulation,
inheritance, and polymorphism. It allows users to create objects they want
and create methods to handle those objects. The basic concept of OOPs is to
create objects, re-use them throughout the program, and manipulate these
objects to get results.
OOP meaning “Object Oriented Programming” is a popularly known and
widely used concept in modern programming languages like Java.

OOPs Concepts in Java with Examples


The following are general OOPs concepts in Java:

1) Class
The class is one of the Basic concepts of OOPs which is a group of similar
entities. It is only a logical component and not the physical entity. Lets
understand this one of the OOPs Concepts with example, if you had a class
called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota,
etc. Its properties(data) can be price or speed of these cars. While the
methods may be performed with these cars are driving, reverse, braking etc.

2) Object
An object can be defined as an instance of a class, and there can be multiple
instances of a class in a program. An Object is one of the Java OOPs concepts
which contains both the data and the function, which operates on the data.
For example – chair, bike, marker, pen, table, car, etc.

3) Inheritance
Inheritance is one of the Basic Concepts of OOPs in which one object
acquires the properties and behaviors of the parent object. It’s creating a
parent-child relationship between two classes. It offers robust and natural
mechanism for organizing and structure of any software.
4) Polymorphism
Polymorphism refers to one of the OOPs concepts in Java which is the ability
of a variable, object or function to take on multiple forms. For example, in
English, the verb run has a different meaning if you use it with a laptop, a
foot race, and business. Here, we understand the meaning of run based on
the other words used along with it. The same also applied to Polymorphism.

5) Abstraction
Abstraction is one of the OOP Concepts in Java which is an act of
representing essential features without including background details. It is a
technique of creating a new data type that is suited for a specific application.
Lets understand this one of the OOPs Concepts with example, while driving a
car, you do not have to be concerned with its internal working. Here you just
need to concern about parts like steering wheel, Gears, accelerator, etc.

6) Encapsulation
Encapsulation is one of the best Java OOPs concepts of wrapping the data
and code. In this OOPs concept, the variables of a class are always hidden
from other classes. It can only be accessed using the methods of their
current class. For example – in school, a student cannot exist without a class.

7) Association
Association is a relationship between two objects. It is one of the OOP
Concepts in Java which defines the diversity between objects. In this OOP
concept, all objects have their separate lifecycle, and there is no owner. For
example, many students can associate with one teacher while one student
can also associate with multiple teachers.

8) Aggregation
In this technique, all objects have their separate lifecycle. However, there is
ownership such that child object can’t belong to another parent object. For
example consider class/objects department and teacher. Here, a single
teacher can’t belong to multiple departments, but even if we delete the
department, the teacher object will never be destroyed.

9) Composition
Composition is a specialized form of Aggregation. It is also called “death”
relationship. Child objects do not have their lifecycle so when parent object
deletes all child object will also delete automatically. For that, let’s take an
example of House and rooms. Any house can have several rooms. One room
can’t become part of two different houses. So, if you delete the house room
will also be deleted.

Advantages of OOPs (Object-Oriented Programming


System):

1. OOPs Concepts in Java offer easy to understand and a clear modular


structure for programs.

2. Objects created for Object-Oriented Programs can be reused in other


programs. Thus it saves significant development cost.

3. Large programs are difficult to write, but if the development and


designing team follow OOPS concepts, then they can better design with
minimum flaws.

4. It enhances program modularity because every object exists


independently.
Comparison of OOPS with other programming styles with
help of an Example
Let’s understand with example how Java OOPs Concepts are different than other programming
approaches.
Programming languages can be classified into 3 primary types
1. Unstructured Programming Languages: The most primitive of all
programming languages having sequentially flow of control. Code is
repeated through out the program
2. Structured Programming Languages: Has non-sequentially flow of
control. Use of functions allows for re-use of code.
3. Object Oriented Programming Languages: Combines Data &
Action Together.
Let’s understand these 3 types with an example.
Suppose you want to create a Banking Software with functions like
1. Deposit
2. Withdraw
3. Show Balance
Unstructured Programming Languages
The earliest of all programming language were unstructured programming language. A very
elementary code of banking application in unstructured Programming language will have two
variables of one account number and another for account balance
int account_number=20;
int account_balance=100;

Suppose deposit of 100 dollars is made.


account_balance = account_balance+100

Next you need to display account balance.


printf(“Account Number = %d, account_number)
printf(“Account Balance = %d, account_balance)

Now the amount of 50 dollars is withdrawn.


account_balance=account_balance-50

Again, you need to display the account balance.


printf(“Account Number=%d,account_number)
printf(“Account Balance=%d,account_balance)

For any further deposit or withdrawal operation – you will code repeating the
same lines again and again.
Structured Programming
With the arrival of Structured programming repeated lines on the code were
put into structures such as functions or methods. Whenever needed, a
simple call to the function is made.
Object-Oriented Programming
In our program, we are dealing with data or performing specific operations
on the data. In fact, having data and performing certain operation on that
data is very basic characteristic in any software program. Experts in
Software Programming thought of combining the Data and Operations.
Therefore, the birth of Object Oriented Programming which is commonly
called OOPS. The same code in Object Oriented Programming languages will
have same data and some action performed on that data.
Class Account
{
int account_number;
int account_balance;
public void showdata()
{
System.out.println(“Account Number”+ account_number)
System.out.println(“Account Balance”+ account_balance)
}
}
By combining data and action, we will get many advantages over structural
programming viz,
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism
They are discussed in greater details in as we progress

Class and Object in Java

What is Classes and Objects in Java?


Classes and Objects in Java are the fundamental components of OOP’s. Often
there is a confusion between classes and objects. In this tutorial, we try to
tell you the difference between Class and Object in Java. First, let’s
understand what they are.

What is Class in Java?


Class are a blueprint or a set of instructions to build a specific type of object.
It is a basic concept of Object-Oriented Programming which revolve around
the real-life entities. Class in Java determines how an object will behave and
what the object will contain.

Syntax of Class in Java


class <class_name>
{
field;
method;
}
What is an Object in Java?
Object is an instance of a class. An object in OOPS is nothing but a self-
contained component which consists of methods and properties to make a
particular type of data useful. For example color name, table, bag, barking.
When you send a message to an object, you are asking the object to invoke
or execute one of its methods as defined in the class. From a programming
point of view, an object in OOPS can include a data structure, a variable, or a
function. It has a memory location allocated. Java Objects are designed as
class hierarchies.

Object Syntax in Java


ClassName ReferenceVariable = new ClassName();

What is the Difference Between Object and Class in Java?


A Class in object oriented programming is a blueprint or prototype that defines the variables and
the methods (functions) common to all Java Objects of a certain kind.
An object in OOPS is a specimen of a class. Software objects are often used to model real-world
objects you find in everyday life.

Classes and Objects with an example.


Let’s take an example of developing a pet management system, specially meant for dogs. You
will need various information about the dogs like different breeds of the dogs, the age, size, etc.

You need to model real-life beings, i.e., dogs into software entities.

Moreover, the million dollar question is, how you design such software?
Here is the solution- First, let’s do an exercise. You can see the picture of
three different breeds of dogs below.

Can you identify the difference? List down the differences between them.

Some of the differences you might have listed out maybe breed, age, size,
color, etc. If you think for a minute, these differences are also some common
characteristics shared by these dogs. These characteristics (breed, age, size,
color) can form a data members for your object.
Next, list out the common behaviors of these dogs like sleep, sit, eat, etc. So
these will be the actions of our software objects.

So far we have defined following things,


 Class – Dogs
 Data members or objects– size, age, color, breed, etc.
 Methods– eat, sleep, sit and run.
Now, for different values of data members (breed size, age, and color) in Java
class, you will get different dog objects.
You can design any program using this OOPs approach. While creating a
class, one must follow the following principles.

1. Single Responsibility Principle (SRP)- A class should have only one


reason to change

2. Open Closed Responsibility (OCP)- It should be able to extend any


classes without modifying it

3. Liskov Substitution Responsibility (LSR)- Derived classes must be


substitutable for their base classes

4. Dependency Inversion Principle (DIP)- Depend on abstraction and not


on concretions

5. Interface Segregation Principle (ISP)- Prepare fine grained interfaces


that are client specific.
Classes and Objects in Java Example Programs

// Class Declaration
public class Dog
{
// Instance Variables
String breed;
String size;
int age;
String color;

// method 1
public String getInfo()
{
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is:
"+color);
}
public static void main(String[] args)
{
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo());
}
}

This code is editable. Click Run to Compile and Execute


Output:
Breed is: Maltese Size is: Small Age is:2 color is: white

Java Object and Class Example: main outside class


In previous program, we are creating main() method inside the class. Now,
we create classes and define main() method in another class. This is a better
way than previous one.

// Class Declaration
class Dog
{
// Instance Variables
String breed;
String size;
int age;
String color;

// method 1
public String getInfo()
{
return ("Breed is: "+breed+" Size is:"+size+" Age is:"+age+" color is:
"+color);
}
}
public class Execute
{
public static void main(String[] args)
{
Dog maltese = new Dog();
maltese.breed="Maltese";
maltese.size="Small";
maltese.age=2;
maltese.color="white";
System.out.println(maltese.getInfo());
}
}

Output:
Breed is: Maltese Size is: Small Age is: 2 color is: white

Summary
1. Java Class is an entity that determines how Java Objects will behave and
what objects will contain
2. A Java object is a self-contained component which consists of methods
and properties to make certain type of data useful
3. A class system allows the program to define a new class (derived class) in
terms of an existing class (superclass) by using a technique
like inheritance, overriding and augmenting.

You might also like