Inheritance Understand


Grasping Inheritance through inner class and inner static class

Inheritance is a core principal aspect of 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:

  1. Reusability: Inheritance promotes code reusability. By extending an existing class, you can create a new class that reuses the fields and methods of its parent class.
  2. 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.
  3. Method Overriding: A subclass can offer a distinct implementation of a method that is already present in its superclass. This is known as method overriding.
  4. Polymorphism: Inheritance supports polymorphism, which allows objects to be treated as instances of their parent class rather than their actual class. This facilitates the use of a single interface for a general class of actions.
  5. Single Inheritance vs. Multiple Inheritance:

Ø  Single Inheritance: A class derives from one superclass, which is the most prevalent type of inheritance.

__________________1

I am kcm static A

I am kcm static B

I am kcm  static C

I am kcm static F

I am kcm {} A

I am kcm  parameterised constructor A

I am kcm {} B

I am kcm  constructor B

I am kcm {} C

-------------------2

I am kcm function A

------------------3

30

-----------------4

I am kcm {} A

I am kcm  parameterised A

-----------------5

10

-----------------6

20

-----------------7

50

-----------------8

I am kcm {} A

I am kcm  parameterised constructor A

I am kcm {} B

I am kcm  constructor B

I am kcm {} C

------------------9

------------------10

I am kcm function C.D

------------------11

I am kcm {} A

I am kcm  parameterised constructor A

I am kcm {} B

I am kcm  constructor B

--------------------12

60

--------------------13

 

class KcmA {

 int i = 10;

 static {

  System.out.println("I am kcm static A");

 }

 {

  System.out.println("I am kcm {} A");

 }

 

 public KcmA() {

  System.out.println("I am kcm parameterised A");

 }

 

 public KcmA(int i) {

  this.i = i + 20;

  System.out.println("I am kcm parameterised constructor A");

 }

 

 void function() {

 System.out.println("I am kcm function A");

 }

}

 

class KcmB extends KcmA {

 static int i = 10;

 static {

  System.out.println("I am kcm static B");

 }

 {

  System.out.println("I am kcm {} B");

 }

 

 public KcmB() {

  super(i);

  System.out.println("I am kcm constructor B");

 }

}

class KcmC extends KcmB{

 

  static class KcmD

     {

   static int i=50;

     }

     class KcmE

     {

      KcmB b=new KcmB();

     

     }

    

     static

     {

      System.out.println("I am kcm static C");

     }

     {

      System.out.println("I am kcm {} C");

     }

     KcmC(){

      i = i + 10;

     }

}

class KcmF extends KcmC{

 

 class KcmH extends KcmD

    {

  void function()

     {

   System.out.println("I am kcm function C.D");

     }

    }

    class KcmG extends KcmE

    {

     int i = 60;

     public KcmG(){

      super();

     }

    

    }

   

    static

    {

     System.out.println("I am kcm static F");

    }

}

 

public class KcmSource{

    public static void main(String[] args)

    {

        System.out.println("--------------1");

        KcmA a = new KcmF();

        System.out.println("--------------2");

        a.function();

        System.out.println("--------------3");

        System.out.println(a.i);

        System.out.println("--------------4");

        a=new KcmA();

        System.out.println("--------------5");

        System.out.println(a.i);

        System.out.println("--------------6");

        System.out.println(KcmB.i);

        System.out.println("--------------7");

        System.out.println(KcmC.KcmD.i);

       

        System.out.println("--------------8");

        KcmF f=new KcmF();

        System.out.println("--------------9");

        KcmF.KcmH b=f.new KcmH();

        System.out.println("--------------10");

        b.function();

        System.out.println("--------------11");

        KcmF.KcmG c=f.new KcmG();//this is inner non static class call

        System.out.println("--------------12");

        System.out.println(c.i);

        System.out.println("--------------13");

    }

}

 

 

^^^^^^^^^1

I am Kamical static A

I am Kamical static B

I am Kamical static C

I am Kamical static F

I am Kamical {} A

I am Kamical  parameterised constructor A

I am Kamical {} B

I am Kamical constructor B

I am Kamical {} C

^^^^^^^^^^^^^2

I am Kamical function A

^^^^^^^^^^^^^^3

30

^^^^^^^^^^^^^^4

I am Kamical {} A

I am Kamical  parameterised A

^^^^^^^^^^^^^^5

10

^^^^^^^^^^^^^6

20

^^^^^^^^^^^^^7

