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

Java_Concepts_Detailed_Answers

The document covers key Java concepts including inheritance, constructors, exception handling, collections, JavaFX controls, access modifiers, method overloading and overriding, and data types. It explains inheritance types with examples, constructor functionalities, exception hierarchy, and how to handle exceptions using try-catch blocks. Additionally, it discusses Java Collections Framework and JavaFX for GUI development, along with a detailed overview of access modifiers and data types.

Uploaded by

mahanoobhume
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)
24 views

Java_Concepts_Detailed_Answers

The document covers key Java concepts including inheritance, constructors, exception handling, collections, JavaFX controls, access modifiers, method overloading and overriding, and data types. It explains inheritance types with examples, constructor functionalities, exception hierarchy, and how to handle exceptions using try-catch blocks. Additionally, it discusses Java Collections Framework and JavaFX for GUI development, along with a detailed overview of access modifiers and data types.

Uploaded by

mahanoobhume
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
You are on page 1/ 5

Java Concepts – 7 Marks Detailed Answers

1. Explain Inheritance with its types and give suitable example


Inheritance is a core concept of Object-Oriented Programming (OOP) in Java where one
class acquires the properties (fields) and functionalities (methods) of another class. It helps
in code reusability and establishes a relationship between different classes.

Types of Inheritance:
1. Single Inheritance – One subclass inherits from one superclass.
2. Multilevel Inheritance – A class inherits from a class which in turn inherits from another
class.
3. Hierarchical Inheritance – Multiple classes inherit from one superclass.
4. Multiple Inheritance (via interfaces) – A class can implement multiple interfaces.
5. Hybrid Inheritance – A combination of multiple types, supported via interfaces.

Example:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}

class Dog extends Animal {


void bark() {
System.out.println("The dog barks.");
}
}

public class Test {


public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}

2. Define Constructor. How objects are constructed?


A constructor in Java is a special method used to initialize objects. It has the same name as
the class and does not have a return type. It is called automatically when an object is created
using the `new` keyword.
Constructors can be:
1. Default Constructor – Created by Java if no constructor is defined.
2. Parameterized Constructor – Accepts parameters to initialize the object.

Example:
class Student {
String name;

Student(String name) {
this.name = name;
}
}

Student s = new Student("John"); // Constructor called during object creation.

3. Explain Constructor Overloading with an example


Constructor Overloading is a feature in Java where a class can have more than one
constructor with different parameter lists. It allows the creation of objects in different ways.

Example:
class Student {
String name;
int age;

Student(String name) {
this.name = name;
}

Student(String name, int age) {


this.name = name;
this.age = age;
}
}

Student s1 = new Student("John");


Student s2 = new Student("Alice", 20);

4. What is an Exception? Explain the exception hierarchy


An exception is an unwanted or unexpected event that occurs during the execution of a
program, disrupting the normal flow of instructions.

Exception Hierarchy:
- Throwable (Root class)
- Error (serious issues like OutOfMemoryError)
- Exception (for exceptional conditions)
- Checked Exceptions (e.g., IOException)
- Unchecked Exceptions (e.g., ArithmeticException, NullPointerException)

Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
}

5. Explain how to throw, catch, and handle exceptions


Java uses try-catch blocks to handle exceptions. You can also throw exceptions using the
`throw` keyword.

Syntax:
try {
// risky code
} catch (ExceptionType name) {
// handling code
} finally {
// cleanup code (optional)
}

Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Cleanup actions.");
}

You can also throw an exception:


throw new ArithmeticException("Cannot divide by zero");

6. Short note on Java Collection


Java Collections Framework provides architecture to store and manipulate groups of data as
a single unit.
Important Interfaces:
- List – Ordered, allows duplicates. e.g., ArrayList, LinkedList
- Set – Unordered, no duplicates. e.g., HashSet, TreeSet
- Map – Key-value pairs. e.g., HashMap, TreeMap

Collections are in the `java.util` package and support operations like searching, sorting,
insertion, manipulation, and deletion.

7. Write short note on JavaFX Controls


JavaFX is a software platform for creating and delivering desktop applications with a
modern UI.

JavaFX Controls include:


- Label – Displays text.
- Button – Triggers actions.
- TextField – Accepts user input.
- RadioButton, CheckBox – Options selection.
- ComboBox – Dropdown menu.

These controls are part of `javafx.scene.control` and are added to scenes to build GUIs.

8. Explain all Access Modifiers and their visibility


Java has four access modifiers:

1. public – Accessible from anywhere.


2. protected – Accessible within the same package and subclasses in other packages.
3. default (no modifier) – Accessible only within the same package.
4. private – Accessible only within the class.

Example:
public class Test {
private int a; // Only inside Test
int b; // Default – package only
protected int c; // Package + subclass
public int d; // Everywhere
}

9. Explain Overloading and Overriding with example


Overloading: Same method name with different parameters in the same class.

Overriding: A subclass provides a specific implementation of a method already present in


the parent class.

Example of Overloading:
class Calculator {
int add(int a, int b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}

Example of Overriding:
class Animal {
void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {


void sound() { System.out.println("Bark"); }
}

10. Data types in detail with example


Java has two data type categories:

1. Primitive: byte, short, int, long, float, double, char, boolean


2. Reference: Arrays, Objects

Examples:
int age = 25;
float salary = 50000.75f;
char grade = 'A';
boolean passed = true;
String name = "John"; // Reference type

You might also like