Unit_II_Inheritance_Interface_Packages
Unit_II_Inheritance_Interface_Packages
2.1 Inheritance
(fields and methods) from another class (superclass). This promotes code reuse and establishes a
Types of Inheritance:
Method Overriding:
- When a subclass provides a specific implementation of a method already defined in its superclass.
Final Keyword:
An interface in Java is a reference type, similar to a class, that can contain only abstract methods,
static methods,
and final variables. Interfaces specify what a class must do but not how it does it.
Extending Interfaces:
2.3 Packages
A package in Java is a namespace that organizes classes and interfaces. It helps avoid name
Types of Packages:
- A package is declared using the 'package' keyword at the top of the Java source file.
Accessing Packages:
- Classes and interfaces from packages can be accessed using the 'import' statement.
Import Statement:
- 'import package.classname;' or 'import package.*;' to import specific classes or all classes from a
package.
Static Import:
- Allows access to static members of a class directly without class reference (e.g., 'import static
java.lang.Math.*').
- New classes and interfaces can be added by placing the source files in the package directory and
compiling them.