JAVA OOPS

OOPS(Object Oriented  Programming Language) Concepts

          Java is an OOPS Language. So it should satisfy all the oops concepts which we are discussing below.


Encapsulation :

           Encapsulation means hiding the data while accessing the object. We should not expose the data which means state  of an object This is the key rule of OOPS.

      To achieve Encapsulation in JAVA we can declare any variable with access modifier private by default the access modifier is public which means any variable which have reference of an object can access the state. We have define the variables of an object like below.


     class Student {

        private String name;

        private String rollNo;

    }  

    


             In the above object we declared 2 instance variables name and rollNo and give them the access modifier private to achieve data hiding . So here we will get one question that if we restrict the variables From accessing then how any reference variable can access them?

             Below is the answer  JAVA gives us methods which popularly called setters and getters these should have public access modifiers. So we will write setters and getters of variables of an object and call these methods using reference variables of Student Type and set or get the variable. Here we will get one question that still we are accessing the variables from reference variables using setters and getters right? That’s what java gives beautiful thing here for us. If we define the setters and getters of variables the we can restrict what type of data to set for any variable . Like rollNo should not contain alphabets then we will add condition in the setter then setter will help us in restricting the data.


import java.util.regex.*;


class Student {

     private String name;

     private String rollNo;



      public    String getName(){

           return name;

     }

  

     public void setName(String name) {

        name  = name;

    }


      public    String getRollNo(){

           return rollNo;

     }

  

     public void setRollNo(String rollNo) {

        if(isAllDigits(rollNo){

           rollNo  = rollNo;

         }

        

    } 


   public boolean isAllDigits(String str) {

               String regex = "\\d+"; // Matches one or more digits

                Pattern pattern = Pattern.compile(regex);

            Matcher matcher = pattern.matcher(str);

              return matcher.matches();

     }

}



               In the above example we restricted the rollNo for only digits  that is why setters and getters are useful to define our object correctly and secured. Here we will get another doubt that name is not retracting anything and setters and getters of name are extra piece of code. That’s what java is also used for enterprise applications so in enterprise applications we wrote lot of business logic and features will change frequently . Suppose after 1 month the client came to developer and asked to restrict the name with some regex expression then if we don’t have setter and getters the complete code which are accessing the name variable directly while disturb we have use the getter and setter all the places again that is why we define all variables with private and use getters and setters. In above example if we use setter and getter from before only then any requirement coming to restrict the name will end by adding just on extra if condition in the setter without disturbing the complete code.



Inheritance :

                Inheritance means passing behaviour and state of one class to another class which is in the hierarchy .,like children’s are getting behaviour from the parents . Similarly in JAVA we can achieve this using extends keyword when we  extends class A in class B the class A is super class means parent class and class B is child class which is inheriting the behaviour from its parent. 


               To check whether  a class is inheritable to another class we can ask one question like class B  IS-A class B for example Cat IS - A Animal . Here Cat can inherits from his parent Animal means cat will do all things what Animal can do.


               To check HAS-A relationship  we can check class B HAS-A class-A like Bathroom HAS-A Shower that means Bathroom and Shower classes had HAS-A relationship means in class Bathroom there is instance variable of type Shower.


             These IS-A and HAS-A relationships will help us to design well reusable code in JAVA.



             The main advantages of Inheritance is reusable code and code maintainability to write any new functionalities without breaking the old functionalities.


             Here we have a problem that when  we say Animal class  it is not good to ask what is the size of Animal or How Animal barks but in above scenario if we create Animal as a super class JAVA will allow us to create object for Animal class which is of no use because Animal class is generic class for all Animals classes like Dog ., Cat etc..    To solve this problem JAVA introduced abstract classes .   


            Abstract classes means classes which have at least one abstract method(Its not mandatory to have one also). The Main Moto of abstract classes is to not to allow developer to create object for  abstract classes. That’s what exactly JAVA gives us here if we declare any class with abstract then JAVA will allow developer to create instance . We can mark the methods as well as an abstract if we mark any method as abstract then we should define the body of the method in this method . The body of method should be implemented by the first child class which is extending the super class.   The Abstract method declaration is like below :

                public abstract add(int I);  ends with semicolon.

 


           We can define the Muti level Inheritance also in JAVA like if  class B is extending class A and class C is extending class B the we can say class C have all properties of class B and class A ., In Other words class C IS-A class B and class C IS-A class A. But for class B its only one level.


            In Java all the classes by default extends Object class which is in java.lang class and if any class having any super class the JAVA will not add extends to child because child can inherit all properties of Object class from Parent class the top level Parent Class is enough to extend the  Object class. Object class have many methods  like getClass., hashCode , equals, toString etc..


           In Java there is a rule for what to inherit from parent class  like only methods or variables having public  or any other access specifier which allows package level can only be inherit to child classes . If any method having private access modifier then the child class will not inherit that method.


                There is one Problem in JAVA Inheritance called Diamond Death Problem(DDP) because of this problem JAVA will not support for multiple inheritance means one child extending 2 parent classes . This is not allowed in JAVA Due to ambulance  . If 2 parent classes same method name and everything same which method should  Child class Inherit.

                  

                 To Avoid above problem JAVA introduced interfaces. Interfaces are full abstract classes all the methods are by default abstract and public.  The class which implements  the interface they have to give definition to the methods this way JAVA solved DDP .



 Polymorphism. :

                  Polymorphism means having many forms. This can be achieved by inheritance when 2 class are in inheritance Then we can declare any variable as parent class and assign object of any type which the parent class Is inherited . In this way same behaviour having many forms. Like Animal is the parent class and Dog and Cat is the child class the if we create a reference variable of type Animal ., Note that we can declare variable of type abstract class but we can not instantiate them.  Now the method Bark is there in both Cat and Dog . Both classes can override the parent class behaviour if parent class Is not satisfying this is called method overriding. Now we created a reference variable of type Animal and for example if we assign an instance of Dog object the invoke the bark method on the variable then it will call bark method of Dog class if we assign Cat object then it will automatically call bark method of Cat Class. See the magic of polymorphism we created only one variable type which is generic like Animal and we can assign any object which is extending Super class.



              There is a limitations on overriding  the method .if the method signature is final the we cannot override them like getClass of object is final that’s why we cannot override the method.



                The child class can add any extra methods in the class which are not defined in the parent. 

So if we create any variable of type Parent then we don’t have access to the child methods unless we cast the variable with exact and assign them to variable of the child type.

       

                We can also do method overloading like the method name is same but signature will change like args and return type or only arguments . If we change only return Type it is not legal method overloading in JAVA


               As Object is a generic parent of all classes we can define any variable with Object type . But this is not recommended as Java is type strict programming language if we define any variable generically as Object then JVM might throws class CastException at runtime


 

Comments

Popular posts from this blog

JAVA Collections

JAVA Annotations