Core Java Concepts - Detailed Notes
1. Java Features
Java is a powerful, object-oriented programming language known for its platform
independence and security. Key features include:
- Simple: Easy to learn and use.
- Object-Oriented: Based on OOP principles (encapsulation, inheritance, etc.).
- Platform Independent: "Write once, run anywhere" due to JVM.
- Secure: Built-in security features like bytecode verification.
- Robust: Strong memory management and exception handling.
- Multithreaded: Supports concurrent execution using threads.
- High Performance: Through Just-In-Time (JIT) compilers.
- Distributed: Supports remote method invocation (RMI).
- Dynamic: Supports dynamic memory allocation and reflection.
2. OOP Principles
- Encapsulation: Wrapping data and code into a single unit (class), using access modifiers.
- Inheritance: Mechanism for a class to inherit properties and behaviors from another class.
- Polymorphism: Ability to take many forms; achieved via method overloading and
overriding.
- Abstraction: Hiding implementation details and showing only essential features using
abstract classes or interfaces.
3. Difference Between C and Java
| Feature |C | Java |
|---------------|----------------------------|-----------------------------|
| Paradigm | Procedural | Object-Oriented |
| Memory | Manual (malloc, free) | Automatic (Garbage Collector) |
| Platform | Platform dependent | Platform independent (JVM) |
| Pointers | Supports pointers | No pointer support |
| Compilation | Compiled to machine code | Compiled to bytecode |
| Inheritance | Not supported | Supported |
4. Inheritance and Its Types with Example
Inheritance allows a class to acquire properties of another class using `extends`.
Types:
1. Single Inheritance:
class Animal {
void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking"); }
}
2. Multilevel Inheritance:
class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }
class Puppy extends Dog { void weep() {} }
3. Hierarchical Inheritance:
class Animal { void eat() {} }
class Dog extends Animal { void bark() {} }
class Cat extends Animal { void meow() {} }
Note: Java does not support Multiple Inheritance using classes, but it is possible with
interfaces.
5. Interface with Example
An interface is a reference type that contains only abstract methods (before Java 8).
interface Printable {
void print();
}
class Document implements Printable {
public void print() {
System.out.println("Printing document");
}
}
6. Package with Example
A package is a namespace that organizes classes and interfaces.
package mypackage;
public class MyClass {
public void display() {
System.out.println("Package example");
}
}
To use it:
import mypackage.MyClass;
class Test {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}
7. Life Cycle of Thread
1. New: Thread is created using `new Thread()`.
2. Runnable: Thread is ready to run using `start()`.
3. Running: Thread is running.
4. Blocked/Waiting: Thread is waiting for a resource or signal.
5. Terminated: Thread completes or is stopped.
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
8. Life Cycle of Applet
1. init(): Called once, for initialization.
2. start(): Called after init and every time applet becomes visible.
3. paint(): Called to draw on screen.
4. stop(): Called when applet is not visible.
5. destroy(): Called before the applet is destroyed.
public class MyApplet extends Applet {
public void init() { }
public void start() { }
public void paint(Graphics g) { }
public void stop() { }
public void destroy() { }
}
9. Runnable Interface
It represents a task that can be executed by a thread.
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running via Runnable");
}
}
class Test {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
}
}
10. Exception Handling Keywords
- try: Code that may cause exception.
- catch: Handles the exception.
- finally: Always executes (used to clean up).
- throw: Manually throw an exception.
- throws: Declares exceptions that a method can throw.
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e);
} finally {
System.out.println("Always executed");
}
11. Overloading and Overriding
Overloading: Same method name, different parameters (compile-time).
void display(int a) {}
void display(String b) {}
Overriding: Subclass provides a new implementation for a superclass method (runtime).
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
12. Constructors and Types
A constructor is a special method used to initialize objects.
Types:
- Default Constructor: No parameters.
- Parameterized Constructor: With parameters.
- Copy Constructor: Used to copy values.
class Student {
String name;
Student() {
name = "Default";
}
Student(String n) {
name = n;
}
Student(Student s) {
name = s.name;
}
}