You are on page 1of 7

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.

List of 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.

Abstraction is the concept of object-oriented programming that “shows” only


essential attributes and “hides” unnecessary information. The main purpose of
abstraction is hiding the unnecessary details from the users. Abstraction is
selecting data from a larger pool to show only relevant details of the object to
the user. It helps in reducing programming complexity and efforts

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.

Abstraction Encapsulation
Abstraction in Object Oriented
Encapsulation solves it implementation
Programming solves the issues at the
level.
design level.

Abstraction in Programming is about


Encapsulation means binding the code and
hiding unwanted details while showing
data into a single unit.
most essential information.

Data Abstraction in Java allows Encapsulation means hiding the internal


focussing on what the information details or mechanics of how an object does
object must contain something for security reasons.
Difference between Abstract Class and Interface
Abstract Class Interface

An abstract class can have both abstract and The interface can have only
non-abstract methods. abstract methods.
It does not support multiple inheritances. It supports multiple inheritances.
It can not provide the
It can provide the implementation of the interface. implementation of the abstract
class.
An abstract class can have protected and An interface can have only have
abstract public methods. public abstract methods.

An abstract class can have final, static, or static The interface can only have a
final variable with any access specifier. public static final variable.

What is Abstract Class?


Abstract Class is a type of class in OOPs, that declare one or more abstract
methods. These classes can have abstract methods as well as concrete
methods. A normal class cannot have abstract methods. An abstract class is a
class that contains at least one abstract method.

What are Abstract Methods?


Abstract Method is a method that has just the method definition but does not
contain implementation. A method without a body is known as an Abstract
Method. It must be declared in an abstract class. The abstract method will
never be final because the abstract class must implement all the abstract
methods.

Advantages of Abstraction
 The main benefit of using an Abstraction in Programming is that it
allows you to group several related classes as siblings.
 Abstraction in Object Oriented Programming helps to reduce the
complexity of the design and implementation process of software.
When to use Abstract Methods & Abstract Class?
Abstract methods are mostly declared where two or more subclasses are also
doing the same thing in different ways through different implementations. It
also extends the same Abstract class and offers different implementations of
the abstract methods.

Abstract classes help to describe generic types of behaviors and object-


oriented programming class hierarchy. It also describes subclasses to offer
implementation details of the abstract class.

What is Encapsulation in Java?


Encapsulation in Java is a mechanism to wrap up variables(data) and
methods(code) together as a single unit. It is the process of hiding information
details and protecting the data and behavior of the object.

 Encapsulation is more about “How” to achieve a functionality


 Abstraction is more about “What” a class can do.

Data Hiding in Java


Data Hiding in Java is hiding the variables of a class from other classes. It
can only be accessed through the method of their current class. It hides the
implementation details from the users. But more than data hiding, it is meant
for better management or grouping of related data.
To achieve a lesser degree of encapsulation in Java, you can use modifiers
like “protected” or “public”. With encapsulation, developers can change one
part of the code easily without affecting other.

Getter and Setter in Java


Getter and Setter in Java are two conventional methods used to retrieve and
update values of a variable. They are mainly used to create, modify, delete
and view the variable values. The setter method is used for updating values
and the getter method is used for reading or retrieving the values. They are
also known as an accessor and mutator.

 Primitive types are predefined (already defined) in Java. Non-primitive


types are created by the programmer and is not defined by Java (except
for String).
 Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
 A primitive type has always a value, while non-primitive types can
be null.
 A primitive type starts with a lowercase letter, while non-primitive types
starts with an uppercase letter.
 The size of a primitive type depends on the data type, while non-primitive
types have all the same size.

Java Syntax

public class Main {

public static void main(String[] args) {

System.out.println("Hello World");

A class should always start with an uppercase first letter. Any code inside
the main() method will be executed. every Java program has a class name
which must match the filename, and that every program must contain
the main() method.

System.out.println()
Inside the main() method, we can use the println() method to print a line of text
to the screen:

System is a built-in Java class that contains useful members, such as out,
which is short for "output". The println() method, short for
"print line", is used to print a value to the screen (or a file)

You should also note that each code statement must end with a semicolon ( ;).
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant", which
means unchangeable and read-only): e.g final int myNum = 15;

Casting. Is when you assign the value of a primitive datatype to another datatype
public class Main {

public static void main(String[] args) {

int myInt = 9;

double myDouble = myInt; // Automatic casting: int to double

System.out.println(myInt); // Outputs 9

System.out.println(myDouble); // Outputs 9.0

Code Result

\n New Line

\r Carriage Return

\t Tab

\b Backspace

\f Form Feed
Java Short Hand If...Else
(Ternary Operator)
int time = 20;

String result = (time < 18) ? "Good day." : "Good evening.";

System.out.println(result);

 The switch expression is evaluated once.


 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional

You might also like