Unit - I (Introduction to Java & Java Programming)
Introduction to Java
Java is a high-level, object-oriented programming language developed by James Gosling at
Sun Microsystems in 1995. It is widely used for building web applications, mobile apps,
enterprise software, and more.
---
Object-Oriented Concepts in Java
Java follows four main Object-Oriented Programming (OOP) principles:
1. Encapsulation – Wrapping data (variables) and methods inside a single unit (class) to
protect it from outside interference. This helps in data hiding and improves security.
2. Inheritance – One class inherits properties and behavior from another, promoting code
reusability and reducing redundancy.
3. Polymorphism – The ability of a method or operator to perform different tasks based on
the object or data type. This includes method overloading and method overriding.
4. Abstraction – Hides implementation details and only shows necessary features to the
user, making programs simpler and easier to maintain.
---
History and Features of Java
Developed by: James Gosling at Sun Microsystems in 1995.
Key Features:
- Platform-independent (Runs on JVM → "Write Once, Run Anywhere")
- Secure and simple with built-in security features.
- Object-oriented supporting OOP principles.
- Multithreading support, allowing multiple tasks to run simultaneously.
---
JDK, JRE, and JVM
- JDK (Java Development Kit) → Includes tools to write, compile, and run Java programs.
- JRE (Java Runtime Environment) → Provides libraries and environment to execute Java
programs.
- JVM (Java Virtual Machine) → Converts Java bytecode into machine code that the computer
understands. JVM makes Java platform-independent.
---
Classes, Objects, and Methods
Class: A blueprint or template for creating objects. It defines attributes (variables) and
behaviors (methods).
Object: A real-world entity that has state (attributes) and behavior (methods).
Method: A function inside a class that performs an action.
Example:
```java
class Car {
String model;
void displayModel() {
[Link]("Car Model: " + model);
}
}
public class Main {
public static void main(String args[]) {
Car myCar = new Car();
[Link] = "Tesla";
[Link]();
}
}
```
---
Instance Methods
Instance methods belong to objects and require an object to be called.
```java
class Person {
String name;
void greet() {
[Link]("Hello, my name is " + name);
}
}
public class Main {
public static void main(String args[]) {
Person p = new Person();
[Link] = "Armaan";
[Link](); // Calling the instance method
}
}
```
---
Constructors in Java
A constructor is a special method that is called when an object is created. It has the same
name as the class and no return type.
Types of Constructors:
1. Default Constructor – No parameters, assigns default values.
2. Parameterized Constructor – Accepts arguments to initialize objects.
Example:
```java
class Student {
String name;
// Default Constructor
Student() {
[Link]("Default Constructor Called");
}
// Parameterized Constructor
Student(String studentName) {
name = studentName;
}
void display() {
[Link]("Student Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Default Constructor
Student s2 = new Student("Armaan"); // Parameterized Constructor
[Link]();
}
}
```
---
Constructor Overloading & Method Overloading
- Constructor Overloading – Multiple constructors in the same class with different
parameters.
- Method Overloading – Multiple methods in the same class with the same name but
different parameters.
---
Access Qualifiers
Access qualifiers control the visibility of classes, variables, and methods.
```java
class Example {
public int a = 10;
private int b = 20;
public void show() {
[Link]("Public Variable: " + a);
[Link]("Private Variable: " + b);
}
}
```
---
Arrays in Java
An array is a collection of elements of the same type stored in contiguous memory locations.
```java
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
}
```
---
Unit - II (Strings, Inheritance, Packages & Interfaces)
String Classes and Methods
- String (Immutable Class) → Cannot be modified after creation.
```java
String str = "Hello";
str = [Link](" Java"); // Creates a new string "Hello Java"
```
- StringBuffer (Mutable Class) → Can be modified after creation.
```java
StringBuffer sb = new StringBuffer("Hello");
[Link](" Java"); // Modifies existing string
[Link](sb);
```
- StringTokenizer → Splits a string into multiple tokens (words).
```java
import [Link];
StringTokenizer st = new StringTokenizer("Java is fun");
while ([Link]()) {
[Link]([Link]());
}
```
---
Inheritance in Java
Inheritance allows a class to acquire the properties of another class.
Types of Inheritance:
1. Single Inheritance – One class inherits from another.
2. Multilevel Inheritance – A → B → C (Chain of inheritance).
3. Hierarchical Inheritance – One class is inherited by multiple subclasses.
---
Super Keyword
The `super` keyword is used to call the parent class constructor or method in the child class.
---
Method Overriding
A subclass provides a new implementation of a method already present in the parent class.
---
Abstract Classes
An abstract class cannot be instantiated and must be extended by a subclass. It can contain
both abstract (without body) and concrete (with body) methods.
---
Final Keyword
The `final` keyword is used to prevent modification:
- `final variable` → Value cannot be changed.
- `final method` → Cannot be overridden.
- `final class` → Cannot be inherited.
---
Packages and Interfaces
- Packages: Collections of related classes and interfaces. Used to organize code.
```java
import [Link]; // Importing a package
```
- Interfaces: Define rules for classes to follow. A class must implement all methods of an
interface.
```java
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
[Link]("Bark");
}
}
```