Unit - II Inheritance, Interface and Packages
Unit - II Inheritance, Interface and Packages
2.1 Inheritance: concept of inheritance , types of Inheritance: single inheritance, multilevel inheritance, hierarchical inheritance, method overriding, final variables, final methods, use of super, abstract methods and classes
2.2 Interfaces: Define interface, implementing interface, accessing interface variables and methods, extending interfaces
2.3 Package: Define package, types of package, naming and creating package, accessing package, import statement, static import, adding class and interfaces to a package
2.1 Inheritance: concept of inheritance , types of Inheritance: single inheritance, multilevel inheritance, hierarchical inheritance, method overriding, final variables, final methods, use of super, abstract methods and classes
Inheritance (Concept)
-
Inheritance is a mechanism in Java where one class acquires the properties and behaviors of another class.
-
It promotes code reusability by allowing a child class to reuse the fields and methods of a parent class.
-
The
extendskeyword is used to create a subclass from a superclass. -
Inheritance supports hierarchical relationships between classes in object-oriented programming.
-
A derived class can add its own members while also using the parent class members.
-
It helps in implementing real-world relationships and supports polymorphism.
Syntax:
class Subclass extends Superclass { }
Example:
Single Inheritance
-
In single inheritance, one subclass inherits from a single parent class.
-
It is the simplest form of inheritance in Java.
-
The subclass can access all non-private members of the parent class.
-
It helps eliminate redundant code by reusing parent class methods.
-
Only one level of class hierarchy is involved.
-
Single inheritance is the most commonly used inheritance in Java.
Syntax:
class Child extends Parent { }
Example:
Multilevel Inheritance
-
In multilevel inheritance, a class inherits from a class which is already a subclass of another class.
-
It creates a chain of inheritance from grandparent to parent to child.
-
The last derived class inherits features from both immediate and top-level parent classes.
-
It is useful when behavior needs to be passed through multiple levels.
-
Java supports multilevel inheritance using the
extendskeyword. -
It helps in deepening the hierarchy to reflect real-world models.
Syntax:
class C extends B
class B extends A
Example:
Hierarchical Inheritance
-
In hierarchical inheritance, multiple subclasses inherit from a single superclass.
-
Each subclass inherits common features from the parent and adds its own.
-
It helps avoid duplication by placing shared code in the superclass.
-
All child classes are treated independently even though they share the same parent.
-
Java supports hierarchical inheritance as long as no multiple class extensions are used.
-
It supports better organization and structure of code.
Syntax:
class A → class B extends A, class C extends A
Example:
Comments
Post a Comment