Core Java

         Java is an object oriented programming language developed by Sun Microsystems which enables programmers to create flexible, modular and reusable codes. There are four pillars of OOP

             1. Encapsulation
             2. Abstraction
             3. Polymorphism.
             4. Inheritance


1. Inheritance :
                        Inheritance provides mechanism which one object of child class can acquire all the properties and behaviors of parent object. It will create IS-A relationship.
                       Extend keyword is used in Inheritance to use parent class.
                       Main usage of inheritance in java is for code reusability and method overriding to
achieve runtime polymorphism. Multiple inheritance is not supported in java.

Example :
               
              public class DemoA{
                 
                      public void Message(){

                      System.out.println("Demo");

                        }

              }

              public class DemoB extends DemoA{
                 
                      public void Message(){

                      System.out.println("Demo");

                        }

              }

Why multiple inheritance is not supported in Java?

           To reduce the complexity and simplify the language multiple inheritance is not supported
in Java.

             public class DemoA{
                 
                      public void Message(){

                      System.out.println("Demo");

                        }

              }

              public class DemoB{
                 
                      public void Message(){

                      System.out.println("Demo");

                        }

              }

             public class DemoC extends A,B { // If it were

                  public static void main(String args[])
                  {
                           DemoC  c = new DemoC();
                           c.Message(); // Now Which Message() method would be invoked?
                  }

            }

 Output :
               Compile Time Error



1 comment: