Friday, August 2, 2024

Inheritance and Types

 

Inheritance:

Def:

·        Getting Parent class Variables and Parent class Methods in child class  without rewriting variable and Method.

(Or)

               Acquiring Parent class Variables and Parent class Methods in child class .

·        To perform inheritance, we have to use 'extends' k/w.

·        extends k/w can be used to achieve the Inheritance.

ex:

package Inheritance;

public class Parent {

               // instance variable land= 25.5f;cars  = 4;house= 4;                         

                              float land=  25.5f;

                              int houses =4;

                              int cars =4;

                             

                                             // Inst Methods - display()

                              void display()

                              {

                                             System.out.println("land="+land);

                              }                                                          

                             

               }

----------------------

package Inheritance;

public class Child extends Parent

{

//  extends is predefined k/w in java

               //  can be used to perform inheritance//  extend child class  by getting parent class Variable +  Parent class Method

//                                                                                                                     copy of parent   insta variable +   Parent class Methods i.e parent

                                                                                                                        // instance variable land= 25.5f;cars  = 4;house= 4;                             

                                                                                                         //                                                                                       float land=  25.5f;

                                                                                                         //                                                                                       int houses =4;

                                                                                                         //                                                                                       int cars =4;

                                                                                                         //                                                                                      

                                                                                                         //                                                                                                      // Inst Methods - display()

                                                                                                         //                                                                                       void display()

                                                                                                         //                                                                                       {

                                                                                                         //                                                                                                      System.out.println("land="+land);

                                                                                                         //                                                                                       }                                           

              

               public static void main(String arr[])

               {

                              // create obj for 'child' class

                              Child ch1 = new Child();

                              // display parent class var- land

                              System.out.println("ch1 land"+ ch1.land);

                                             //                          25.5

                              // display parent class var- cars

                              System.out.println("ch1 cars"+ ch1.cars);

                              //                              4

                              //  call display() method

                              ch1.display();                    

                             

               }

              

}

o/p:

ch1 land25.5

ch1 cars4

land=25.5

ex2:

package Inheritance;

public class A

{

               // Define a =10;

               int a =10;

                              // Define M1()

               void M1()

               {

                              System.out.println("Parent class Method");

               }                            

}

--------------------------------------

package Inheritance;

public class B extends A

{

               //  Child class - B    and Parent class  = A

               //         get parent class variable +  parent class method in chilld class i.e a=10  and M1()

               //                                                                                                                                                                  int a =10;

               //                                                                                                                                                                  void M1()

               //                                                                                                                                                                  {

               //                                                                                                                                                                                 System.out.println("Parent class Method");

               //                                                                                                                                                                  }

               //  Define b =20

               int b =20;

               // Define M2()

               public void M2()

               {

                              System.out.println("Child class Method - M2()");

               }

               public static void main(String[] args) {

                              //  Call parent class variable and Parent class Methods --

                              // create obj for class - A   and it has variable  a=10, and M1()

                              A aref =  new A();

                              // call M1()

                              aref.M1();

                              // create obj  for child class i.e  B

                              B bref=  new B();

                              // Display b1 a

                              System.out.println("bref a ="+ bref.a);

                              //

                              // calling b1 M1()

                              // Parent class Method

                              bref.M1();

                              // Display b1 b

                              System.out.println("bref b="+ bref.b);    

                              // 20

                              // Child class Method - M2()       

                              bref.M2();

               }

}

o/p:

Parent class Method

bref a =10

Parent class Method

bref b=20

Child class Method - M2()

Note:

·  Object Creation for Parent Class:

  • When an object is created for the parent class, only the parent class's variables and methods can be accessed.

   Or

If we create object for parent class, we can access parent class variable and parent class Methods only.

 

·  Object Creation for Child Class:

  • When an object is created for the child class, it has access to the child class's variables and methods, as well as the parent class's variables and methods.

Or

If we create object for child class, we can access child class variable  and child class Methods  and also parent class variable and parent class Methods.

 

FAQ What is Inheritance?

k/w   ------ ????  --------

     child               parent

 

HW  Create 2 classes C,D. D is parent class and C is child class .  display parent class variable and Methods using child class obj refer variable ?

