0% found this document useful (0 votes)
3 views42 pages

Oop in Java2

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including definitions of classes, objects, constructors, inheritance, and various Java modifiers. It explains key principles such as encapsulation, abstraction, and polymorphism, along with their significance in software development. Additionally, it highlights the advantages of OOP in Java, emphasizing code reusability, modularity, and data security.

Uploaded by

saikiransandeep1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views42 pages

Oop in Java2

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including definitions of classes, objects, constructors, inheritance, and various Java modifiers. It explains key principles such as encapsulation, abstraction, and polymorphism, along with their significance in software development. Additionally, it highlights the advantages of OOP in Java, emphasizing code reusability, modularity, and data security.

Uploaded by

saikiransandeep1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ippudu manaki ani concepts midha manchi idea

ochindhi kadha. So now Perfect ga prathi concept ni


nerchukundham with definitions.

Perfectingam Phase.
What is OOP?

Object-Oriented Programming (OOP) is a


programming paradigm that organizes data and
behavior into objects. It uses the concept of real-
world entities like objects and classes to design and
implement software. OOP simplifies complex
problems by modeling them in terms of objects and
interactions.

Key Principles of OOP:

1. Encapsulation: Bundling data and methods


together in a class.
2. Inheritance: Deriving new classes from existing
ones.
3. Polymorphism: Performing a single action in
different ways.
4. Abstraction: Hiding internal details and exposing
only essential features.

Vignesh Julakanti
What is a Class?

A class is a blueprint or template that defines the structure and behavior (data and methods)
of objects. It is like a recipe that describes how to create objects of a particular type.

Key Points about Classes:

1. A class defines:
o Attributes (variables) that represent the data.
o Methods (functions) that represent behavior.
2. Classes do not occupy memory until objects are created from them.

Here:

• Car is the class.


• brand and speed are attributes.
• accelerate() is a method that defines behavior.

What is an Object?

An object is an instance of a class. It is a real-world entity created from a class and has:

• State (data) represented by attributes.


• Behavior (functionality) defined by methods.

Key Points about Objects:

1. Objects are created using the new keyword.


2. Multiple objects can be created from the same class.

Vignesh Julakanti
What is a Constructor?

A constructor is a special method used to initialize objects of a class. It is automatically


called when an object is created. Constructors set up the initial state of an object by
initializing its attributes.

Key Characteristics of Constructors:

1. Same Name as the Class: The constructor's name must match the class name.
2. No Return Type: Constructors do not have a return type (not even void).
3. Called Automatically: The constructor is invoked when an object is created using the
new keyword.

Types of Constructors in Java

1. Default Constructor:
o A constructor with no parameters.
o It is provided by Java if no constructor is explicitly defined.

Vignesh Julakanti
Output:

Vignesh Julakanti
2) Parameterized Constructor:

• A constructor that accepts parameters to initialize the object with specific values

Output:

Vignesh Julakanti
Constructor Overloading

We can define multiple constructors in the same class, each with a different set of parameters.
This is called constructor overloading.

Vignesh Julakanti
The super keyword is used to:

1. Access parent class methods or fields.


2. Call the parent class constructor.

Go to page no 35, to know more about


super for constructors.

Vignesh Julakanti
What is Inheritance?

Inheritance is an Object-Oriented Programming (OOP) concept where a child class (or


subclass) derives properties (fields) and behaviors (methods) from a parent class (or
superclass). It promotes code reusability, extensibility, and maintainability.

Key Features of Inheritance

1. Code Reusability: Common functionality can be reused in derived classes.


2. Extensibility: Child classes can extend the functionality of the parent class.
3. Method Overriding: Child classes can override parent methods to provide specific
implementations.
4. Hierarchical Structure: Helps model "is-a" relationships (e.g., a Car is-a Vehicle).

Types of Inheritance in Java

Note: Java does not support multiple inheritance with classes to avoid ambiguity. It is achieved
through interfaces.

Vignesh Julakanti
Multiple Inheritance

Vignesh Julakanti
Single Inheritance: A child class inherits from a single parent class.
Example: Car inherits from Vehicle.

Vignesh Julakanti
Multilevel Inheritance: A child class inherits from a parent class, and
that parent class itself inherits from another class.
Example: Sedan inherits from Car, which inherits from Vehicle.

Vignesh Julakanti
Hierarchical Inheritance: Multiple child classes inherit from a single
parent class.
Example: Car and Bike inherit from Vehicle.

Vignesh Julakanti
Multiple Inheritance (via Interfaces in Java): A class inherits from
multiple interfaces.
Example: FlyingCar implements Vehicle and FlyingObject.