50

^^^^^^^^^^^^^8

I am Kamical {} A

I am Kamical  parameterised constructor A

I am Kamical  {} B

I am Kamical   constructor B

I am Kamical {} C

^^^^^^^^^^^^^^^9

^^^^^^^^^^^^^^^10

I am Kamical function C.D

^^^^^^^^^^^^^^^11

I am Kamical {} A

I am Kamical   parameterised constructor A

I am Kamical {} B

I am Kamical  constructor B

^^^^^^^^^^^^^^12

60

^^^^^^^^^^^^^^13

 

 

 

Sequence is ---> static block -->simple block --->

 

 

 

 

class KamicalA {

 int i = 10;

 static {

  System.out.println("I am Kamical static A");

 }

 {

  System.out.println("I am Kamical{} A");

 }

 

 public KamicalA() {

  System.out.println("I am Kamical parameterised A");

 }

 

 public KamicalA(int i) {

  //Constructor incrementing static variable or simple variable

  this.i = i + 20;

  System.out.println("I am Kamical parameterised constructor A");

 }

 

 void function() {

  System.out.println("I am Kamical function A");

 }

 

int a; //initialized to zero

static int b; //initialized to zero only when class is loaded not for each object created.

 

  KamicalA(String x,String y){

   //Constructor incrementing static variable b

   b++;

  }

 

   public void showData(){

      System.out.println("Value Kamical of a = "+a);

      System.out.println("Value Kamical of b = "+b);

   }

  

 

 

}

class KamicalB extends KamicalA {

 static int i = 10;

 static {

  System.out.println("I am Kamical static B");

 }

 {

  System.out.println("I am Kamical{} B");

 }

 

 public KamicalB() {

  super(i);

  System.out.println("I am Kamical constructor B");

 }

}

class KamicalC extends KamicalB{

 

  static class KamicalD

     {

   static int i=50;

     }

     class KamicalE

     {

      KamicalB b=new KamicalB();

     

     }

    

     static

     {

      System.out.println("I am Kamical static C");

     }

     {

      System.out.println("I am Kamical{} C");

     }

     KamicalC(){

      i = i + 10;

     }

}

class KamicalF extends KamicalC{

 

 class KamicalH extends KamicalD

    {

  void function()

     {

   System.out.println("I am Kamical function C.D");

     }

    }

    class KamicalG extends KamicalE

    {

     int i = 60;

     public KamicalG(){

      super();

     }

    

    }

   

    static

    {

     System.out.println("I am Kamical static F");

    }

}

 

public class KamicalSource{

    public static void main(String[] args)

    {

        System.out.println("^^^^^^^^^1");

  //this will print first static block and next with out static block from KamicalA to KamicalF

        KamicalA a = new KamicalF();

        System.out.println("^^^^^^^^^2");

        a.function();

        System.out.println("^^^^^^^^^3");

        System.out.println(a.i);

        System.out.println("^^^^^^^^^4");

        a=new KamicalA();

        System.out.println("^^^^^^^^^5");

        System.out.println(a.i);

        System.out.println("^^^^^^^^^6");

        System.out.println(KamicalB.i);

        System.out.println("^^^^^^^^^7");

        System.out.println(KamicalC.KamicalD.i);

       

        System.out.println("^^^^^^^^^8");

        KamicalF f=new KamicalF();

        System.out.println("^^^^^^^^^9");

  //this is inner Kamical non static Kamical class call

        KamicalF.KamicalH b=f.new KamicalH();

        System.out.println("^^^^^^^^^10");

        b.function();

        System.out.println("^^^^^^^^^11");

  //this is inner Kamical non static Kamical class call

        KamicalF.KamicalG c=f.new KamicalG();

        System.out.println("^^^^^^^^^12");

        System.out.println(c.i);

        System.out.println("^^^^^^^^^13");

  // static inner class declaration

  KamicalC.KamicalD innerTwo=new KamicalC.KamicalD(); 

  System.out.println(innerTwo.i);

  // This represents an inner superclass of Kamical that accepts parameterized arguments.

  KamicalB b1=new KamicalB();

  KamicalC c1=new KamicalC(b1);

  System.out.println(c1.a.i);

 

    }

}

 

 

Ø  Multiple Inheritance: A class has the ability to inherit from multiple superclasses. Some languages like Python support this, while others like Java do not (instead, Java uses interfaces to achieve similar functionality).

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.

 


Post a Comment

0 Comments