HW create Vehicle- class , declare instance variable,  brand = "Ford";  sound() in Parent class

   create 'Car' class, modelName = "Mustang";  --> child class,  display parent class variable and Methods using child class obj refer variable ?

if Parent class and child class  have same variable names :

 

package Inheritance;

public class A

{

               // Define a =10;

               int a =10;

}

--------------------

package Inheritance;

//get variable +  Methods from class -A

public class B extends A

{

               //  B - child class,  A- Parent class

               // get parent class variable  +  parent class Method

                                                                                                                        //                         int a =10;              

               int a=20;             

               public static void main(String[] args) {

                             

                              B b1 =  new B();

                             

                              System.out.println("b1 .a ="+ b1.a);

                              //  display child class variable val  i.e 20  but not parent class variable val

                              //                              but not 10

                             

                              //  if  parent class and child class have same variable name i.e 'a' ,

                              //1st priority will be given for child class variable but not parent class var

               }

}

o/p:

b1 .a =20

Parent class and Child class has same method name:

package InheritanceBasics;

public class A

{

               int a =10;            

              

               void M1()

               {

                              System.out.println("M1() from Parent class");

               }

}

--------------

package Inheritance;

//get variable +  Methods from class -A

public class B extends A

{

               //  B - child class,  A- Parent class

               // get parent class variable  +  parent class Method

                                                                                                         // Define a =10;

//                                                                                                                     int a =10;

                                                                                                                       

//                                                                                                                     void M1()

//                                                                                                                     {

//                                                                                                                                    System.out.println("M1() from Parent class");

//                                                                                                                     }

               int a=20;

              

               void M1()

               {

                              System.out.println("Child class - M1 Method");

               }

               public static void main(String[] args) {

                             

                              B b1 =  new B();

                             

                              System.out.println("b1 .a ="+ b1.a);

                              //  display child class variable val  i.e 20  but not parent class variable val

                              //                              but not 10

                             

                              //  if  parent class and child class have same variable name i.e 'a' ,

                              //1st priority will be given for child class variable but not parent class var

                             

               //  if  parent class and child class have same Method name i.e 'M!' ,

                              //1st priority will be given for child class Metohd  but not parent class Method

                             

                                             b1.M1();

               }

}

o/p:

b1 .a =20

Child class - M1 Method

Note:

To access parent class variable and Methods -- we have to use 'Super'  k/w 

Other Terminologies in Inheritance :

Parent class - Super class - Base class

child class -  Sub class   - Derived class

super:

FAQ What is the super k/w?

·        ‘super’ is k/w in java.

·        The super keyword serves as a reference to the parent class in Java

 

·        ‘super’ k/w can be used to refer parent class variable 

·        ‘super’ k/w can be used to refer parent class Method

·        ‘super’ k/w can be used to refer parent class constructor

 

// parent class variable 

 

                                             //                                         //             parent class Method

                                             //                       parent class constr

 

//                                          This :

                              //                                              can be used to refer current class variable  

                                                                           //                       current class Method

                                                                           //                       current class constr

 

Ex on super :     

package Inheritance;

class A5

{

               int a =10;

               void M1()

               {

                              System.out.println("Parent class M1()");

               }

}

package Inheritance;

class B5 extends A5

{

               //     copy           a=10  and M1()  from Class A5

//                                                                                                                                                   void M1()

//                                                                                                                                                   {

//                                                                                                                                                                  System.out.println("Parent class M1()");

//                                                                                                                                                   }

              

               int a= 20;

               void M1() // non static method

               {

                              System.out.println("Child class M1()");

                              //  calling parent class variable  using super k/w

                              System.out.println("parent class variable " + super.a);

                              //                                                                                                                                   10

                              //  calling parent class method M1 using super k/w

                              super.M1();                       

                              //Parent class M1()"

               }

}

package Inheritance;

public class TestSuper

{

               public static void main(String[] args) // static method

