0% found this document useful (0 votes)
22 views4 pages

Inheritance UNIT 2 CHATGPT

Uploaded by

kaminiganesan13
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)
22 views4 pages

Inheritance UNIT 2 CHATGPT

Uploaded by

kaminiganesan13
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/ 4

Inheritance

Inheritance is a mechanism where one class acquires the properties and behaviors of another
class, promoting code reuse and hierarchy.

Basic Concepts:

 Parent Class (Super Class): The class whose properties and methods are inherited.
 Child Class (Sub Class): The class that inherits properties and methods.

Types of Inheritance:

1. Single Inheritance: A subclass inherits from a single superclass.


2. Multilevel Inheritance: A class inherits from another subclass, forming a chain.
3. Hierarchical Inheritance: Multiple classes inherit from a single superclass.
4. Hybrid Inheritance: A combination of different types of inheritance (not directly
supported in Java due to ambiguity; can be achieved using interfaces).
5. Multiple Inheritance: Achieved in Java through interfaces, as a class cannot inherit from
multiple classes.

Member Access Rules:

 Public: Accessible from anywhere.


 Protected: Accessible within the same package and by subclasses.
 Default (Package-private): Accessible only within the same package.
 Private: Accessible only within the same class.

Keywords: this and super:

 this: Refers to the current instance of the class.


o Example: this.field = value;
 super: Refers to the immediate parent class.
o Example: super.method();

Polymorphism

Polymorphism allows methods to perform different tasks based on the object that calls them.

Method Overloading:

 Defining multiple methods with the same name but different parameter lists in the same
class.
 Example:
java
Copy code
void display(int a);
void display(String b);

Method Overriding:

 Redefining a method in the subclass that already exists in the parent class.
 Example:

java
Copy code
@Override
void display() { ... }

Abstract Classes:

 Classes that cannot be instantiated and may contain abstract methods (without
implementation).
 Example:

java
Copy code
abstract class Shape {
abstract void draw();
}

Dynamic Method Dispatch:

 The process by which a call to an overridden method is resolved at runtime.


 Example:

java
Copy code
Shape s = new Circle();
s.draw(); // Executes Circle's draw method

final Keyword:

 Used to prevent inheritance, method overriding, or reassigning variables.


o Final Class: Cannot be subclassed.
o Final Method: Cannot be overridden.
o Final Variable: Its value cannot be changed once assigned.

Packages

A package is a namespace that organizes classes and interfaces.


Definition:

 A way to group related classes and interfaces.

Access Protection:

 Classes in the same package can access each other's members (except private
members).

Importing Packages:

 import packageName.ClassName;
 import packageName.*; (imports all classes in a package)

Interfaces

Interfaces define methods that must be implemented by a class.

Definition:

 A blueprint for a class; only abstract methods and static/final variables.

Implementation:

 A class implements an interface using the implements keyword.

Extending Interfaces:

 An interface can extend another interface.

Exception Handling

Handles runtime errors to maintain normal program flow.

Keywords:

1. try: Block where exceptions may occur.


2. catch: Block to handle the exception.
3. throw: Used to explicitly throw an exception.
4. throws: Declares exceptions a method might throw.
5. finally: Block that executes regardless of exceptions.
Built-in Exceptions:

 ArithmeticException
 NullPointerException
 ArrayIndexOutOfBoundsException

Creating Custom Exceptions:

 Extend the Exception class to create user-defined exceptions.


 Example:

java
Copy code
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}

You might also like