Detailed_Java_Concepts
Detailed_Java_Concepts
Syntax for creating a class, object, method, and constructor in Java with all these in 1
example
In Java, a class is a blueprint from which objects are created. A constructor is used to initialize the
Example:
class MyClass {
// Constructor
MyClass(int num) {
number = num;
// Method
void display() {
code reusability.
Example:
class Animal {
void eat() {
void bark() {
System.out.println("Dog barks");
3. What is method overloading and method overriding? Syntax and example of each
Method Overloading: Same method name but different parameters in the same class.
class MathOperation {
return a + b;
return a + b;
Overriding example:
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
Interface: Only abstract methods (before Java 8), from Java 8 onwards it can have default and static
methods.
void color() {
System.out.println("Has color");
void draw() {
System.out.println("Drawing Circle");
Interface example:
interface Drawable {
void draw();
System.out.println("Drawing Rectangle");
Differences:
- Abstract class can have method implementation; interface cannot (except default/static).
- A class can implement multiple interfaces but extend only one class.
5. What are packages? Syntax for creating and using packages. Why is package necessary?
Creating a package:
package mypackage;
Using a package:
import mypackage.MyClass;
Why packages?
Example:
package utilities;
In another file:
import utilities.Calculator;
Error: Indicates serious problems that a program should not try to catch (e.g., OutOfMemoryError).
Exception: Represents issues that a program might want to catch (e.g., IOException).
Other names:
Difference:
Collections are frameworks that provide architecture to store and manipulate a group of objects.
Syntax:
Hierarchy:
Collection
Queue (PriorityQueue)
Each interface/class provides different data handling mechanisms like ordering, sorting, uniqueness.
8. What is a thread? What is multi-threading? Ways to create thread with example use cases
System.out.println("Thread running");
System.out.println("Runnable running");
Use case with multithreading: Server handling multiple clients, media player loading video while
playing audio.
Example:
Purpose:
Steps:
2. Establish connection
3. Create statement
4. Execute query
5. Process results
6. Close connection
Example: