Oop Basic Concepts
Oop Basic Concepts
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented
Programming is a methodology or paradigm to design a program using classes and objects. It simplifies
software development and maintenance by providing some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
Object:
Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard,
bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. It contains an address and takes up some space in
memory. Objects can communicate without knowing the details of each other's data or code. The
object of a class can be created by using the new keyword in the Java Programming language.
Example: A dog is an object because it has states like color, name, breed, etc. as well as behaviors like
wagging the tail, barking, eating, etc.
Class:
A class can also be defined as a blueprint from which you can create an individual object. Class does not
consume any space.
Abstraction:
Hiding internal implementation and showing functionality only to the user is known as abstraction. For
example, phone call, we do not know the internal processing.
Encapsulation
In OOP, data and methods operating on that data are combined together to form a single unit, which is
referred to as a Class. Encapsulation is the mechanism that binds together code and the data it
manipulates and keeps both safe from outside interference and misuse.
Inheritance:
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.
It provides code reusability. It is used to achieve runtime polymorphism.
Inheritance is a process of obtaining the data members and methods from one class to another class. It
is a fundamental feature of object-oriented programming.
Sub Class: The class that inherits the other class is known as a subclass (or a derived class, extended
class, or child class)
The subclass can add its own fields and methods in addition to the superclass fields and methods.
Polymorphism:
Definitions of Polymorphism:
Polymorphism allows you to define one interface and have multiple implementations.
We can create functions or reference variables that behave differently in different programmatic
contexts.
Real-world example: Suppose if you are in a classroom at that time you behave like a student, when you
are in the market at that time you behave like a customer, and when you are at home at that time you
behave like a son or daughter. Here one person presents different behaviors in different contexts.
Dynamic binding (also known as late binding) is a concept in Object-Oriented Programming (OOP) that
refers to the process of determining the method to be called or the function to be executed at runtime,
rather than at compile-time.
In OOP, dynamic binding is primarily used in the context of inheritance and polymorphism. It allows an
object to invoke the appropriate method of its derived class, even if the method is not explicitly known
at compile time. This happens through the use of virtual methods (in languages like C++), abstract
methods (in languages like Java), or interfaces.
In Object-Oriented Programming, message passing refers to how objects interact by sending and
receiving messages in the form of method calls. These messages allow objects to request actions from
other objects without needing to know their internal details. This concept promotes encapsulation,
polymorphism, and loose coupling, which are essential principles of OOP that make software more
modular, flexible, and maintainable.
message passing allows objects to interact with each other by invoking methods and passing data
between them. Message passing involves sending messages from one object to another, triggering a
specific behavior or action. These messages are typically in the form of method invocations, where one
object invokes a method on another object to request an operation or exchange information
• Documentation Section
• Package Declaration
• Import Statements
• Interface Section
• Class Definition
• Class Variables and Constants
• Main Method Class
• Methods and Behaviors
Documentation Section
The documentation section is an important section but optional for a Java program. It includes basic
information about a Java program. The information includes the author's name, date of creation,
version, program name, company name, and description of the program. It improves the readability of
the program. Whatever we write in the documentation section, the Java compiler ignores the
statements during the execution of the program. To write the statements in the documentation section,
we use comments. The comments may be single-line, multi-line, and documentation comments.
Documentation Comment: It starts with the delimiter (/**) and ends with */.
Package Declaration:
The package declaration is optional. It is placed just after the documentation section. In this section, we
declare the package name in which the class is placed. Note that there can be only one
package statement in a Java program. It must be defined before any class and interface declaration. It is
necessary because a Java class can be placed in different packages and directories based on the module
they are used. For all these classes package belongs to a single parent directory. We use the
keyword package to declare the package name.
example:
package com.javatpoint; //where com is the root directory and javatpoint is the subdirectory
Import Statements:
The package contains the many predefined classes and interfaces. If we want to use any class of a
particular package, we need to import that class. The import statement represents the class stored in
the other package. We use the import keyword to import the class. It is written before the class
declaration and after the package statement. We use the import statement in two ways, either import a
specific class or import all classes of a particular package. In a Java program, we can use multiple import
statements.
For example:
import java.util.*; //it imports all the class of the java.util package
Interface Section:
interface car
void start();
void stop();
Class Definition:
In this section, we define the class. It is vital part of a Java program. Without the class, we cannot create
any Java program. A Java program may conation more than one class definition. We use
the class keyword to define the class. The class is a blueprint of a Java program. It contains information
about user-defined methods, variables, and constants. Every Java program has at least one class that
contains the main() method. For example:
Class Variables :
In this section, we define variables and constants that are to be used later in the program. In a Java
program, the variables and constants are defined just after the class definition. The variables and
constants store values of the parameters. It is used during the execution of the program. We can also
decide and define the scope of variables by using the modifiers. It defines the life of the variables.
example:
int id;
double percentage;
In this section, we define the main() method. It is essential for all Java programs. Because the execution
of all Java programs starts from the main() method. In other words, it is an entry point of the class. It
must be inside the class. Inside the main method, we create objects and call the methods. We use the
following statement to define the main() method:
example:
//statements
} }
In this section, we define the functionality of the program by using the methods. The methods are the
set of instructions that we want to perform. These instructions execute at runtime and perform the
specified task.
example:
void display()
System.out.println("Welcome to javatpoint");
//statements
• Java is an OOP language which is case-sensitive, platform-independent, and uses both compiler
and interpreter.
• Java program structure consists of compulsory classes to be written.
• The main() method is the entry point of the compiler from where it starts the execution.
• Variables and identifiers are used for naming variables and classes.
• Keywords are reserved words which are predefined and have a meaning.
• Access modifiers serve to specify the visibility and accessibility of classes, methods, constructors,
or data members within a program.
• Java file should be saved with the name of the public class and should have the extension .java
after the file name.