               {

                              B5 b5ref =  new B5();

                                             System.out.println("b5 a ="+ b5ref.a);// 20

                                             // access child class variable as it has high priority

                                             //   2 variable have have same name  and same Method  --  we can acces  only child class variable  and child class Method

                                             b5ref.M1();//Child class M1()

                                            

                                             //  how can we access parent class variable and parent class Method

//                                          super.a =30;                                    

                                            

//                                          System.out.println("accessing parent class Variable ="+ super.a);

                                             //  CE:   Cannot use super in a static context

                                             // super k/w cannot be used in side static Method

                                             // Note:   this , super k/w's should not be used inside static method

                                            

                                            

//                                          super.M1();

                             

               }

}

o/p:

b5 a =20

Child class M1()

parent class variable 10

Parent class M1()

Calling parent class constructor using super  :

package InheritanceConstrctor;

public class A6

{

               // Define instance variable a =10;

               int a =10;

               // Define constr A6

               A6()

               {

                              System.out.println("Dc from Parent class A6");

               }             

               // Define M1()

               void M1()

               {

                              System.out.println("Parent class M1()");

               }

              

}

package InheritanceConstrctor;

class B6 extends A6

{

//                                                                                                      // Define instance variable a =10;

//                                                                                                                     int a =10;

//                                                                                      

//                                                                                                                     // Define constr A6

//                                                                                                                     A6()

//                                                                                                                     {

//                                                                                                                                    System.out.println("Dc from Parent class A6");

//                                                                                                                     }             

//                                                                                      

//                                                                                                                     // Define M1()

//                                                                                                                     void M1()

//                                                                                                                     {

//                                                                                                                                    System.out.println("Parent class M1()");

//                                                                                                                     }

                             

              

// Define instance variable b =20;

               int b =20;

              

               // define constr for B6   

 //  constr

               B6()

               {

                              System.out.println("DC is called  from child class -B6");

               }             

               //Define M2()

               void M2()

               {

                              System.out.println("child class M2");

               }

                             

}

package InheritanceConstrctor;

public class TestSuperConstr {

               public static void main(String[] args) {

                              // TODO Auto-generated method stub

                              // create obj for class B6 -  then constr will be called by default

                               B6 b6ref=  new B6();                    

                               //  calls DC of B6                           

               }

}

o/p:

Dc from Parent class A6

DC is called  from child class -B6

Ex: Calling parent class- Default constructor:

package InheritanceBasics;

class A6

{

               int a =10;

               A6()

               {

                              System.out.println("Dc from Parent class A6");

               }

               void M1()

               {

                              System.out.println("Parent class M1()");

               }             

}

package InheritanceConstrctor;

class B6 extends A6

{

               int b =20;            

               B6() //  constr

               {

                              // call parent class constr

                                                            // optional

//                           super();// call dc of parent class

                              System.out.println("DC is called  from child class -B6");

//                           super();// call parent class constr //  Constructor call must be the first statement in a constructor

                              // We should not write in 2nd line or 3rd line

                              //  always super() mus be defined as first line only  inside constr

               }             

               void M2() // non static method

               {

                              System.out.println("child class M2");

                              super.a =33;

                              super.M1();

                             

                              // call super class constr

//                           super();

//                           super();// CE: Constructor call must be the first statement in a constructor

                              //  Note : super() constr - must be used inside constr only

                              //    must be the first statement  --  We should not use in non static method

               }

}

package InheritanceConstrctor;

public class TestSuperConstr {

               public static void main(String[] args)

               {

                              // create obj for class - B6 then constr will be called by default

                              B6 b6ref = new B6();

                                                                                          //  calls DC of B6

                              // call Parent class constr

//                                          super();

                             

//                                          super();// Dc

//                                          super(10);  call 1 PC

                                            

                             

               }

}

o/p:

Dc from Parent class A6

DC is called  from child class -B6

ex:  Calling 1 PC with super :

package InheritanceBasics;

class A6

{

               int a =10;

               A6()

               {

                              System.out.println("Dc from Parent class A6");

               }

               //define 1 - PC -- pass Parameter  -  1

               A6(String name) //  1 PC

               {

                              System.out.println("1 PC is called name="+ name);

               }

              

               void M1()

               {

                              System.out.println("Parent class M1()");

               }             

}

package InheritanceConstrctor;

class B6 extends A6

{

               int b =20;            

               B6() //  constr

