[go: up one dir, main page]

0% found this document useful (0 votes)
54 views3 pages

UNIT 3-2mark

Inheritance in Java allows classes to inherit properties and behaviors from superclasses. A subclass inherits from one superclass using the extends keyword. Subclasses can override methods from the superclass. The super keyword is used to access members of the superclass. Abstract classes contain abstract methods and can be extended, while interfaces define contracts using abstract methods and can be implemented by multiple classes. Exceptions represent errors and can be handled using try-catch blocks, with finally blocks for cleanup. Checked exceptions must be caught or declared, while unchecked exceptions do not require handling.

Uploaded by

poomanim
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)
54 views3 pages

UNIT 3-2mark

Inheritance in Java allows classes to inherit properties and behaviors from superclasses. A subclass inherits from one superclass using the extends keyword. Subclasses can override methods from the superclass. The super keyword is used to access members of the superclass. Abstract classes contain abstract methods and can be extended, while interfaces define contracts using abstract methods and can be implemented by multiple classes. Exceptions represent errors and can be handled using try-catch blocks, with finally blocks for cleanup. Checked exceptions must be caught or declared, while unchecked exceptions do not require handling.

Uploaded by

poomanim
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/ 3

UNIT 3- 2 Mark Question and answers

1. What is inheritance in Java?


Inheritance in Java is a mechanism that allows one class (subclass or derived class) to inherit the
properties and behaviors (fields and methods) of another class (superclass or base class). It
promotes code reuse and supports the concept of "is-a" relationship between classes.

2. How is inheritance implemented in Java?


Inheritance in Java is implemented using the extends keyword. A subclass can extend a
superclass by specifying extends followed by the superclass name in the class declaration.

3. What is a superclass and a subclass in Java inheritance?


In Java, a superclass (or base class) is the class whose properties and behaviors are inherited by
another class. The class that inherits from a superclass is called a subclass (or derived class).

4. Can a subclass inherit from multiple superclasses in Java?


No, Java does not support multiple inheritance for classes. A subclass can inherit from only one
superclass. However, it can implement multiple interfaces, achieving a form of multiple
inheritance through interface implementation.

5. What is method overriding in Java inheritance?


Method overriding is a feature in Java inheritance where a subclass provides a specific
implementation of a method that is already defined in its superclass. The overridden method in
the subclass should have the same name, return type, and parameters as the method in the
superclass.

6. What is the super keyword used for in Java inheritance?


The super keyword in Java is used to call methods or access members (fields or methods) of the
superclass from within a subclass. It is often used to differentiate between the members of the
subclass and those of the superclass with the same name.

7. What is the purpose of the final keyword in Java inheritance?


In Java, the final keyword can be used to prevent a class from being extended (subclassed).
When applied to a method, it prevents that method from being overridden in subclasses. When
applied to a field, it makes the field a constant that cannot be modified.

8. What is an abstract class in Java?


An abstract class in Java is a class that cannot be instantiated on its own and is typically used as a
base class for other classes. It can have both abstract (unimplemented) methods and concrete
(implemented) methods.

9. How do you declare an abstract class in Java?


To declare an abstract class in Java, you use the abstract keyword before the class keyword in the
class declaration.
For example: abstract class MyAbstractClass.

10. What is a package in Java?


A package in Java is a way to organize and group related classes and interfaces into a single unit.
It helps in avoiding naming conflicts and provides a hierarchical structure to organize classes.
11. How do you declare a package in a Java source file?
To declare a package in a Java source file, you use the package statement at the beginning of the
file.
For example: package com.mycompany.mypackage;

12. What is the purpose of the java.lang package in Java?


The java.lang package is a special package in Java that is automatically imported into every Java
program. It contains fundamental classes and data types such as String, Object, and various
utility classes, making them readily available without the need for explicit imports.

13. What is an interface in Java?


An interface in Java is a collection of abstract methods (methods without a body) that define a
contract for classes that implement the interface. Classes that implement an interface are required
to provide concrete implementations for all the methods declared in the interface.

14. How do you declare an interface in Java?


To declare an interface in Java, you use the interface keyword followed by the interface's name
and a list of abstract method declarations enclosed in curly braces.
For example:
public interface MyInterface {
void myMethod();
}

15. Why use interfaces in Java?


Interfaces are used in Java for several reasons, including:
 Defining a contract .
 Achieving multiple inheritance.
 Encouraging code reusability.
 Enforcing design patterns.

16. What is an exception in Java?


An exception in Java is an abnormal condition or error that occurs during the execution of a
program. It disrupts the normal flow of a program and can be caused by various factors such as
invalid inputs, resource unavailability, or programming errors.

17. What is the purpose of exception handling in Java?


Exception handling in Java is used to gracefully manage and recover from unexpected errors or
exceptional situations that can occur during program execution. It helps prevent program crashes
and allows for controlled error reporting.

18. What is the difference between checked and unchecked exceptions in Java?
Checked exceptions are exceptions that must be either caught and handled using try-catch blocks
or declared using the throws keyword in the method signature. Unchecked exceptions, also
known as runtime exceptions, do not need to be explicitly caught or declared.
19. What is the try-catch block used for in Java?
The try-catch block in Java is used for handling exceptions. Code that may throw exceptions is
enclosed within a try block, and the potential exceptions are caught and handled in one or more
catch blocks.

20. What is the purpose of the finally block in Java exception handling?
The finally block is used in Java exception handling to specify code that should be executed
regardless of whether an exception is thrown or not. It is typically used to release resources or
perform cleanup operations.

21. How can you throw a custom exception in Java?


You can throw a custom exception in Java by creating a new class that extends the Exception or
a subclass of Exception (e.g., RuntimeException). Then, you can use the throw keyword to throw
an instance of your custom exception.

22. What is the purpose of the throws keyword in Java method signatures?
The throws keyword in Java method signatures is used to declare that a method might throw one
or more exceptions. It informs the caller of the method about the potential exceptions they need
to handle or propagate.

You might also like