Difference between Structured Programming and Object Oriented Programming
Language:
Structured Programming Object-Oriented Programming
Follows a top-down approach Follows a bottom-up approach
Focuses on functions or procedures Focuses on objects and classes
No concept of inheritance, Supports OOP concepts like inheritance, encapsulation,
encapsulation, etc. polymorphism
Data is global and not secured Data is encapsulated within objects
Examples: C, Pascal Examples: Java, C++, Python
Three Features of Java Programming:
1. Platform Independent: Java code is compiled into bytecode which can run on any
system with the JVM.
2. Object-Oriented: Java supports all basic OOP principles such as inheritance,
polymorphism, abstraction, and encapsulation.
3. Robust: Java has strong memory management and exception handling features that make
it reliable.
Polymorphism: Ability of a variable, function, or object to take multiple forms. Example:
method overloading and overriding.
Encapsulation: Wrapping of data and methods into a single unit (class) and restricting access
using access modifiers.
Abstraction: Hiding internal details and showing only necessary features. Achieved using
abstract classes and interfaces.
Platform Independence: Java programs are compiled into bytecode which can be executed on
any OS having JVM.
Robust Programming: Java handles errors through exception handling and avoids memory
leaks through garbage collection.
Wrapper Class: Converts primitive data types into objects. Example: Integer, Double.
JVM (Java Virtual Machine): Executes Java bytecode and provides platform independence.
JRE (Java Runtime Environment): Provides libraries, JVM, and other components to run Java
applications.
JIT (Just In Time) Compiler: Improves performance by compiling bytecode into native code at
runtime.
Type Casting: Converting one data type into another.
Implicit Casting: Automatically done.
int a = 10;
double b = a; // implicit
Explicit Casting: Manually done using parentheses.
double a = 10.5;
int b = (int)a; // explicit
Why Java is not Completely Object-Oriented?
Because it supports primitive data types like int, float, char which are not objects.
Bytecode: Intermediate code generated after compilation of Java source code, executed by JVM.
Examples of Java Keywords:
static: Belongs to class, not instance.
class Example {
static int count = 0;
}
super: Refers to parent class.
super.methodName();
this: Refers to current object.
this.name = name;
Syntax of Defining Class and Object:
class Student {
int id;
String name;
}
Student s1 = new Student();
Instance Variable Hiding: When local variable name is same as instance variable.
class Example {
int x = 10;
void display() {
int x = 20;
System.out.println(x); // 20
}
}
Constructor: Special method invoked at the time of object creation.
Same name as class
No return type
Called automatically
Constructor Overloading: Multiple constructors with different parameters.
class Example {
Example() {}
Example(int a) {}
}
Inheritance: Process by which one class acquires properties of another.
Types:
1. Single
2. Multilevel
3. Hierarchical
4. Hybrid (Not supported directly in Java)
Syntax:
class A {
}
class B extends A {
}
final Keyword: Used to declare constants, prevent method overriding or inheritance.
final int x = 10;
Method Overloading vs Overriding:
Overloading Overriding
Same method name, different parameters Same method name and parameters
Compile time polymorphism Runtime polymorphism
In same class In inherited class
Initializing Base Class Constructor:
class A {
A(int a) {}
}
class B extends A {
B() {
super(10);
}
}
Interface: Abstract type to achieve abstraction and multiple inheritance.
interface A {
void show();
}
Java does not support multiple inheritance via class due to ambiguity.
Can achieve via multiple interfaces:
interface A {}
interface B {}
class C implements A, B {}
Package vs Import Statement:
Package: Organizes classes
package mypack;
Import: Imports classes from packages
import java.util.*;
Exception: An unwanted or unexpected event.
Checked: Checked at compile time (IOException)
Unchecked: Checked at runtime (ArithmeticException)
try-catch Block:
try {
// code
} catch(Exception e) {
// handle
}
Multiple catch: To handle multiple exceptions.
try {
} catch(IOException e) {
} catch(Exception e) {
}
throw and throws:
void check(int age) throws Exception {
if(age < 18) throw new Exception("Invalid");
}
Thread: Independent path of execution.
Thread Life Cycle: New -> Runnable -> Running -> Blocked -> Terminated
join(): Waits for a thread to die.
t1.join();
Thread Synchronization: Prevents data inconsistency when multiple threads access the same
resource.
synchronized void print() {}
Thread Priority: Thread execution order influenced by priority (1 to 10).
Stream Class: Handles input/output of data.
Classification:
Input Stream
Output Stream
Reader
Writer
File Handling Classes:
FileInputStream
FileOutputStream
FileReader
FileWriter
Java Program to Create and Read Text File:
import java.io.*;
class FileExample {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("example.txt");
fw.write("Hello File");
fw.close();
FileReader fr = new FileReader("example.txt");
int i;
while((i = fr.read()) != -1) {
System.out.print((char)i);
}
fr.close();
}
}