Anything which has existing in the real world is known as object.
• Type of variables
• · Every object contains state and behaviour. • 1) Instance Variables
• Object according to java- • 2) Class Variables
• · According to java object means a block of memory which is • 3) Local Variables
created in heap area.
• 1) Instance Variables:
• · The state of an object are represented by non-static variable
and instance variable. • A variable that is defined as a member of a class is known as an
• · Behaviour of an object is represented non-static method. • 1) instance variable.
• · Before creating any object the blueprint of an object is • Memory allocated to instance variables whenever an object is
important. C created.
• · according to java blueprint means class • Instance variables are stored in heap area.
• It is an instance of a class. 2) Class Variables:
• Syntax to create an object: • A variable that is defined as a static member of a class is known
as
• ClassName ObjectReference = new Constructor();
• class variable.
• Example:
• Memory allocated to class variables whenever class is loaded.
• Emp e=new Emp();
• Class variables are stored in method area.
• new is called as dynamic memory allocation operator and it
allocates • 3) Local Variables:
• the memory to variables. • variable that is defined inside a method is known as local
variable.
• Constructor constructs the object.
• Memory allocated to local variables whenever method is called.
• Local variables are stored in stack area.
• Notes 1:-local variable can not static in java .
• No any global variable in java .(out side of class)
There are five memory blocks in Java Virtual Machine(JVM):
1) Heap Area
2) Method Area
3) Stack Area
4) Native Stack Area
5) PC(Program Counter) Registers
Execution Priority:
1) Class Variables(Static Variables)
2) Static Blocks
3) Main Method
• There are four ways to access class variales:
• 1) Directly
• 2) By using class name
• 3) By using an object
• 4) By using an object reference
• Notes >>:- 3& 4 Memory
West.
• There is only one way of
access of local variables.=>
that is Inside of class.
Arrays:
a
An array is a collection of similar data elements.
Array index always begins with 0 and ends with size-1.
In Java, Array itself an object.
Array reference is called as an object reference or reference variable.
Declaration:
Syntax:
• Methods:
• A group of statements into a single logical unit is called as method.
• In Java, methods are divided into four categories:
1) Methods with arguments and with return value.
2) Methods with arguments and without return value.
3) Methods without arguments and with return value.
4) Methods without arguments and without return value.
Methods are used to perform the task.
Advantages of methods:
• 1) Modularity
• 2) Reusability
• Instance Method:
• A method that is defined as a member of a class is called as an
instance method.
• There are two ways to access an instance method:
• 1) By using an object
• 2) By using an object reference
• Use object to access an instance method if it is required only one
time.
• Use object reference to access an instance method if it is required
more than
• one time
• 2 Method with arguments and without return value
• Method Overloading. • Method Overriding:
• if two or more methods with the same name and with different • If two or more methods with the same name and with the same
parameters list, then it is said to be method overloading. parameters list,
• Parameters can be different in no. of arguments, data types or order • then it is said to be method overriding.
of an arguments
• Example:
• n method overloading return type can be same or different.
• void add(int a, int b){}
• Examples:
• void add(int x, int y){}
• 1) Different in no. of arguments:
• Note: Methods cannot be overridden in the same class because of
• void add(int a, int b){} ambiguity to call.
• void add(int a, int b, int c){} • "static" keyword:
• 2) Different in data types: • It is called as modifier because it modifies the behaviour of a variable,
method, class
• void add(int a, int b){}
& block.
• void add(float a, float b){}
• By using static keyword, we can create class variables, class methods,
• 3) Different in order of arguments: nested top
• void add(int a, float b){} • level classes(It is a one type of inner class) & static initialization blocks.
• void add(float a, int b){} • Class Methods:
• A method that is defined as a static member of a class is called as class
method.
• There are four ways to access class method:
• 1) Directly
• 2) By using Class Name
• 3) By using an object
• 4) By using an object reference
• 3rd & 4th ways are not recommanded to use.
• Access class method directly if it is present in the same class.
• Use class name to access class method if it is present in another class.
• "this" keyword:
• It is called as an object reference or reference variable because it
refers object.
• It always refers current object.
• It is implicitly present in instance method, initialization block &
constructor.
• this keyword explicitly required to access an instance variable
"this" keyword: whenever both
It is called as an object reference or reference variable because it refers an • instance variable & local variable names are same.
object.
It always refers current object. • static method does not support this keyword in anyway.
It is implicitly present in instance method, initialization block & constructor.
"this" keyword explicitly required to access an instance
variable whenever both
instance variable & local variable names are same.
• Constructors:
• A constructor is a special method which has same name as the this();
class name and It calls default constructor of current class.
• which has no return type. this(arg1, arg2, ...... );
• Constructor is automatically whenever an object is created. It calls parameterized constructor of current class.
• Constructors are used to initialize instance variables.
Note: this(); (or) this(arg1, arg2, ... ); must be a first
statement in a constructor.
• Constructors are two types:
• 1) Default Constructor(Without Arguments)
• 2) Parameterized Constructor(With Arguments)
• If the class does not contain any other constructor then only Java
compiler implicitly provides one default constructor.
• Static Blocks:
• A block with static keyword is called as static block.
• A static block is also called as static initialization block because
static block
• can be used to initialize static variables.
• A class can have any number of static blocks and all those static
blocks are executed
• from top to bottom order whenever class is loaded.
• Static blocks are executed before main() method only.
• Concept of oops in java =>4 concept of oops
• Types of inheritance:
1=>Inheritance 2=>polimophisim3=>Ecapsulation4=>abstruction
• 1. Single level inheritance
Inheritance:
• 2. Multilevel inheritance
The process of one class acquiring the property of another class is
known as inheritance • 3. Hierarchical inheritance
. The class which is providing all its property to the another class is • 4. Multiple inheritance
known as parent class • 5. Hybrid inheritance
or super class.
. The class which is acquiring all its property from another class is
known as child class/su
In inheritance, existed class is said to be super class & new class is
said to be subclass.
Whenever super class object is created then memory allocated to
super class members only.
Whenever sub class object is created then memory allocated to both
super class & sub class members.
NOTE: in Inheritance child class acquired the property of parent class
but parent class will not
acquire the property of child class.
extends-
· It is a keyword.
. It is used to achieve the inheritance between class to class and
interface to interface.
· By using extend keyword one class can inherit only one class at a
time.
• Hybrid inheritance: it is a combination of multiple inheritance
and hierarchical .
• NOTE: in inheritance private member of a class and constructor of
a class are not inherited to
• Note1: Multipath inheritance is also one example of hybrid
inheritance.
• Note2: Java does not support multiple inheritance, multipath
inheritance & hybrid inheritance.
• Polymorphism = One thing, many forms
(“Poly” = many, “morph” = forms)
• In Java, polymorphism means the same method behaves
differently based on the object.
• Polymorphism:
• The ability to take more than one form is called as polymorphism.
• Poly means many, morphism means forms & Polymorphism
means many forms.
• There are two types of polymorphism:
• 1) Compile time polymorphism(Static polymorphism) Method
Overloading.
• 2) Run time polymorphism(Dynamic polymorphism) Method
Overriding
• 1) Compile time polymorphism:
• Binding of method call statement with method
definition is done at compile time known as
compile time polymorphism.
• If two or more methods with the same name and
with different parameters list,
• then it is said to be method overloading.
• In method overloading, parameters can be
different in no. of arguments, data types or order
of an arguments.
• In method overloading, return type can be same or
different.
Method Overriding • class Animal {
• Binding of method call statement with • public void sound() {
method definition is done at runtime is called
• [Link]("Animal makes
• as run time polymorphism. sound");
• Example: • }
• Method Overriding: • }
• If two or more methods with the same name
and with the same parameters list, then • class Dog extends Animal {
• it is said to be method overriding. • @Override
• Note: Methods cannot be overridden in the • public void sound() {
same'class because of ambiguity to call.
• Methods can be overridden in inheritance • [Link]("Dog barks");
only. • }
• }
• public class Test {
• public static void main(String[] args) {
• Animal a = new Dog(); // upcasting
• [Link]();
• }
• }
• class Animal {
• void sound() {
• [Link]("Animal sound");
• }
• }
• class Dog extends Animal {
• void sound() {
• [Link]("Dog barks");
• }
• void eat() {
• [Link]("Dog eats");
• }
• }
• public class Test {
• public static void main(String[] args) {
• Animal a = new Dog(); // Upcasting
• [Link](); // Calls Dog's method
• }
• }
• class Animal { • 1. What is Abstraction?
• void sound() { • Abstraction:
• Abstraction means hiding the complex details and showing only
• [Link]("Animal makes sound"); the important information to the user
• } • Just like:
• } • When you drive a car, you only use the steering, brake, accelerator.
You don’t need to know how the engine works internally.
• In Java, abstraction hides internal code logic and shows only
essential methods.
• class Dog extends Animal {
• Providing necessary information and hiding unnecessary
• void bark() { information is called as an abstraction.
• [Link]("Dog barks"); • (or)
• Providing necessary information without including background
• } details is called as an abstraction.
• } • How Abstraction is Achieved in Java?
• Java provides two ways to achieve abstraction:
• public class Test { • 1. Using Abstract Classes (0% to 100% abstraction)
• public static void main(String[] args) { • 2. Using Interfaces (100% abstraction)
• 1. Abstract Class (Simple Explanation)
• An abstract class cannot be instantiated (cannot create object)
• Animal a = new Dog(); // Upcasting
• It may contain abstract methods (no body)
• Dog d = (Dog) a; // Downcasting • It may contain normal methods (with body) Abstract Class:
• A class that is declared with abstract keyword is called as an
abstract class.
• [Link](); // Calling child-specific
method • Abstract class can have only abstract methods, only non abstract
methods or
• } • both abstract & non abstract methods.
• }
• Abstract Method:
• Abstract class can be inherited in other class by using extends
• A method that has no body is called an abstract keyword.
method .Abstract method must be declared with
abstract keyword in Java otherwise compile time • Whenever abstract class is inherited then all abstract methods
must be overridden in a sub class or sub class must be declared
error occurs. with abstract keyword otherwise compile time error occurs.
• Non abstract methods are called concrete • Concrete Method:
methods. • A method that has a body is called as concrete method.
• Concrete Method: • abstract void show(); = > Abstract Method
• A method that has a body is called as concrete • void print() => Concrete Method
method. {
• If the class contains an abstract method then the • -------
class must be declared with abstract keyword
otherwise compile time error abstract keyword • -------
occurs. }
• If method is abstract then class most be abstract. • void display(){} => Null Body Method
• If class is abstract then it is not compalsaries that • Note: Null body method also called
method should be abstract.
• Abstract class cannot create object .or
instantiated.
• Abstract class cannot be final. .
• Abstract method can not be static .
• Abstract method can not be final .
• Abstract method can not be private.
• Abstract class can have static members. .
• Abstract class can have main () members. .
Examples :- • Interfaces:
abstract class Animal { • It is a collection of public static final variables & public abstract methods.
• abstract void sound(); // abstract method • In interface, all variables are implicitly public static final and all methods are
implicitly public abstract.
• void sleep() { // normal method • 1) class A{}
• [Link]("Animal sleeps"); • 2) abstract class B{}
• } • 3) interface C{}
• } • (or)
• class Dog extends Animal { • abstract interface D{}
• void sound() { • Interfaces cannot be instantiated.
• Interface can be inherited into a class by using implements keyword.
• [Link]("Dog barks");
• Interface can also be inherited into another interface by using extends
• } keyword.
• } • Whenever interface is inherited then all abstract methods of an interface must
be
• 2. Interface (Simple Explanation)
• overridden in a sub class or sub class must be declared with abstract keyword
• Interface contains only abstract methods (before Java 8)
• otherwise compile time error occurs.
• Supports 100% abstraction
• Java 8 Features:
• A class implements an interface
• 1) Default methods in interface.
• 2) Static methods in interface.
• 3) Functional interfaces.
• 4) @FunctionalInterface annotation.
• 5) Lambda expressions.
• 6) Method references.
• 7) Constructor references.
• 8) Predefined functional interfaces.
• 9) Stream API.|
• 10) New Date & Time API.
Encapsulation:
• Binding of variables & methods into a class is called as an • // Step 1: Data hiding using private
encapsulation.
• (or)
• Binding of variables with methods and those methods
operating on same variabless also known as encapsulation.
• Wrapping data (variables) and methods (functions)
into a single unit (class)
Hiding important data from direct access and allowing
access through getter & setter methods only.
• class Emp
• int empNo;
• String name;
• float salary;
• void accept(){}
• void display(){}
• The Java compiler generates . class file for inner class
also.
• 2) Static Member Class:
• A class that is defined as a static member of another class
is called as static member
• class.
• 3) Local Class:
• A class that is defined inside a method is known as local
class.
• ) Anonymous Class:
• It is a one type of local class which has no name.
• It is always sub class of a class or interface .
• It is also a local class.
Feature Inner Static Nested Local Anonymous
Needs outer
Yes No Yes Yes
object
Can be static No Yes No No
Has name Yes Yes Yes No
Common use Logical grouping Utility class Method helper One-time use
• String Handling:
• There are four string related classes to handle strings:
• 1) [Link]
• 2) [Link]
• 3) [Link]
• 4) [Link]
• javap:
• It is a jdk tool and it is used to get the profile of a class or interface.
• Profile contains list of variables, methods & constructors.
• Packages:
• A package is a collection of sub packages, classes &
interfaces.
• Java compiler generates . class file for every class & every
interface.
• Java compiler also generates folder for every package &
every sub package. I
Category Package Name Simple Explanation • import [Link];
Basic features like strings, numbers, and
Core [Link]
math (automatically included).
Utilities [Link]
Useful tools such as lists, maps, random
numbers, and user input. • class Main {
• public static void main(String[]
Reads and writes data to files, keyboard,
Input / Output [Link]
or screen.
Input / Output [Link]
Faster and modern file and data
input/output handling. args) {
• LocalDate today =
Lets programs communicate over the
Networking [Link]
internet and networks.
GUI [Link]
Creates basic windows, buttons, and
graphics. [Link]();
• [Link](today);
Builds advanced and colorful desktop
GUI [Link]
user interfaces.
• }
Database [Link] Connects Java programs to databases.
Works with dates, times, and clocks
Date & Time [Link]
•}
easily.
Formats dates, numbers, and text for
Text & Format [Link]
display.
Math [Link]
Handles very large or very precise
• // output =>2026-01-22
numbers.
Protects data using encryption and
Security [Link]
security rules.
Runs multiple tasks at the same time
Multithreading [Link]
efficiently.
XML [Link] Reads and processes XML data files.
• Array=>
• String=>. What is a String in Java?
• In Java, a String is an object that represents a sequence of
characters, such as words, names, or sentences.
• Examples of strings:
• "Hello"
• "Java Programming"
• "12345"
• // wrapping class
• int a = 10;
• Integer obj = [Link](a);
• [Link](obj);//10
• //unwrapping class
• Integer obj = 20;
• int b = [Link]();
• [Link](b);//20