Mastering Java: From Basics to Advanced OOP Principles
Java Programming Fundamentals and Features
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now owned by Oracle) in 1995. Java follows the principle "Write Once, Run Anywhere (WORA)", meaning a Java program can run on any system that has a Java Virtual Machine (JVM).
Key Features of Java
- Simple: Easy to learn and use.
- Object-Oriented: Based on the concept of classes and objects.
- Platform Independent: Runs on any operating system using the JVM.
- Portable: Programs can be moved easily from one platform to another.
- Secure: Provides security through bytecode verification and the JVM.
- Robust: Features strong memory management and exception handling.
- Multithreaded: Supports the execution of multiple threads simultaneously.
- Distributed: Supports network-based applications.
- High Performance: Utilizes the Just-In-Time (JIT) compiler.
- Dynamic: Supports dynamic loading of classes.
Applications of Java
- Desktop Applications
- Web Applications
- Mobile Applications (Android)
- Enterprise Applications
- Banking Systems
- E-commerce Websites
- Scientific Applications
- Cloud Computing
- Embedded Systems
- Game Development
Difference Between Java and C++
Understanding JDK, JRE, and JVM
JDK (Java Development Kit) is a software package used to develop Java applications. It contains everything required to write, compile, debug, and run Java programs.
- JDK = JRE + Development Tools
- Development tools include: Java Compiler (
javac), Debugger, and Documentation tools.
JRE (Java Runtime Environment) provides the environment required to run Java applications. It contains the JVM and Java libraries but does not contain development tools like the compiler.
- JRE = JVM + Java Libraries
JVM (Java Virtual Machine) is a virtual machine that executes Java bytecode. It converts bytecode into machine code so that Java programs can run on any operating system. JVM is responsible for memory management, security, and platform independence.
Relationship Between JDK, JRE, and JVM
- JDK
- JRE
- JVM
- Java Libraries
- Development Tools (
javac, Debugger, etc.)
- JRE
Compilation and Execution Process
- Step 1: Write the Program – Write the source code in a file with the
.javaextension (e.g.,Hello.java). - Step 2: Compile the Program – The Java compiler (
javac) converts the source code into bytecode (.classfile).javac Hello.java - Step 3: Execute the Program – The JVM loads the bytecode, converts it into machine code, and executes it.
java Hello
Java Operators and Type Casting
Operators are special symbols that perform operations on operands and produce a result.
- Arithmetic Operators:
+,-,*,/,% - Relational Operators:
==,!=,>,<,>=,<= - Logical Operators:
&&(AND),||(OR),!(NOT) - Increment and Decrement:
++,-- - Assignment Operators:
=,+=,-=,*=,/=,%= - Conditional (Ternary) Operator:
?: - Bitwise Operators:
&,|,^,~,<<,>> - Special Operators:
new,instanceof
Type Casting in Java
Type casting is the process of converting one data type into another.
- Implicit Type Casting (Widening): Conversion from a smaller to a larger data type. Done automatically.
int a = 10; double b = a; // 10.0 - Explicit Type Casting (Narrowing): Conversion from a larger to a smaller data type. Done manually.
double a = 10.75; int b = (int)a; // 10
Object-Oriented Programming Principles
Object-Oriented Programming (OOP) is a paradigm that organizes programs using objects and classes. It promotes reusability and modularity.
Core Principles of OOP
- Class: A blueprint for creating objects.
- Object: An instance of a class.
- Encapsulation: Wrapping data and methods into a single unit.
- Abstraction: Hiding implementation details and showing only essential features.
- Inheritance: Acquiring properties and methods of another class.
- Polymorphism: One interface, many forms.
Example: Class and Object Implementation
class Student {
String name = "Rahul";
int age = 20;
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
public static void main(String args[]) {
Student s = new Student(); // Creating an object
s.display(); // Calling method
}
}Inheritance Types and Implementation
Inheritance allows a Child (Sub) Class to acquire properties of a Parent (Super) Class using the extends keyword.
Types of Inheritance
- Single Inheritance: One child inherits from one parent.
- Multilevel Inheritance: A class inherits from a class that is already inherited.
- Hierarchical Inheritance: Multiple child classes inherit from the same parent.
- Multiple Inheritance: Not supported through classes in Java to avoid ambiguity; achieved via Interfaces.
- Hybrid Inheritance: A combination of two or more types; implemented using interfaces.
Single Inheritance Example
class Animal {
void eat() { System.out.println("Animal is eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog is barking"); }
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.bark();
}
}Constructors and Access Specifiers
A Constructor is a special member function called automatically when an object is created to initialize it. It must have the same name as the class and no return type.
- Default Constructor: Takes no arguments.
- Parameterized Constructor: Accepts arguments to initialize fields.
Access Specifiers
- public: Accessible from anywhere.
- private: Accessible only within the same class.
- protected: Accessible within the same package and by subclasses.
- Default: Accessible only within the same package.
Method Overloading and Overriding
Method Overloading (Compile-time Polymorphism) involves multiple methods with the same name but different parameters within the same class.
Method Overriding (Run-time Polymorphism) involves redefining a parent class method in a child class with the same name, parameters, and return type.
Common Java Programming Examples
Area of a Rectangle
class Rectangle {
public static void main(String args[]) {
int length = 10, breadth = 5;
System.out.println("Area = " + (length * breadth));
}
}Check Odd or Even
class OddEven {
public static void main(String args[]) {
int n = 8;
if (n % 2 == 0) System.out.println("Even");
else System.out.println("Odd");
}
}Factorial of a Number
class Factorial {
public static void main(String args[]) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
System.out.println("Factorial = " + fact);
}
}Java Keywords and String Handling
Strings in Java
Strings are immutable objects of the String class. Common methods include length(), toUpperCase(), concat(), and equals().
Essential Keywords
- final: Used to make variables constant, prevent method overriding, and prevent inheritance.
- static: Belongs to the class rather than objects; can be accessed without an instance.
- this: Refers to the current object instance.
- super: Refers to the immediate parent class instance.
Java Packages and Wrapper Classes
A Package is a collection of related classes. Built-in packages include java.lang, java.util, and java.io. User-defined packages are created using the package keyword.
Wrapper Classes
Wrapper classes convert primitive data types into objects. This includes Integer for int, Double for double, etc.
- Boxing: Converting a primitive to a wrapper object.
- Unboxing: Converting a wrapper object back to a primitive.
English with a size of 187.84 KB