Inheritance & Access Modifiers


Inheritance

  • Combining things of one property to another property is known as Inheritance.

Consider these code samples,

class Car (){                    class ModernCar{                         class Car{
void drive (){                  void drive (){                                void drive (){
}                                      }                                                    }
void brake (){                 void brake (){                                void brake(){
}                                      }                                                    }
void reverse (){              void reverse (){                             void reverse (){
}                                      }                                                    }
}                                      void fly (){                                    }
 Modern Car inherit        }                                                    class Car extends ModernCar
 Car                                 void surf (){                     void fly (){
 //inherit = extends          }                                                    }
                                        }                                                    void surf (){
                                                                                              }   
                                                                                              void drive (){
                                                                                              }    
                                                                                              void reverse (){
                                                                                              }     
                                                                                              void brake (){
                                                                                              }     

  • Orange = Super Class
         Red =  Sub Class       

  • In the highlighted code sample, the data will auto received because the "Car extended"  with "ModernCar".       
  1. We can get one class's data using inheritance.
  2. Main class is identified as "Super Class" & New class is identified as "Sub Class"
  3. In this scenario, the data will automatically goes to the sub class.
  4. Also, we can add more new things to the sub class.


      Access Modifiers

     According to the security (modification) provided for the classes, variables, methods.            The access of data will be decided whether yes or no.

  • There are 4 types of access modifiers, namely,
  1. Public
  2. Protected
  3. Default
  4. Private

     Public

                                                           Human -- Java Project
                   Human Java package --  Human 
                                                           Human.java -- Human Package > Human Java class
  •                         Man Java Package --  Man
                                                           Man.java -- Man Package > Man Java class
                        Child Java Package-- Child -- Child Package > Child Java class

  • In this part the access modifier is Public.
  • Classes, variables, methods access = Yes

  • If the classes, variables, methods are public any classes, variables, methods in this project and package, class can be accessed.


           Protected

Class (Inside this protected class), Package (Inside this protected package), Sub class (Protected class would be a super class and its inside's sub class) protected Classes, variables, methods would be accessed.

    Human                                          1.  Human.java                                                                Human.java                                        protected class Human                                                Human1.java                                                                                                                          Man                                                   Human 1.java                                                              Man.java                                             class Human 1 extends Human 

                                                                           //Access = Yes
 

2. Human.java                                     3.  Human 1.java
    protected void eat(){                            protected void run (){
    }                                                           }
    Human 1.java                                       Man.java - run ();
    class Human 1 extends Human{              
    eat ();                                                   //Access = No
    }
    
     //Access = Yes


            Default

  • Class (Inside defaulted class), Package (Inside defaulted package that the defaulted class is located) default class, variables, methods are accessed.



Human                                   1.  Human.java         
Human.java                                           default class Human{ 
Human 1.java                                        }
Man                                                       Human.java
Man.java                                               class Human 1 extends Human
                                                               
                                                             //Access = Yes 


2.   Human.java
      default class Human{
      (default) void run (){
      }
      class Human 2 extends Human{
      run(); 
      }
      }

     //Access = Yes


 Private

  • Only private class can access to the private class, variables, methods.
                                    private class Human{
                                    private String d;
                                    d = "ab12"
                                    private int a = 10;
                                    int b = 5;
                                    int c = int a + (private) int b; 
                                    private void run () {
                                    }
                                    }
                                    //Access = Yes
                                    class Man extends Human{
                                    (Can't access data of Human because its private)
                                    //Access = No
                                    }

-(5th Article)        
                                                                                                    


Thank You!
                                                                                                           


                                                                                                            
                     
                                                                                                       -Article about Java by Hirun Yapa



 

Comments

Popular posts from this blog

Interfaces

Super Keyword

Abstract Classes