Vignesh Julakanti
Java Modifiers
We divide modifiers into two groups:

• Access Modifiers - controls the access level


• Non-Access Modifiers - do not control access level, but provides
other functionality

Access Modifiers in Java

Access modifiers in Java control the visibility and accessibility of classes, methods, and
variables. They are used to enforce encapsulation and maintain control over how different
parts of a program interact.

Vignesh Julakanti
1. Public Modifier

• Description: The public modifier allows the member to be accessed from anywhere,
without restrictions.
• Scope: Accessible in the same class, same package, subclasses, and outside packages.

Vignesh Julakanti
2. Protected Modifier

• Description: The protected modifier allows access within the same package and
subclasses in different packages.
• Scope: Accessible in the same class, same package, and subclasses (even in different
packages).

Vignesh Julakanti
3. Default (Package-Private) Modifier

• Description: When no modifier is specified, the member is accessible only within the
same package.
• Scope: Accessible in the same class and package, but not in subclasses or other
packages.

Vignesh Julakanti
4. Private Modifier

• Description: The private modifier restricts access to within the class only.
• Scope: Accessible only in the same class

Vignesh Julakanti
Non-Access Modifiers in Java

Non-access modifiers in Java define additional properties of classes, methods, or variables.


These modifiers control aspects like behavior, memory management, and usability.

1. Static Modifier

The static modifier in Java allows members (variables, methods, and blocks) to belong to
the class rather than any specific object. These members are shared among all instances of the
class.

Features of static:

1. Shared Memory: Static members are stored in a common memory area, saving space.
2. Class-Level Access: Static members can be accessed using the class name without creating
an object.
3. Initialization Order:
o Static variables and blocks are initialized in the order they appear in the class.

Vignesh Julakanti
Static Variables:

• Shared among all objects of the class.


• Changes made to the variable are reflected across all objects.

Static Methods:

• Can be called using the class name.


• Cannot access non-static members directly (since they are tied to objects).

Vignesh Julakanti
2. Final Modifier

The final modifier is used to restrict modification. It can be applied to variables, methods,
and classes.

Features of final:

1. Final Variables: Values cannot be changed after initialization.


2. Final Methods: Cannot be overridden in subclasses.
3. Final Classes: Cannot be extended.

Final Variables:

• Must be initialized either at declaration or in the constructor.


• Acts as a constant.

Vignesh Julakanti
Final Methods:

• Prevents method overriding in subclasses.

Final Classes:

• Prevents inheritance.

Vignesh Julakanti
3. Abstract Modifier

The abstract modifier is used for incomplete classes or methods. These classes or methods
must be implemented in subclasses.

Abstract Classes:

• Cannot be instantiated.
• Can have both abstract and concrete (non-abstract) methods.
• Used when some behavior is common but certain details vary across subclasses.

Vignesh Julakanti
4. Synchronized Modifier

• Description: The synchronized modifier ensures that only one thread can access a
method or block at a time.
• It is used to avoid race condition
• Race Condition:

A race condition occurs when two or more threads or processes try to access and
modify the same resource (such as a variable or file) simultaneously, leading to
unpredictable or incorrect results.

5. Volatile Modifier

• Description: The volatile modifier ensures that a variable's value is always read
from and written to main memory, not a thread's local cache.

Vignesh Julakanti
6. Transient Modifier

• Description: The transient modifier excludes variables from being serialized.

7. Native Modifier

• Description: The native modifier is used for methods implemented in non-Java


languages like C or C++.

8. Strictfp Modifier

• Description: The strictfp modifier ensures consistent floating-point calculations


across platforms.
• Calculations strictly conform to IEEE 754, ensuring consistent results.

Vignesh Julakanti
Interface in Java
What is an Interface?

An interface in Java is a blueprint of a class that defines a set of abstract methods (methods
without a body) and constants (final variables). It specifies what a class must do but not
how it does it. A class that implements an interface provides the concrete implementation for
its methods.

Key Features of Interfaces:

1. Abstract Methods: By default, all methods in an interface are public and abstract
(before Java 8).
2. Default Methods: From Java 8, interfaces can have methods with a default
implementation using the default keyword.
3. Static Methods: From Java 8, interfaces can have static methods.
4. Multiple Inheritance: A class can implement multiple interfaces.
5. Constants: All variables in an interface are public, static, and final by default.

Vignesh Julakanti
Vignesh Julakanti
Vignesh Julakanti
Vignesh Julakanti
What is a Package?

A package in Java is a namespace that organizes classes, interfaces, and sub-packages. It is


