0% found this document useful (0 votes)
34 views82 pages

Oop in Java Final

The document is a comprehensive guide on Object-Oriented Programming (OOP) with a focus on Java, covering key concepts such as classes, objects, inheritance, encapsulation, polymorphism, and various modifiers. It includes explanations of constructors, method overloading, and overriding, along with examples of different types of inheritance. The content is structured into phases for learning, perfecting, and revising OOP concepts, with practical coding examples and links to additional resources.
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)
34 views82 pages

Oop in Java Final

The document is a comprehensive guide on Object-Oriented Programming (OOP) with a focus on Java, covering key concepts such as classes, objects, inheritance, encapsulation, polymorphism, and various modifiers. It includes explanations of constructors, method overloading, and overriding, along with examples of different types of inheritance. The content is structured into phases for learning, perfecting, and revising OOP concepts, with practical coding examples and links to additional resources.
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
You are on page 1/ 82

Engineering Animuthyam

OOP – Object Oriented Programming

Super Easy Explanation.


Single Shot 💉

Why Oops?

Vignesh Julakanti
Phases of this Video.
1) Learningam Phase
2) Perfectingam Phase
3) Quick Revisingam Phase
Samaptham – End Card.

Kids leave this video before finishing Learningam.


Men/Women finishes Learningam.
Legends finishes Perfectingam.
U,the one greater than every one else will finish
Quick Revisingam also.

Wishing you,
All the very best to finish this best Course on
OOP in java

This youtube video Link:


https://youtu.be/dT-
4mt3lxJo?si=F0M40_2UBmWc5u-2

Vignesh Julakanti
Miku Oops world ki oka sneak peak istha. Dhani
tarvatha basics nundi ani chudham.
Just observe how beautiful oops is ani.

Mana Animuthyam Oka Chai, Shop Petkundu.

Branch : “BahubaliAdda”
cupsSold : 4
PricePerCup : 5
numberOfEmployees : 2

Profits Ochinai mana Animuthyam ki, so inka ekva


branchulu terchindu.

Branch : “PokiriJaaga”
cupsSold : 8
PricePerCup : 2

Vignesh Julakanti
numberOfEmployees : 5

Branch : “JalsaKottu”
cupsSold : 2
PricePerCup : 4
numberOfEmployees : 7

Branch : “Pushpa”
cupsSold : 1
PricePerCup : 6
numberOfEmployees : 4

Ippudu e data motham manaki java file lo kavali


ante ela?
Vignesh Julakanti
Prasthuthaniki ila ankondi.

Manaki nachina mixed DataTypes Create


Cheskovochu Classes UseChesi.

Lets Create a DataType Called ChaiShop.


That has branchName
and cupsSold with it.

Methods inside class.

Constructors.
Constructors with parameters.

Display.
this keyword.
Multiple keywords.
getRevenue;

AnnaChaii

Vignesh Julakanti
Two Access Modifiers gurinchi nerchukundham.

Public
Private.

Java Inheritance
Mana Animuthyam tea ki masthu profitlu ochinai
ani, oka xerox shop petindu.

ChaiShop Attributes.
branchName;
cupsSold;
pricePerCup;

Xerox Shop Attributes.


branchName;
papersSold;
PricePerPaper;

Vignesh Julakanti
Common attributes.
ownerName
ownerAge
ownerNum

common method
display ownerInfo.

extends keyword.
Owner can have its own Objects also.

Parent class / BaseClass.


Child class.

Hierarchical Inheritance
More than one subclass inherits the parent class.
Owner ni, ChaiShop and TeaShop rendu Extend
Cheskuntunai.

Vignesh Julakanti
Method OverLoading.
Same method name but it should be different in any
one of the below.
➔ Number of Parameters
➔ Type of Parameters
➔ Order of parameters
(single class Example)
With display(int a,int s)

Method Overriding.
Same method name with same number and type
and order of parameters.

@Override annotation.

Vignesh Julakanti
Inko oka class Create Chedham.
Room
roomNo
noOfChairs
isAcRoom

Now Room extends ChaiShop.


( MultiLevel Inheritance)

Types of Inheritance
• Single Inheritance
o One parent class – One child class.

Vignesh Julakanti
o Owner – ChaiShop

• Multilevel Inheritance
o Multiple level inheritance
o Owner – Chaishop – Room

Vignesh Julakanti
• Hierarchical Inheritance
o Single Base class and multiple child classes
o Owner – chaiShop, XeroxShop

• Hybrid Inheritance
o Mix of two or more types of inheritance.
o Owner – chaiShop (-> Shop), XeroxShop

Vignesh Julakanti
• Multiple Inheritance
o A child class inherits from multiple parent
class.
o Lets try to check this with ChaiPodiSupplier
example

Vignesh Julakanti
o Ippudu mana tea shop ki ChaiPodiSupplier
supply chese vala
▪ Class kuda tayaru chedham.

▪ supplierName.
▪ supplierAge.
▪ supplierHeight.

▪ Ippudu mana ChaiShop ki.


Owner class lo unavi kavali.
Supplier class lo unavi kavali.

o But Java lo single class can’t extend


multiple parent classes.
o Why? (Finish this).
chaiPodiSupplier and Owner has display
method.
Now if ChaiShop inherits Owner and
ChaiPodiSupplier, What display should it
use inside chaiShop.

