INTELLIGENCE LEARNING
BCS403 Object Oriented
Programming with Java
UNIT 1 ONE SHOT
INTELLIGENCE LEARNING
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
SYLLABUS
Unit 1
Introduction: Why Java, History of Java, JVM, JRE, Java Environment,
Java Source File Structure, and Compilation. Fundamental.
Programming Structures in Java: Defining Classes in Java, Constructors,
Methods, Access Specifies, Static Members, Final Members, Comments,
Data types, Variables, Operators, Control Flow, Arrays & String.
Object Oriented Programming: Class, Object, Inheritance Super Class,
Sub Class, Overriding, Overloading, Encapsulation, Polymorphism,
Abstraction, Interfaces, and Abstract Class.
Packages: Defining Package, CLASSPATH Setting for Packages, Making
JAR Files for Library Packages, Import and Static Import Naming
Convention For Packages.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Introduction to Java
Java is a popular computer programming language that is used to create software and apps.
It was made by James Gosling in 1995 at Sun Microsystems (now owned by Oracle). You can
use Java to build Mobile apps (like Android apps), Websites, Games, Big business
applications. One big reason Java is special “You write the code once, and it can run on any
computer — this is called “Write Once, Run Anywhere.””
Why Learn or Use Java?
1. Works Everywhere:- Java can run on any computer or device that has Java software
installed.
2. Easy to Learn:- Its rules (syntax) are simple and similar to C/C++ if you've seen those.
3. Organized Code:- Java uses objects, which helps keep code neat and reusable.
4. Very Safe:- Java doesn’t let you do risky things like using unsafe memory, so it's more
secure.
5. Less Bugs, More Stability:- It handles errors well and does things like cleaning up
memory automatically.
6. Can Do Many Things at Once:- Java can run several tasks at the same time
(multithreading), which is great for games and fast apps.
7. Lots of Ready-Made Tools:- Java gives you many built-in tools to save time while
coding.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
History of Java
1. Started in 1991:- Java was created by James Gosling and his team at Sun
Microsystems. It was part of a secret project called the Green Project. The
goal was to make software for small electronic devices like TVs.
2. Java’s First Name – Oak:- Java was first named Oak, because Oak is a tree
that near James Gosling’s office. Later, the name was changed to Java,
which is a type of coffee.
3. Java Released in 1995:- Java was released to the public in 1995. It was
made to be simple, secure, and able to run on any computer. This idea was
called "Write Once, Run Anywhere".
4. Used in Websites and Phones:- Java became popular for making websites,
mobile apps, and games. It was also used in mobile phones and smart
devices.
5. Oracle Buys Java in 2010:- In 2010, a company called Oracle bought Java
from Sun Microsystems. Oracle continued to improve Java and release new
versions.
6. Modern Java (Today):- Java now has many new features and is used in:
Android apps, Big company software, Banking systems, Websites and cloud
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Java Environment
The Java Environment is everything you need to write, run, and test Java
programs. It includes different tools and parts that work together to make Java
programs run properly.
Main Parts of Java Environment
1. JDK (Java Development Kit)
2. JRE (Java Runtime Environment)
3. JVM (Java Virtual Machine)
4. Compiler
5. Byte code
6. Interpreter
7. Standard Libraries
Working
1. You use the JDK to write and compile the code.
2. Then, the JRE (inside JDK) helps run it.
3. The JVM (inside JRE) reads the byte code and executes it.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
JDK (Java Development Kit)
Full Form: Java Development Kit
Purpose: JDK is used to write, compile, and run Java programs.
Imagine you want to make a Java program.
To do this, you need:
1. Tools to write the program (like a pen).
2. Tools to convert your code into byte code (compiler).
3. Tools to run the program (JRE & JVM).
4. The JDK gives you all of this in one package.
JDK Contains:
Part What It Does
JRE Runs your Java programs.
JVM Part of JRE, runs byte code.
Compiler Converts your code (.java) to byte code (.class).
Debugger & Tools Helps fix and test your programs.
Java Docs Tool
LIKE, SHARE,Makes documentation for your code.
COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
JVM (Java Virtual Machine)
Full Form: Java Virtual Machine Purpose: It runs Java programs.
When you write a Java program, it does not run directly on your computer.
First, the Java program is converted into byte code (a special form of code).
This byte code is understood and run by the JVM.
It reads and runs Java byte code. It helps Java programs work the same on any
computer (Windows, Mac, Linux). (यह जावा प्रोग्रामों को ककसी भी कंप्यट ू र
(ववंडोज, मैक, लऱनक्स) ऩर समान रूऩ से काम करने में मदद करता है ।) It
handles things like:
1. Memory management (manages how much space your program uses)
2. Garbage collection (cleans unused data)
3. Error handling (manages mistakes in code while running)
You need JVM to understand Java bytecode
Imagine your Java program is a storybook written in a special language. The
JVM is like a translator that reads the book and speaks it in your language
(computer's language).
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Component Explanation
Class Loader Loads Java classes into memory to be used in the program.
Method Area Stores class-level data like method names and variable types.
Heap Stores all objects and their data during program execution.
JVM Language Stacks Helps run methods and keeps temporary data during execution.
PC Registers Stores the address of the current instruction being executed.
Native Method Stacks Supports running methods written in other languages like C/C++.
Execution Engine Actually runs the bytecode instructions.
Native Method Interface Connects Java code with code written in native languages.
LIKE, SHARE,
ContainsCOMMENT
Native Method Libraries & SUBSCRIBE
code written in other languages that Java can use.
INTELLIGENCE LEARNING
JRE (Java Runtime Environment)
Full Form: Java Runtime Environment
Purpose: It provides everything needed to run a Java program.
If you want to run a Java program on your computer, you must install JRE. You
need JRE to run Java programs
JRE is like a toolkit that has:
The JVM (to run your Java program). Some Java libraries (pre-made code/tools
that your program may need). Supporting files.
JRE do not do These:- You cannot write or compile new Java programs using
only JRE. It is only for running Java programs, not making them.
How They Work Together:
1. You write a Java program → MyProgram.java
2. You compile it → gets converted to byte code → MyProgram.class
3. The JVM (inside JRE) reads that .class file and runs it.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Java Source File Structure
// 1. Package Declaration (optional)
package mypackage;
// 2. Import Statements (optional)
import java.util.Scanner;
// 3. Class Declaration
public class MyProgram {
// 4. Constructor (optional)
public MyProgram() {
System.out.println("Constructor runs when object is created");
}
// 5. Main Method
public static void main(String[] args) {
MyProgram obj = new MyProgram(); // creates object
System.out.println("Hello Java!");
}
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Part Meaning
Package Declaration Groups your file into a folder (optional).
Brings ready-made Java tools to use. Brings tools
Import Statement
from Java’s library.
The main building block of your program. You
Class Declaration must write your code inside a class. The file
name and class name must match.
Special code that runs when you create an
Constructor
object.
This is the starting point. The program runs from
Main Method
here.
Statements / Code Instructions that do something, like printing.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Access Modifiers
Access modifiers control who can see or use parts of your code like classes,
methods, or variables.
Types of Access Modifiers
public Anyone can access it from anywhere.
Use when: You want anyone to access it.
Example:-
public class Car { // Anyone can access this class
public void start() { // This method can be called from anywhere
System.out.println("Car started");
}
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Explanation
It can be Accessed Only inside the same Only the class itself can
private
class use it.
Use when: You want it to be used only inside the same class.
Example:-
public class Car {
private int speed; // Only Car class can access this
private void secretFeature() {
System.out.println("Secret feature");
}
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Explanation
It can be Accessed Same Accessible in the same group and
protected
package + subclasses by child classes.
Use when: You want to share with related classes (child classes).
Example:-
class Vehicle {
protected void horn() {
System.out.println("Beep!");
}
}
class Car extends Vehicle {
void test() {
horn(); // Allowed because Car is a subclass
}
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Explanation
(no
It can be Accessed Same Can be used only in the same
modifier)(default
package only folder/group (package).
)
Use when: You want access only inside the same package.
Example:-
class Bike { // no modifier = default
void ride() { // Only other classes in the same package can use
this
System.out.println("Riding...");
}
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Modifier Used With Who Can Access It?
public Class, method, variable Anyone, from anywhere
private Method, variable Only inside the same class
protected Method, variable Same package + subclasses
(default) Class, method, variable Only inside the same package
Compilation in Java
Compilation means changing your Java code into a form that the computer
can understand.
Steps of Compilation:
1. You write code in a file like MyProgram.java.
2. Java compiler (javac) changes it into byte code → MyProgram.class.
3. The JVM (Java Virtual Machine) runs the byte code.
Compilation Process:
1. Write code → Hello.java
2. Compile: javac Hello.java → creates Hello.class
3. Run: java Hello → Output: Hello Java!
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Java Fundamentals
Term Meaning
Class The main block of code; like a container.
Object A real thing created from a class.
Method A block that does some work (like a function).
Variable A container to store data (like numbers or words).
Data Type Tells what kind of data (e.g., int, float, String).
Main Method The starting point of every Java program.
Constructor A special method that runs when an object is created.
Statement A single line of instruction (ends with ;).
Comments Notes in the code for humans; ignored by Java.
Loop Repeats code (like for, while).
Condition Checks something (like if, else).
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Concept Meaning
A template to create objects and A class is like a blueprint to
Class
create objects.
A constructor is a special method that runs automatically
Constructor
when you create an object.
A method is a block of code that performs a task when
Method
called.
Access Specifier They define who can use a class, method, or variable.
Static means the variable or method belongs to the class,
Static
not objects.
Final Final means the value cannot be changed after it is set.
Comment Comments are notes in code for humans, not run by Java.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Data Type in Java
A data type in Java defines the type of data a variable can hold, such as a number,
letter, or text. A data type in Java tells the computer what kind of data you are using —
like numbers, text, true/false, etc.
1. Primitive Data Types (Basic Types)
These are built-in basic data types in Java. They store simple values like numbers, characters, or
true/false.
List of Primitive Data Types:
Data Type Store Example
int Whole numbers int age = 20;
float Decimal numbers (small) float marks = 92.5f;
double Decimal numbers (large) double pi = 3.14;
char Single letter or character char grade = 'A';
boolean True or false boolean pass = true;
byte Very small numbers (–128 to 127) byte b = 100;
short Small numbers (–32,768 to 32,767) short s = 20000;
long LIKE,
Very SHARE,
big numbers COMMENT &phone
long SUBSCRIBE
= 9876543210L;
INTELLIGENCE LEARNING
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Non-Primitive Data Types (Reference Types)
These are not basic types. They store multiple values or custom data, and they
refer to objects.
Examples of Non-Primitive Types:
Data Type What It Is Example
String A group of characters (text) String name = "Amit";
Array A list of many values of the same type int[] numbers = {1, 2, 3};
Class A user-defined blueprint Car myCar = new Car();
Object An instance of a class new Student();
Primitive = Simple values (like numbers, letters, true/false)
Non-Primitive = Complex values (like sentences, lists, or your own types)
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
What is a Variable?
Variable stores a value (number, text, etc.). You can change the value later.
Every variable has a type (like int, float, String, etc.)
A variable is like a box in your computer’s memory where you can store data.
You can give this box a name and use it later.
Example:-
int age = 20; // age is a variable that stores number 20
String name = "Amit"; // name is a variable that stores the text "Amit"
What is a String?
A String is used to store text (like names, sentences, words). String stores a
group of characters (text). It is not a primitive type — it is a class. You can use
many functions with Strings (like length, to UpperCase, etc.)
Example:- String message = "Hello, Java!";
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
What is an Array?
An array is like a group of boxes kept together. It can store many values of the
same type (all numbers, or all names, etc.). Array holds multiple values. All
values must be of the same type. You can access a value using its index
(position number starting from 0).
Example:-
int[] numbers = {10, 20, 30}; // Array of 3 integers
String[] names = {"Amit", "Ravi", "Neha"}; // Array of 3 names
Operators in Java
Operators are special symbols used to perform actions on variables or values
like adding, comparing, or checking conditions.
Use +, -, * for math
Use ==, >, < for comparison
Use &&, ||, ! for logic
Use = and += for assignment
Use ++ and -- for increase/decrease
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Types of Operators in Java
1. Arithmetic Operators – Do math
Operator Use Example Result
+ Add 10 + 5 15
- Subtract 10 - 5 5
* Multiply 10 * 5 50
/ Divide 10 / 2 5
% Remainder 10 % 3 1
2. Logical Operators – Combine conditions
Operator Name Meaning Example Result
&& Logical AND True only if both are true 5 > 3 && 8 > 6 true
|| Logical OR True if at least one is true 5 > 3 || 8 > 6 true
Turns true to false, and false
! Logical NOT !(5 > 3) false
to true
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
3. Relational / Comparison Operators – Compare values
Operator Use Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 4 true
> Greater than 6>2 true
< Less than 3<5 true
>= Greater or equal 5 >= 5 true
<= Less or equal 4 <= 5 true
4. Unary Operators – Works on one value
Operator Use Example Meaning
++ Increment (+1) x++ x=x+1
-- Decrement (–1) x-- x=x-1
+ Positive value +x same as x
- Negative value -x makes x negative
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
5. Assignment Operators – Store values
Operator Use Example Meaning
= Assign value x = 10 x is 10
+= Add and assign x += 2 x=x+2
-= Subtract and assign x -= 2 x=x-2
*= Multiply and assign x *= 2 x=x*2
/= Divide and assign x /= 2 x=x/2
6. Bitwise Operators – Works on bits (advanced)
Operator Name Use
& AND Bitwise AND
| OR Bitwise OR
^ XOR Bitwise XOR
~ NOT Bitwise NOT
<< Left Shift Shifts bits to left
>> Right Shift Shifts bits to right
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Conditional Operator (Ternary Operator)
The conditional operator (also called the ternary operator) is a shortcut for
writing if-else
Syntax:-
condition ? expression_if_true : expression_if_false;
• If the condition is true, it runs the first expression.
• If the condition is false, it runs the second expression.
Control Flow Statements in Java
Control flow refers to the order in which statements are executed in a Java
program. Java provides different statements to control the flow of execution
like decisions, loops, and jumps.
Types of Control Flow Statements:
1. Decision-Making Statements
2. Looping Statements
3. Jump Statements
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
1. Decision-Making Statements
Used to make choices in your program based on certain conditions.
a. if Statement
Runs a block of code if the condition is true.
Syntax:-
if (x > 10) {
System.out.println("x is greater than 10");
}
b. if-else Statement
Runs one block if true, and another block if false.
Syntax:-
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
c. if-else if-else Statement
Allows multiple conditions.
Syntax:-
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 70) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
d. switch Statement
Used when you have many options based on one variable.
Syntax:-
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
2. Looping Statements
Used to repeat a block of code.
a. for Loop
Use when you know how many times you want to run.
Syntax:-
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
b. while Loop
Repeats code as long as the condition is true.
Syntax:-
c. do-while Loop
int i = 0; Same as while loop, but runs at least once
while (i < 5) { either condition is true or not.
System.out.println(i); Syntax:-
i++; int i = 0;
} do {
System.out.println(i);
i++;
} while (i < 5);
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
For Loop
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Jump Statements
Jump statements in Java are used to transfer control to another part of the
program. These are mainly used to alter the normal flow of execution,
especially in loops and switch cases.
Types of Jump Statements
1. break Statement
Used in loops (for, while, do-while) and switch.
Exits the loop or switch when a condition is met.
Example:-
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Loop ends when i is 3
}
System.out.println(i);
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
2. continue Statement
Skips the current iteration of the loop and goes to the next one.
Example:-
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip when i is 3
}
System.out.println(i);
}
3. return Statement
Used to exit from a method.
Can return a value if the method is not void.
Example:-
public int square(int x) {
return x * x;
}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Object-Oriented Programming (OOP)
Object-Oriented Programming is a programming paradigm based on the concept of
objects, which contain data (fields) and methods (functions).
1. Class
A class is a blueprint or template for creating objects. It defines the properties (variables)
and behaviors (methods).
EX:- class Car {
// properties
String color;
int speed;
// method
void drive() {
System.out.println("Car is driving");
}
}
2. Object
An object is an instance of a class. It is created using the new keyword.
EX:- Car myCar = new Car(); // Object of Car class
myCar.color = "Red";
myCar.drive();
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Overloading and Overriding
Overloading (Same Name, Different Work)
When one class has multiple methods with the same name, but they do
different things depending on the inputs.
• Happens in the same class.
• Method name is same, but number or type of parameters is different.
• Helps do similar tasks in different ways.
Overriding (Child Changes Parent's Method)
When a child class gives its own version of a method that it gets from the
parent class.
• Happens in inheritance (between parent and child class).
• Method name and parameters are exactly same.
• Child class changes the behavior of the method.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Encapsulation and Abstraction
Encapsulation
Encapsulation means hiding the data of a class and allowing access through methods
only like Putting data inside a capsule (class) and protecting it.
• Data is private, so it can’t be changed directly.
• We use getters and setters to access or change the data.
• Helps in data protection and security.
Abstraction
Abstraction means hiding the complex details and showing only important things to
the user.
Ex:- You drive a car using a steering wheel and pedals, but you don’t see the engine
working inside that’s abstraction.
• Focuses on what to do, not how to do it.
• Done using:
• Abstract classes (abstract class)
• Interfaces
• Helps to reduce complexity.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
What is a Constructor in Java?
A constructor is a special method in Java used to initialize objects. It is automatically
called when an object is created.
Properties of a Constructor:
1. The constructor must have the same name as the class.
2. It does not have a return type, not even void.
3. It is called automatically when a new object is created using the new keyword.
4. You can define multiple constructors in a class with different parameters
(constructor overloading).
Syntax:- class ClassName { Ex:- class Car {
// Constructor // Constructor
ClassName() { Car() {
// Code to initialize object System.out.println("Car object created!");
} }
} public static void main(String[] args) {
Car myCar = new Car(); // Constructor is
called here
}}
Output:- Car object created!
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Inheritance in Java?
Inheritance is a concept where one class can use (inherit) the properties and behaviors
(like methods and variables) of another class.
This helps to:
• Reuse code
• Avoid repetition
• Organize related classes
Terms Used in Inheritance Meaning
Class A blueprint or template to create objects.
Super Class / Parent Class The class whose features are inherited. It is the base class.
Sub Class / Child Class The class that inherits features from the super class.
extends keyword Used to create a sub class from a super class.
Type of Inheritance Description
1. Single Inheritance One sub class inherits from one super class.
2. Multilevel Inheritance A sub class inherits from another sub class.
3. Hierarchical Inheritance Multiple sub classes inherit from one super class.
Java does not support multiple inheritance with classes to avoid
4. Multiple Inheritance
LIKE, SHARE, COMMENT & SUBSCRIBE
confusion (but allows it through interfaces).
INTELLIGENCE LEARNING
Single Inheritance
One sub class inherits from one super class.
Example:-
class Animal {
void eat() {
System.out.println("This animal eats food.");
}}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}}
public class SingleInheritance {
void run() {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eats();
myDog.bark();
}}
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Multilevel Inheritance
A sub class inherits from another sub class.
Example:-
class Animal {
void eat() {
System.out.println("This animal eats food.");
}}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}}
class Horse extends Dog {
void run() {
System.out.println("The horse is running.");
}}
public class Multilevel_Inheritance {
public static void main(String[] args) {
Horse myHorse = new Horse();
myHorse.eat(); // from Animal
myHorse.bark(); // from Dog
myHorse.run(); // from Horse
}} LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
Hierarchical Inheritance
Multiple sub classes inherit from one super class.
Example:- class Animal {
void eat() {
System.out.println("This animal eats food.");
}}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}}
class horse extends Animal {
void run() {
System.out.println("The horse is running");
}}
public class Hierarchical_Inheritance {
public static void main(String[] args) {
horse myhorse = new horse();
myhorse.eat();
myhorse.run();
Dog myobj = new Dog();
myobj.eat();
LIKE, SHARE, COMMENT & SUBSCRIBE
myobj.bark();
INTELLIGENCE LEARNING
What is an Abstract Class?
An abstract class is a class that cannot be used to create objects directly. It is used as a
base class and may contain abstract methods (methods with no body).
Ex:- abstract class Animal {
abstract void sound(); // Abstract method (no body)
void sleep() { // Normal method
System.out.println("Sleeping...");
}}
Explantion:-
1. Animal एक abstract class है , मतऱब इसका object नह ं बना सकते।
2. sound() एक abstract method है — इसका काम बच्चे वाऱ class (जैसे Dog) को
बताना होता है ।
3. sleep() एक normal method है , जो सभी जानवरों के लऱए ready है और सीधा चऱ
सकता है ।
4. इसका मतऱब — सभी जानवर सो सकते हैं एक जैसे, ऱेककन आवाज़ सबकी अऱग
होगी, जो subclass बताएगा।
LIKE, SHARE, COMMENT & SUBSCRIBE
•sleep() सब जानवरों में common है — इसलऱए एक ह बार लऱखा।
•Dog class ने अऩनी आवाज़ (sound()) खुद define की।
INTELLIGENCE LEARNING
abstract class Animal {
// Abstract method (बिना body के)
abstract void sound();
// Normal method (body के साथ)
void sleep() {
System.out.println("जानवर सो रहा है ...");
}}
// Dog class जो Animal को extend कर रही है Explanation in More Easy Way
class Dog extends Animal { 1. Animal एक abstract class है — इसका
// abstract method को override कर रहे हैं object नह ं बनता।
void sound() { 2. sound() को हर जानवर अऩनी तरह से
System.out.println("कुत्ता भौंक रहा है "); बताएगा, इसलऱए वो abstract है ।
}} 3. sleep() सब जानवरों में common है —
// Main class इसलऱए एक ह बार लऱखा।
public class Main { 4. Dog class ने अऩनी आवाज़ (sound()) खुद
public static void main(String[] args) { define की।
Dog d = new Dog(); // Dog का object िनाया
Output:-
d.sound(); // Dog की आवाज़
d.sleep(); // Animal की sleep method कुत्ता भौंक रहा है
}} जानवर सो रहा है ...
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
What is Polymorphism?
Polymorphism means: "One name, many forms"
In simple words, the same method or function does different work based on the
situation.
Breakdown:
"Poly" = many
"Morph" = forms
So, Polymorphism = one thing in many forms
Example:
Think of a person:
At home: he is a son
At school: he is a student
At office: he is a worker
One person, but different roles = Polymorphism
Types of Polymorphism in Java:
Type Meaning
Same method name, but different parameters in
1. Compile-time (Method Overloading)
same class
LIKE, SHARE, COMMENT
2. Run-time (Method Overriding)
& SUBSCRIBE
Child class changes the method of parent class
INTELLIGENCE LEARNING
Package in Java
A package is like a folder that groups related classes and interfaces together.
Why use packages?
• To organize code properly.
• To avoid name conflicts.
• To reuse code easily.
• To provide access protection.
Types of Packages in Java:
Type Meaning
Packages that are already provided by Java (like tools we can
1. Built-in Packages
use).
2. User-defined Packages Packages that you create yourself to organize your own code.
Static Import in Java
Static import is used to import static members (like variables or methods) of a class directly, so
you don't need to write the class name every time.
LIKE, SHARE, COMMENT & SUBSCRIBE
INTELLIGENCE LEARNING
THANK YOU FOR WATCHING
DO LIKE SHARE COMMENT AND SUBSCRIBE
LIKE, SHARE, COMMENT & SUBSCRIBE