used to avoid name conflicts, improve code modularity, and control access.

Vignesh Julakanti
Vignesh Julakanti
What is Encapsulation?

Encapsulation is the process of bundling data (fields) and methods (functions) that operate
on the data into a single unit, typically a class. It restricts direct access to some of the object’s
components and protects the object from unintended interference.

Key Features of Encapsulation

1. Data Hiding: Prevents external access to sensitive data by declaring fields as private.
2. Controlled Access: Provides access to the data using getter and setter methods.
3. Improved Security: Protects data from unauthorized access or modification.
4. Modularity: Encapsulation helps in creating self-contained, reusable classes

Vignesh Julakanti
What is Abstraction?

Abstraction is the process of hiding the implementation details of a system and exposing
only the essential features. It allows a user to interact with an object without knowing how it
works internally.

Key Features of Abstraction

1. Focus on Essentials: Only the necessary details are exposed to the user.
2. Hides Complexity: The implementation is hidden, and only the interface is visible.
3. Implemented Using:
o Abstract Classes: Classes with at least one abstract method.
o Interfaces: Fully abstract blueprints for classes.

Vignesh Julakanti
What is Polymorphism?

Polymorphism is a key feature of Object-Oriented Programming (OOP) that allows a


single entity (method, operator, or object) to behave differently based on the context. The
term "Polymorphism" is derived from the Greek words "poly" (many) and "morph"
(forms), meaning "many forms."

Types of Polymorphism in Java

1. Compile-Time Polymorphism (Static Binding):


o Achieved through method overloading.
o The method to be executed is determined at compile time.
2. Run-Time Polymorphism (Dynamic Binding):
o Achieved through method overriding.
o The method to be executed is determined at runtime.

1. Compile-Time Polymorphism (Method Overloading)

Method Overloading occurs when multiple methods in the same class have the same name
but different parameters (type, number, or order).

Vignesh Julakanti
Vignesh Julakanti
2. Run-Time Polymorphism (Method Overriding)

Method Overriding occurs when a subclass provides a specific implementation of a method


already defined in its parent class.

Rules for Method Overriding:

1. The method must have the same name and parameters as in the parent class.
2. The method in the child class must have the same or more accessible modifier.
3. The method in the child class cannot override a method declared final or static.

Vignesh Julakanti
Some questions for Interview.
➔ OOP vs Procedural Programming

➔ Is java pure Object Oriented Language.


No, Java is not a pure object-oriented
language. This is because it supports
primitive data types and static methods
and variables.
But Java closely follows OOP rules.

Vignesh Julakanti
➔ Difference Between classes and Objects

➔ Abstract class vs Interface

➔ Encapsulation vs abstraction

Vignesh Julakanti
➔ Method Overloading vs Method
Overriding

Advantages of OOP in java


Advantages of Object-Oriented Programming (OOP) in Java

1. Code Reusability:
o Using concepts like inheritance, existing code can be reused in new programs,
reducing redundancy and saving time.
2. Modularity:
o Programs are organized into classes and objects, making the code modular and
easy to maintain and debug.
3. Data Security:
o Encapsulation ensures that sensitive data is hidden from unauthorized access,
allowing control over data manipulation.
4. Improved Productivity:
o Features like reusability and modularity allow developers to write efficient and
reusable code, boosting productivity.
5. Real-World Mapping:
o OOP models real-world entities using objects and classes, making it intuitive
and easier to understand complex systems.
6. Scalability:
o OOP makes it easier to scale programs by adding new classes and objects
without affecting the existing system.
7. Ease of Maintenance:

Vignesh Julakanti
o By following the principles of modularity and encapsulation, updating or
maintaining the code becomes easier.
8. Flexibility through Polymorphism:
o The ability to define multiple behaviors (method overloading and overriding)
allows for flexible and dynamic code.
9. Code Clarity:
o OOP enforces a structured approach to programming, improving readability
and making code self-explanatory.
10. Supports Design Patterns:
o OOP principles serve as the foundation for implementing standard design
patterns that improve software design.
11. Extensibility:
o Through inheritance and polymorphism, the system can be extended to include
new features without modifying the existing code.

Types Of inheritance

Vignesh Julakanti
Static polymorphism is also know as compile
time polymorphism(Method overloading).

Dynamic Polymorphism is also known as fun


time polymorphism(Method overriding).

Vignesh Julakanti
Hurray Macha You did it.
Video is Done.
Congratulations.

All the Best for you Goals.


- Yours Lovingly,
Engineering Animuthyam.

Vignesh Julakanti

You might also like