Inheritance Understand with inner class and inner static class
Inheritance is a fundamental concept in object-oriented
programming (OOP). It allows a new class (called a subclass or derived class)
to inherit properties and behaviors (methods) from an existing class (called a
superclass or base class). Here’s a breakdown of key aspects of inheritance:
- Reusability:
Inheritance promotes code reusability. You can create a new class by
extending an existing class, thus reusing fields and methods of the parent
class.
- Hierarchy:
It helps in establishing a hierarchical relationship between classes. For
instance, a Dog class can inherit from an Animal class, establishing that
a dog is a type of animal.
- Method
Overriding: A subclass can provide a specific implementation of a
method that is already defined in its superclass. This is known as method
overriding.
- Polymorphism:
Inheritance supports polymorphism, which allows objects to be treated as
instances of their parent class rather than their actual class. This
enables one interface to be used for a general class of actions.
- Single
Inheritance vs. Multiple Inheritance:
Ø
Single Inheritance: A class inherits from
one superclass. This is the most common form of inheritance.
__________________1 I am static A I am static B I am static C I am static F I am {} A I am
parameterised constructor A I am {} B I am
constructor B I am {} C __________________2 I am function
A __________________3 30 __________________4 I am {} A I am
parameterised A __________________5 10 __________________6 20 __________________7 50 __________________8 I am {} A I am
parameterised constructor A I am {} B I am
constructor B I am {} C __________________9 __________________10 I am function
C.D __________________11 I am {} A I am
parameterised constructor A I am {} B I am
constructor B __________________12 60 __________________13 |
class A { int i = 10; static { System.out.println("I am static
A"); } { System.out.println("I am {} A"); } public A() { System.out.println("I am parameterised
A"); } public A(int i) { this.i = i + 20; System.out.println("I am parameterised
constructor A"); } void function() { System.out.println("I am function
A"); } } class B
extends A { static int i = 10; static { System.out.println("I am static B"); } { System.out.println("I am {} B"); } public B() { super(i); System.out.println("I am constructor
B"); } } class C
extends B{ static class D { static int i=50; } class E { B b=new B(); } static { System.out.println("I am static
C"); } { System.out.println("I am {}
C"); } C(){ i = i + 10; } } class F
extends C{ class H extends D { void function() { System.out.println("I am function
C.D"); } } class G extends E { int i = 60; public G(){ super(); } } static { System.out.println("I am static
F"); } } public class
Source{ public static void main(String[] args) {
System.out.println("__________________1"); A a = new F();
System.out.println("__________________2"); a.function();
System.out.println("__________________3"); System.out.println(a.i);
System.out.println("__________________4"); a=new A();
System.out.println("__________________5"); System.out.println(a.i);
System.out.println("__________________6"); System.out.println(B.i);
System.out.println("__________________7"); System.out.println(C.D.i);
System.out.println("__________________8"); F f=new F();
System.out.println("__________________9"); F.H b=f.new H();
System.out.println("__________________10"); b.function();
System.out.println("__________________11"); F.G c=f.new G();//this is inner non
static class call System.out.println("__________________12"); System.out.println(c.i);
System.out.println("__________________13"); } } |
__________________1 I am static A I am static B I am static C I am static F I am {} A I am
parameterised constructor A I am {} B I am
constructor B I am {} C __________________2 I am function
A __________________3 30 __________________4 I am {} A I am
parameterised A __________________5 10 __________________6 20 __________________7 50 __________________8 I am {} A I am parameterised
constructor A I am {} B I am
constructor B I am {} C __________________9 __________________10 I am function
C.D __________________11 I am {} A I am
parameterised constructor A I am {} B I am
constructor B __________________12 60 __________________13 Sequence is
---> static block -->simple block ---> class A { int i = 10; static { System.out.println("I am static
A"); } { System.out.println("I am {} A"); } public A() { System.out.println("I am parameterised
A"); } public A(int i) { //Constructor incrementing static variable
or simple variable this.i = i + 20; System.out.println("I am parameterised
constructor A"); } void function() { System.out.println("I am function
A"); } int a; //initialized
to zero static int b;
//initialized to zero only when class is loaded not for each object created. A(String x,String y){ //Constructor incrementing static variable
b b++; } public void showData(){ System.out.println("Value of a =
"+a); System.out.println("Value of b =
"+b); } } class B
extends A { static int i = 10; static { System.out.println("I am static
B"); } { System.out.println("I am {} B"); } public B() { super(i); System.out.println("I am constructor
B"); } } class C
extends B{ static class D { static int i=50; } class E { B b=new B(); } static { System.out.println("I am static
C"); } { System.out.println("I am {}
C"); } C(){ i = i + 10; } } class F
extends C{ class H extends D { void function() { System.out.println("I am function
C.D"); } } class G extends E { int i = 60; public G(){ super(); } } static { System.out.println("I am static
F"); } } public class
Source{ public static void main(String[] args) {
System.out.println("__________________1"); //this will print first static block and
next with out static block from A to F A a = new F();
System.out.println("__________________2"); a.function();
System.out.println("__________________3"); System.out.println(a.i);
System.out.println("__________________4"); a=new A();
System.out.println("__________________5"); System.out.println(a.i);
System.out.println("__________________6"); System.out.println(B.i);
System.out.println("__________________7"); System.out.println(C.D.i);
System.out.println("__________________8"); F f=new F();
System.out.println("__________________9"); //this is inner non static class call F.H b=f.new H();
System.out.println("__________________10"); b.function();
System.out.println("__________________11"); //this is inner non static class call F.G c=f.new G();
System.out.println("__________________12"); System.out.println(c.i);
System.out.println("__________________13"); // static inner class declaration C.D innerTwo=new C.D(); System.out.println(innerTwo.i); // this is inner super class with
parameterised argument pass B b1=new B(); C c1=new C(b1); System.out.println(c1.a.i); } } |
Ø
Multiple Inheritance: A class can inherit
from more than one superclass. Some languages like Python support this, while
others like Java do not (instead, Java uses interfaces to achieve similar
functionality).
Example in Python:
python
# Base class class Animal: def __init__(self, name): self.name = name def speak(self): raise
NotImplementedError("Subclass must implement abstract method") # Derived
class class
Dog(Animal): def speak(self): return f"{self.name} says
Woof!" class
Cat(Animal): def speak(self): return f"{self.name} says
Meow!" # Creating
objects dog =
Dog("Buddy") cat =
Cat("Whiskers") print(dog.speak()) # Output: Buddy says Woof! print(cat.speak()) # Output: Whiskers says Meow! |
In this example:
Ø Animal
is the base class with an abstract method speak.
Ø Dog
and Cat are derived classes that inherit from Animal and provide their own
implementation of the speak method.
Ø When
speak is called on instances of Dog and Cat, the overridden method in the
respective subclass is executed.
Benefits of Inheritance:
Ø Code
Maintenance: It makes code easier to maintain and modify.
Ø Logical
Grouping: It helps in logically grouping related classes, which makes the
code more readable and organized.
Drawbacks of Inheritance:
Ø Complexity:
Deep inheritance hierarchies can lead to increased complexity.
Ø Tight
Coupling: Subclasses are tightly coupled with their superclasses, which can
reduce flexibility.
Understanding and effectively using inheritance can lead to
more robust and maintainable code in object-oriented programming.
0 Comments