0% found this document useful (0 votes)
7 views9 pages

Java Basics Workbook2

The document is a comprehensive Java workbook designed for teaching and self-study, covering the basics of Java, object-oriented programming concepts, and practical coding examples. It includes detailed theoretical explanations, sample programs, and interview questions with answers. Key topics include Java's features, program structure, data types, control statements, OOP principles, constructors, exception handling, and common interview queries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Java Basics Workbook2

The document is a comprehensive Java workbook designed for teaching and self-study, covering the basics of Java, object-oriented programming concepts, and practical coding examples. It includes detailed theoretical explanations, sample programs, and interview questions with answers. Key topics include Java's features, program structure, data types, control statements, OOP principles, constructors, exception handling, and common interview queries.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Got it 👍

You want a proper, theory-heavy Java workbook — the kind that explains why things
exist, not just how to write code. Below is a structured, classroom-ready workbook with
deep theoretical explanations, diagrams-in-words, sample programs, and interview
questions with clear answers.

You can use this for students, self-study, exams, or interviews.

📘 JAVA WORKBOOK
Basics of Java & Object-Oriented Programming (OOPS)
(With Detailed Theoretical Explanation, Programs & Interview Q&A)

MODULE 1: INTRODUCTION TO JAVA


1.1 What is Java?

Java is a high-level, object-oriented, general-purpose programming language designed to


be platform independent. It follows the principle:

Write Once, Run Anywhere (WORA)

Java programs are first compiled into bytecode, which is then executed by the Java Virtual
Machine (JVM).

1.2 History of Java

 Developed by James Gosling at Sun Microsystems (1995)


 Originally named Oak
 Currently maintained by Oracle Corporation

1.3 Features of Java (Detailed Explanation)

1️⃣ Platform Independent

Java code is compiled into bytecode, not machine code.


Bytecode can run on any OS that has JVM.
C/C++ → OS Dependent
Java → OS Independent

2️⃣ Object-Oriented

Everything in Java revolves around objects and classes, which helps:

 Code reusability
 Better maintainability
 Real-world modeling

3️⃣ Secure

 No explicit pointers
 Bytecode verification
 Security Manager

4️⃣ Robust

 Strong memory management


 Automatic Garbage Collection
 Exception Handling

5️⃣ Multithreaded

Java supports executing multiple tasks at the same time.

MODULE 2: JAVA PROGRAM STRUCTURE


2.1 Basic Java Program
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World");
}
}

Explanation (Line by Line)

 class → Blueprint of object


 public → Accessible everywhere
 static → No object needed to call main
 void → No return value
 main() → JVM starts execution here
 String[] args → Command-line arguments

MODULE 3: DATA TYPES & VARIABLES


3.1 Variables

A variable is a container that holds data values.

int number = 10;

3.2 Data Types (Theory)

Primitive Data Types

Type Size Description


int 4 bytes Stores integers
float 4 bytes Decimal values
double 8 bytes High precision decimals
char 2 bytes Single character
boolean 1 bit true / false

Non-Primitive Data Types

 String
 Arrays
 Classes
 Interfaces

MODULE 4: CONTROL STATEMENTS


4.1 Decision Making
if (marks >= 40) {
[Link]("Pass");
} else {
[Link]("Fail");
}

Theory:
Used to control execution flow based on conditions.
4.2 Looping Statements
for(int i = 1; i <= 5; i++) {
[Link](i);
}

Theory:
Loops help execute code repeatedly, reducing redundancy.

MODULE 5: INTRODUCTION TO OOPS


5.1 What is OOPS?

OOPS is a programming paradigm based on objects, which combine:

 Data (variables)
 Behavior (methods)

5.2 Why OOPS?

 Real-world mapping
 Code reusability
 Easy debugging
 Scalability

MODULE 6: CORE OOPS CONCEPTS

🔹 6.1 Class & Object


Theory

 Class → Blueprint/template
 Object → Real instance of class

Example:
Class → Car
Object → BMW, Audi
Program
class Car {
String brand;

void drive() {
[Link]("Car is driving");
}
}

class Main {
public static void main(String[] args) {
Car c = new Car();
[Link] = "BMW";
[Link]();
}
}

🔹 6.2 Encapsulation
Theory

Encapsulation means wrapping data and methods into a single unit and protecting data
from unauthorized access.

✔ Achieved using:

 private variables
 public getters and setters

Advantages

 Data security
 Controlled access
 Better maintainability

Program
class Employee {
private int salary;

public void setSalary(int s) {


salary = s;
}

public int getSalary() {


return salary;
}
}

🔹 6.3 Inheritance
Theory

Inheritance allows one class (child) to acquire properties of another (parent).

"IS-A relationship"

Types

 Single
 Multilevel
 Hierarchical

Program
class Animal {
void eat() {
[Link]("Eating");
}
}

class Dog extends Animal {


void bark() {
[Link]("Barking");
}
}

🔹 6.4 Polymorphism
Theory

Polymorphism means one name, many forms.

Compile-Time Polymorphism (Overloading)


class Calculator {
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}
}

Runtime Polymorphism (Overriding)


class Parent {
void show() {
[Link]("Parent class");
}
}

class Child extends Parent {


void show() {
[Link]("Child class");
}
}

🔹 6.5 Abstraction
Theory

Abstraction means hiding implementation details and showing only essential features.

Using Abstract Class


abstract class Vehicle {
abstract void run();
}

Using Interface
interface Payment {
void pay();
}

MODULE 7: CONSTRUCTORS
Theory

A constructor:

 Has same name as class


 Automatically invoked
 Initializes object

class Demo {
Demo() {
[Link]("Object created");
}
}

MODULE 8: EXCEPTION HANDLING


Theory

Exception handling prevents abnormal program termination.

try {
int a = 10 / 0;
} catch(Exception e) {
[Link]("Error handled");
}

MODULE 9: INTERVIEW QUESTIONS & ANSWERS


Q1. What is JVM?

A: JVM executes bytecode and provides platform independence.

Q2. Difference between JDK, JRE, JVM?

 JVM → Executes bytecode


 JRE → JVM + Libraries
 JDK → JRE + Compiler

Q3. Why Java does not support pointers?

A: For security and memory safety.

Q4. What is this keyword?

A: Refers to current class object.

Q5. What is Garbage Collection?

A: Automatic memory management in Java.

Q6. Why multiple inheritance not supported in Java?

A: To avoid ambiguity (Diamond Problem).


Q7. Can abstract class have constructor?

A: Yes.

Q8. Difference between == and .equals()?

 == → Reference comparison
 .equals() → Content comparison

You might also like