Also We have famous Diamond problem.

Vignesh Julakanti
A has display.
B and c overrides that method differently.
Now if D inherits B and C both What
version should it consider.
Mari ela?

Interface

So we have something called Interface

A single class can inherit multiple interfaces.

Class implements interface1,interface2.


All variables should be initialized in Interface.
Method should not have body.
Every Method should be overrided in its child class.

Vignesh Julakanti
Inner Classes
Class inside class.

Java Package.
Folders for Java files.
Group of related classes ni oka structured format lo
petadaniki vadutham.

Vignesh Julakanti
https://www.jdoodle.com/online-java-compiler-ide
e compiler vadudham to understand Package.

Owner.Java has ownername Now how to access it in


Myclass.Java which will have ChaiShop to extend it.

Owner class and variables should be public inorder


to access in Myclass.Java

Using above diagram lets understand about


packages.

Vignesh Julakanti
If a file does not have any package, then it will be
default package means. That folder itself is a
package. And it can’t be accessed from any other
package.

Modifiers.
1) Access Modifiers.
2) Non – Access Modifiers.
Access Modifiers.
public
protected
default
private

Vignesh Julakanti
Lets start with Access Modifiers for variables.
public String publicn = "pub";
protected String protectedn = "prot";
String defaultn = "def";
private String privaten = "priv";

System.out.println(ch.publicn);
System.out.println(ch.protectedn);
System.out.println(ch.defaultn);
System.out.println(ch.privaten);

Access Modifiers for Methods.


public void publicMethod() {

System.out.println("Public Method");

protected void protectedMethod() {

System.out.println("Protected Method");

void defaultMethod() {

System.out.println("Default Method");

private void privateMethod() {

System.out.println("Private Method");

Vignesh Julakanti
ch.publicMethod();

ch.protectedMethod();

ch.defaultMethod();

ch.privateMethod();

Access Modifiers for classes.


Top level classes can only have public or default.

Non – Access Modifiers.


➔ static
➔ final
➔ abstract
➔ synchronized
➔ volatile
➔ transient
➔ native

First 3 perfect ga with examples important.


Mithavi just oka idea unte chalu.

Vignesh Julakanti
static
Static member belongs to class not object.
Can be directly accessed through class no need of
object creation.
Will be initialized only once.

Create a Class Counter and try to increment its


count

Static methods example.


Static methods cannot access non static members.

Vignesh Julakanti
Some examples for static.

Inner classes can be static.

Vignesh Julakanti
No inheritance with static members.
(First we need to understand few ways of object
creation)

Vignesh Julakanti
Left side Reference decides, which variables and
static members to access.
If Right side object don’t have same method(non-
static) then, Left side Reference only decides which
method to access.

Right side Object decides, If same method(non-


static) is there in left side reference and right side
object, then right side Object decides which method
to access.

Vignesh Julakanti
Final
final keyword makes a variable constant.
You cannot change it.
You should always initialize final variables or we
should at least initialized in constructor.

Vignesh Julakanti
Final methods cannot be overridden.

Final class cannot be extended or inherited.

Vignesh Julakanti
abstract
If a class is abstract class we cannot directly create
its object.
It should be extended by other class.

Abstract class can contain both abstract methods


and normal methods.

Abstract methods are method without


implementation ( only declaration).
These should be overrided by subclass.
Abstract methods can only be created inside
abstract class

Vignesh Julakanti
synchronized
Ensures thread safety by allowing only one thread
to access a block of code or method at a time.
Prevents race conditions in multi-threaded
environment.

Vignesh Julakanti
volatile
Ensures visibility of changes to a variable across
threads.
Prevents threads from caching the value of the
variable; always fetches the latest value from
memory.

transient
Prevents a variable from being serialized.
Means while storing an object in a file, if we make a
variable transient, it will not store that value in file.
We can make sensitive data transient.

Vignesh Julakanti
native
Declares a method implemented in a language like
C or C++ (native code).

Revise above Data modifiers 😁.

Vignesh Julakanti
Features of OOP.

1) Encapsulation.
The process of wrapping data (variables) and
methods (functions) into a single unit (class)
and restricting direct access to some of the
object's components.

Uses getters and setters to achieve it.

Declares variables as private and methods as


public.

Vignesh Julakanti
2) Abstraction
Hides implementation details and exposes only
the essential features of an object.
This can be achieved using abstract classes and
interfaces.

Vignesh Julakanti
3) Polymorphism
Polymorphism is of 2 types.
➔ Compile time polymorphism(Method
Overloading).

➔ Run time polymorphism(Method Overriding).

Vignesh Julakanti
4)Inheritance
Allows one class (subclass/child class) to inherit
fields and methods from another class
(superclass/parent class).

5) Dynamic Binding
Refers to the process where the method to be
invoked is determined at runtime, enabling
runtime polymorphism.

Vignesh Julakanti
Constructors in Inheritance.
super() will get inserted in child class constructor by
default.

Vignesh Julakanti
Vignesh Julakanti
Vignesh Julakanti
Vignesh Julakanti
Vignesh Julakanti
Singleton class.
It a class that allows only one object to be created.

Vignesh Julakanti
Structure of Collection in java

Vignesh Julakanti
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