               {

//                           super();// optional

                              // call super class 1 PC

                              super("Rama");

                              System.out.println("DC is called  from child class -B6");

//                           super();// call parent class constr //  Constructor call must be the first statement in a constructor

                              // We should not write in2nd line or 3rd line

                              //  always super() mus be defined as first line only  inside constr

                             

                              // HW  Define 2 PC and call using super k./w

               }             

               void M2() // non static method

               {

                              System.out.println("child class M2");

                              super.a =33;

                              super.M1();

//                           super();// CE: Constructor call must be the first statement in a constructor

                              //  Note : super() constr - must be used inside constr only

                              //    must be the first statement  --  We should not use in non sttatic method

//                           super("Rama");

              

               }

}

package InheritanceConstrctor;

public class TestSuperConstr {

               public static void main(String[] args)

               {

                              // create obj for class -  then constr will be called by default

                                             B6 b6ref = new B6(); //  calls DC of B6

//                                          super();// Dc

                                            

//                                          super(10);  //call 1 PC

                                            

                                            

                             

               }

}

o/p:

1 PC is called name=Ramu

DC is called  from child class -B6

// HW  Define 2 PC and call using super k./w

HW WAP to call parent class variable,  Methods, constructors using 'super' k/w?

HW WAP to call parent class -1 param constr?

HW WAP to call parent class -2 param constr?

FAQ what is the use of super ?

super:  refer parent class var

                           Method

                            constr

   parent class   int a=10

 child class      int a=20;

 

FAQ What is this k/w ?

this -  refer current class variables

                            Method

                                                  Constr

Differences Between this and super Keywords in Java

Feature

this

super

Usage Scope

Refers to the current class instance

Refers to the parent class

Instance Variables

Accesses current class instance variables

Accesses parent class instance variables

Methods

Invokes current class methods

Invokes parent class methods

Constructors

Calls current class constructors

Calls parent class constructors

Main Purpose

Differentiates instance variables from local variables and constructor chaining within the same class

Accesses and overrides parent class members

Context

Can only be used within an instance method or constructor

Can be used in instance methods, constructors, or even instance initializer blocks

Keyword Placement

Not necessary to place explicitly if not needed

Required explicitly when referencing parent class members

 

Types of inheritance:

 

1. Single Level:   

 Single Level Inheritance is a type of inheritance in object-oriented programming where a class (child or derived class) inherits properties and behaviors (methods) from a single parent class (base or superclass).

 

class A

{

}

class B extends A  //  single level inheritance

{

}

2. Multi level:

Multi-Level Inheritance is a type of inheritance in object-oriented programming where a class is derived from another class, which is also derived from another class, forming a chain of inheritance.

Forms a chain where a class inherits from another class, which in turn inherits from another class, and so on.

 

package InheritanceBasics;

class A1

{

               int a1 =10;

               void M1()

               {

                              System.out.println("Calling M1 ()");

               }

              

}

class B1 extends A1  //  Single Level Inheritance

{ //  B1  child class , A1 parent class

               // gets parent class variable  +  method s to child class

               // i.e  from class A1 -  a1, M1()

               //                                                       from Class A1

//                                                                                                                                                                                                                              int a1 =10;

//                                                                                                                                                                                                                              void M1()

//                                                                                                                                                                                                                              {

//                                                                                                                                                                                                                                             System.out.println("Calling M1 ()");

//                                                                                                                                                                                                                              }

               int b1 =20;

               void M2()

               {

                              System.out.println("Calling M2 ()");

               }

}

public class C1 extends B1

{   //  child class C1  and Parent class B1

               //  get parent class B1 class variable  + B1 class Method into child class i.e C1

                                             //            //       from Class A1

                                                                                                                                       //            int a1 =10;

                                                                                                                                       //            void M1()

                                                                                                                                       //            {

                                                                                                                                       //                           System.out.println("Calling M1 ()");

                                                                                                                                       //            }

                                                            // From class B1

                                                                                                                        //int b1 =20;

                                                                                                                        //void M2()

                                                                                                                        //{

                                                                                                                        //System.out.println("Calling M2 ()");

                                                                                                                        //}

    int c1 =30;

