Oop in Java Final
Oop in Java Final
Why Oops?
Vignesh Julakanti
Phases of this Video.
1) Learningam Phase
2) Perfectingam Phase
3) Quick Revisingam Phase
Samaptham – End Card.
Wishing you,
All the very best to finish this best Course on
OOP in java
Vignesh Julakanti
Miku Oops world ki oka sneak peak istha. Dhani
tarvatha basics nundi ani chudham.
Just observe how beautiful oops is ani.
Branch : “BahubaliAdda”
cupsSold : 4
PricePerCup : 5
numberOfEmployees : 2
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
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;
Vignesh Julakanti
Common attributes.
ownerName
ownerAge
ownerNum
common method
display ownerInfo.
extends keyword.
Owner can have its own Objects also.
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
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.
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
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.
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);
System.out.println("Public Method");
System.out.println("Protected Method");
void defaultMethod() {
System.out.println("Default Method");
System.out.println("Private Method");
Vignesh Julakanti
ch.publicMethod();
ch.protectedMethod();
ch.defaultMethod();
ch.privateMethod();
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.
Vignesh Julakanti
Some examples for 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.
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.
Vignesh Julakanti
abstract
If a class is abstract class we cannot directly create
its object.
It should be extended by other 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).
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.
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).
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?
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.
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:
What is an Object?
An object is an instance of a class. It is a real-world entity created from a class and has:
Vignesh Julakanti
What is a Constructor?
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.
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:
Vignesh Julakanti
What is Inheritance?
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 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
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:
Static Methods:
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:
Final Variables:
Vignesh Julakanti
Final Methods:
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
7. Native Modifier
8. Strictfp Modifier
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.
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?
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.
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.
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?
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)
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
Vignesh Julakanti
➔ Difference Between classes and Objects
➔ Encapsulation vs abstraction
Vignesh Julakanti
➔ Method Overloading vs Method
Overriding
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).
Vignesh Julakanti
Hurray Macha You did it.
Video is Done.
Congratulations.
Vignesh Julakanti