Description
2.1 Inheritance
Inheritance is a core concept in object-oriented programming (OOP) that allows a new class (the subclass or child class) to inherit properties and behaviors (fields and methods) from an existing class (the superclass or parent class). This promotes code reusability and establishes a clear “is-a” relationship between classes.
Types of Inheritance
- Single Inheritance: A subclass inherits from a single superclass. For example,
Caris aVehicle. - Multilevel Inheritance: A class inherits from a subclass, which in turn inherits from a superclass. For example,
SportsCarinherits fromCar, which inherits fromVehicle. - Hierarchical Inheritance: Multiple subclasses inherit from a single superclass. For example, both
CarandMotorcycleinherit fromVehicle.
Key Concepts in Inheritance
- Method Overriding: A subclass provides its own specific implementation for a method that is already defined in its superclass. The method signature (name and parameters) must be identical.
finalVariables and Methods:- A
finalvariable can be assigned a value only once; its value cannot be changed later. - A
finalmethod cannot be overridden by a subclass.
- A
superKeyword: Thesuperkeyword is used to refer to the superclass’s members, such as a constructor (super()) or a method (super.methodName()). It’s often used in constructors to initialize the parent class or to call an overridden method from the parent class.abstractMethods and Classes:- An
abstractmethod is a method with no body, only a declaration. It must be implemented by any non-abstract subclass. - An
abstractclass is a class that may contain one or more abstract methods. You cannot create an instance (object) of an abstract class. It must be extended by a subclass that provides implementations for its abstract methods.
- An
2.2 Interfaces
An interface is a blueprint of a class. It can contain method declarations (by default public and abstract) and constant fields (by default public, static, and final). It provides a way to achieve multiple inheritance and define a contract for what a class must do, without specifying how it does it.
Key Concepts with Interfaces
- Implementing an Interface: A class uses the
implementskeyword to implement an interface, providing a concrete implementation for all the methods declared in the interface. - Accessing Interface Members: Interface variables are automatically
public,static, andfinal, so they are accessed using the interface name (InterfaceName.CONSTANT_NAME). Interface methods are accessed through a class that implements the interface. - Extending Interfaces: An interface can
extendanother interface. A class that implements a sub-interface must provide implementations for all methods from both the sub-interface and the parent interface.
2.3 Packages
A package is a way of organizing related classes, interfaces, and sub-packages. It helps in managing a large number of files, avoiding naming conflicts, and controlling access to classes.
Key Concepts with Packages
- Types of Packages:
- Built-in Packages: Packages that are part of the Java Development Kit (JDK), such as
java.lang,java.io, andjava.util. - User-defined Packages: Packages created by the programmer.
- Built-in Packages: Packages that are part of the Java Development Kit (JDK), such as
- Naming and Creating a Package: A package is defined using the
packagekeyword at the very first line of a Java source file. The package name convention is typically all lowercase. - Accessing a Package: To use classes and interfaces from another package, you can either use the fully qualified name (
packageName.ClassName) or use theimportstatement to make the classes directly accessible. importStatement: Theimportstatement is used to bring classes from another package into the current file. You can import a single class (import packageName.ClassName;) or all classes in a package (import packageName.*;).static import: This is an enhanced form of theimportstatement that allows you to accessstaticmembers (fields and methods) of a class without using the class name.- Adding Class and Interfaces to a Package: To add a class or interface to a package, you simply declare it with the
packagestatement at the top of the file. The file must be placed in a directory structure that matches the package name.





Reviews
There are no reviews yet.