               void M3()

               {

                              System.out.println("Calling M3 ()");

               }

               public static void main(String[] args)

               {

                              // create obj for class A1

                              A1  a1ref = new A1();

                              System.out.println("a1 var="+ a1ref.a1);// 10

                              a1ref.M1();                        // Calling M1 ()

                             

                              // create  obj for child class B1

                              B1 b1ref = new B1();

                              System.out.println("b1 variable b1 =" + b1ref.b1);// 20

                              b1ref.M2();// Calling M2 ()

                             

                              //  call parent class - A1 variable and + Method  i.e a1 and M1()

                              System.out.println("A1 class varaible can be accessed by using child obj ="+ b1ref.a1);// 10

                              b1ref.M1();// Calling M1 ()"

                             

                              // create obj for child class C1

                             

                              C1 c1ref = new C1();

                              System.out.println("c1 variable ="+ c1ref.c1);//30

                              // call M3()

                              c1ref.M3();// Calling M3 ()

                             

                              //  access  B1 class variable  + Method

                              System.out.println("B1 variable can be accessed using child obj ref ="+ c1ref.b1);//20

                              // call M2()

                              c1ref.M2();// Calling M2 ()

                              // HW Access  A1 class variable and Method i.e a1 and M1()        

                             

               }

}

o/p:

a1 var=10

Calling M1 ()

b1 variable b1 =20

Calling M2 ()

A1 class varaible can be accessed by using child obj =10

Calling M1 ()

c1 variable =30

Calling M3 ()

B1 variable can be accessed using child obj ref =20

Calling M2 ()

Hierarchical:

Hierarchical Inheritance is a type of inheritance in object-oriented programming where multiple classes (derived classes) inherit from a single base class (parent class).

 

2 or more  classes extends the same class

 

package InheritanceConstrctor;

class A3

{             

               int a3 =10 ;

}

class B3 extends A3

{

//  child class - B3

               // PArent - A3

                                                                           //                           int a3 =10

               int b3=20;           

}

public class C3 extends A3//  2 classes extends the same class i.e A3 -- Hierarchical Inheritance

{  //  child  - C3

               //parebt  - A3

                                                                                          //gets int a3 =10 ;

              

               int c3=30;

               public static void main(String[] args) {

                             

                              A3 a3 =  new A3();

                              //HW access A3 Class variable

                              System.out.println("a3 ref.a3 ="+ a3.a3); // 10

                             

                             

                              B3 b3 = new B3();

                              //HW     // access B3 Class variable   and A3 class var

                             

                              C3 c3 = new C3();

                              //HW  // access C3 Class variable   and A3 class variable

}

              

              

              

}

o/p:

a3 ref.a3 =10

Multiple  Inheritance :

Multiple Inheritance is a type of inheritance in object-oriented programming where a class (child or derived class) can inherit properties and behaviors (methods) from more than one base class (parent class).

 

package InheritanceConstrctor;

class A4

{

               // Define inst variable a =10

               int a =10;

}

class B4

{

                              // Define inst variable a =20

               int a =20;

}

class C4 extends A4,B4

//extends A4,B4// invalid synatx

//after extends k.w, we must write only one class  but not multiple classes

//Multiple inheritance is not possible in java using class concept as there is an ambiguity or confusion to compiler

//but  Multiple inheritance is possible using Interface concept 

{

               //  int a=10 from class A4

               //int a =20;  from class B4

              

}

public class TestMultipleInheritance {

               public static void main(String[] args) {

                              // create obj for class- C4

                              C4 c4ref = new C4();

                             

                              // display c4 a

                               System.out.println("c4 a="+  c4ref.a);

                              //   displya  10 or 20  ???  ambiguity issue

               }

}

Hybrid  : not possible

Hybrid Inheritance is a combination of two or more types of inheritance.

It can be any mix of single, multiple, hierarchical, or multi-level inheritance.

 However, Java does not support multiple inheritance with classes

Instead, Java allows multiple inheritance through interfaces.

 

  is combination of difff types of inheritance -  single Level + Multi level +  Hierarchical  +  multiple

Since Java does not support multiple inheritance, hybrid inheritance is also not possible in Java

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...