0% found this document useful (0 votes)
2 views

Detailed_Java_Concepts

The document provides an overview of Java programming concepts including class creation, inheritance, method overloading and overriding, abstract classes and interfaces, packages, error vs exception, collections, multithreading, Swing components, and JDBC. Each concept is explained with examples and syntax. It emphasizes the importance of these concepts for effective Java programming.

Uploaded by

kangirene9705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Detailed_Java_Concepts

The document provides an overview of Java programming concepts including class creation, inheritance, method overloading and overriding, abstract classes and interfaces, packages, error vs exception, collections, multithreading, Swing components, and JDBC. Each concept is explained with examples and syntax. It emphasizes the importance of these concepts for effective Java programming.

Uploaded by

kangirene9705
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

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

object. Methods define the behavior.

Example:

class MyClass {

int number; // Field or property

// Constructor

MyClass(int num) {

number = num;

// Method

void display() {

System.out.println("Number: " + number);

// Main method to create object and call method

public static void main(String[] args) {

MyClass obj = new MyClass(10); // Creating object

obj.display(); // Calling method

2. What is inheritance? How will a class inherit properties of another class?


Inheritance allows one class to acquire the properties and methods of another class. This promotes

code reusability.

Example:

class Animal {

void eat() {

System.out.println("This animal eats food.");

class Dog extends Animal { // Dog inherits from Animal

void bark() {

System.out.println("Dog barks");

public class TestInheritance {

public static void main(String[] args) {

Dog d = new Dog();

d.eat(); // Inherited method

d.bark(); // Child method

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.

Method Overriding: Redefining superclass method in subclass.


Overloading example:

class MathOperation {

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

Overriding example:

class Animal {

void sound() {

System.out.println("Animal makes sound");

class Dog extends Animal {

@Override

void sound() {

System.out.println("Dog barks");

4. Abstract classes and interfaces: Differences, syntax and example

Abstract class: Can have abstract and concrete methods.

Interface: Only abstract methods (before Java 8), from Java 8 onwards it can have default and static
methods.

Abstract class example:

abstract class Shape {

abstract void draw();

void color() {

System.out.println("Has color");

class Circle extends Shape {

void draw() {

System.out.println("Drawing Circle");

Interface example:

interface Drawable {

void draw();

class Rectangle implements Drawable {

public 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.

- Interface supports multiple inheritance, abstract class does not.

5. What are packages? Syntax for creating and using packages. Why is package necessary?

Packages are used to group related classes and interfaces together.

Creating a package:

package mypackage;

public class MyClass { }

Using a package:

import mypackage.MyClass;

Why packages?

- Avoids class name conflicts.

- Makes code easier to manage and maintain.

- Provides access protection.

Example:

package utilities;

public class Calculator { }

In another file:

import utilities.Calculator;

6. Difference between error and exception in Java. Other names

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:

- Error: Unchecked error (fatal)

- Exception: Checked or unchecked exceptions (recoverable)

Difference:

- Errors cannot be recovered from.

- Exceptions can be caught and handled using try-catch blocks.

7. What is a collection in Java? Syntax and diagram

Collections are frameworks that provide architecture to store and manipulate a group of objects.

Syntax:

List<String> list = new ArrayList<>();

Hierarchy:

Collection

List (ArrayList, LinkedList)

Set (HashSet, TreeSet)

Queue (PriorityQueue)

Map (Not part of Collection) HashMap, TreeMap

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

Thread: A unit of execution in a program.

Multithreading: Concurrent execution of two or more threads.


Ways to create thread:

1. Extending Thread class:

class MyThread extends Thread {

public void run() {

System.out.println("Thread running");

2. Implementing Runnable interface:

class MyRunnable implements Runnable {

public void run() {

System.out.println("Runnable running");

Use case without thread: Calculator app (sequential tasks)

Use case with multithreading: Server handling multiple clients, media player loading video while

playing audio.

9. Name a Swing container and a Swing component

Swing container: JFrame (Top-level window)

Swing component: JButton (for creating a clickable button)

Example:

JFrame frame = new JFrame();

JButton btn = new JButton("Click");

10. What is JDBC? Purpose, steps, effects of using or not using it


JDBC (Java Database Connectivity) allows Java applications to interact with databases.

Purpose:

- To execute SQL queries in Java

- Fetch, update, delete data in a database

Steps:

1. Load driver class

2. Establish connection

3. Create statement

4. Execute query

5. Process results

6. Close connection

Without JDBC: Application cannot interact with DB.

With JDBC: You can persist and retrieve data dynamically.

Example:

Connection con = DriverManager.getConnection(DB_URL, USER, PASS);

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM users");

You might also like