OOPS CONCEPT

  • By
  • October 1, 2021
  • HTML & CSS

There are some OOPS concepts which we have seen in C++. All are available in java programming language too and that’s makes java as purely object oriented programming language.

  1. Class and Object
  2. Encapsulation
  3. Abstraction
  4. Inheritance
  5. Polymorphism
  6. Class and Objects

Class is an entity which is defined by the user. All data/information and methods are wrapped inside a class. It is group of multiple objects; almost everything in java is an object. All programming code and data resides within objects and classes.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

The data and functions are written within a class are called as members of a class. The variables are called as instance or class variables and methods are called as instance methods.

In java data items are called as field and functions are called as methods.

To define a class in java we should follow the following format of class.

Class class_name{

   type instance_variable1;

   type instance_variable2;

  type instance_variable n;

return_type method_name1(parameter list)

{

    Body of method;

}

 

return_type method_name2(parameter list)

{

    Body of method;

}

return_type method_name n(parameter list)

{

    Body of method;

    }

}

No semicolon after curly brackets. Class_name should be valid java identifiers. All the rules which are applicable for variables name declaration. Same rules are applicable while writing class name and method name.

 *Field(instance variable) Declaration:

The data or variables define inside the class are called as instance variables. These variables are called as instance variables because they are created whenever an object of a class gets instantiate.

We can’t use same data for all the objects defined inside a class.

Dot Operator is used to access the instance variables from outside the class with the help of object name.

*Method declaration

A class needs to have some methods to act on the variables define inside a class. Methods are used for manipulating the data/variables of class. Methods are declared inside body of the class immediately after instance variables declaration.

-General form of method declaration:

returntype method_name(parameter_list)

{

         body of method;

}

-Method has four parts:

  1. 1. Return type :

Return type specifies type of data return by method. If method is not returning any value then it should be return as a void.

  1. Method name: 

Method name is unique  name in class which is given by the user to the method.

  1. Parameter list

 Parameter list consists of argument type and name on which method has to operate.

  1. Body of method

The body of method describes the action to be performed on data.

*Steps for creating object in java

  1. Declare Object

Just like variable declaration, we have to declare object in class with its class type

For example,   Society saikrupa;

Where, Society is a class name and saikrupa is an object of the class society

  1. Instantiate the object

Just like variable initialization, we can assign the object reference to the variable by using new operator. The new operator dynamically allocates memory for an object.

For example,

saikrupa = new Society();

Where, Society() is a default constructor which initialized to object with value 0.

*Program that shows the use of class and object

class Society

{

   int nos_of_flats;      //instance or class variables declaration

   String society_name;

   void getData(int n, String s) //instance method for accepting data from user

     {

          nos_of_flats =n;

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

          society_name=s;

   }

   void displayInfo() //instance method for displaying the data for society

     {

      System.out.println(“No of flats in ”+ society_name+ “society is : “     +nos_of_flats);

   System.out.println(“Society name is :   ”+nos_of_flats);

     }

}    //class completed

Class MainDemo   //class to define main()  method

{

   Public static void main(String args[])

   {

         Society saikrupa;  //object declaration

         saikrupa = new Society();  //object instantiation

         saikrupa.getData(55,”saikrupa”); //initializing instance variables by        

                                                                                 //Calling instance method

        saikrupa.dispalyInfo();

          }

}

  1. Encapsulation

Encapsulation is the process of wrapping instance variables and instance method together inside a class. Encapsulation is a way of providing security to the data by declaring instance variable as a private. Once we define instance variables with private access modifiers that private data can’t be accessed using normal instance methods. We need to define setters and getters for accessing those private fields. 

Setters and getters are two traditional methods of java programming language. Where setters are used to set or update the values for the variables and getters are used to get or retrieve the values of variables which is set by the setters

 We can make the class read only in java by defining only getters in the class. Means if you are not specify the setters in the class then no one is attempted to update/perform writing on the values of the variables.

Similarly, you can make the class write only by defining only setters in the class.

 *Benefits of Encapsulation

  1. The instance variables of class can be made read only or write only through setters or getters.
  2. Its way to achieve data hiding in java because other classes will not be able to access the data through the private data members.
  3. Std IDE providing the facilities of generating setter and getters in the class so it is easy and fast to create encapsulated class in java.

*Program that shows how to achieve encapsulation in java

package encapsulation;

class StudentInfo

{

    private String name;    

     private int fees; //the data which decalred as private can’t be                                                                    //accessible outside the class

   //setters and getters for accessing private fields

public void setFees(int fees)

{

   this.fees=fees;

}

public int getFees()

{

    return fees;

}

public void setName(String name)

{

      This.name=name;

}

public String getName()

{

      return name;

}

public class EncapsulationDemo

{

       public static void main(String args[])

       {

          StudentInfo s1= new StudentInfo();

                       s1.setFees(20000);

        System.out.println(“student fees = ”+s1.getFees());

                             s1.setName= “abc”;

     System.out.println(“student name is = ”+s1.getName());

    }

}

* Program that shows how to make our class as read only class

package encapsulation;

class Circle

{

     private  double pi=3.14;

     public double getPi()

     {

       return  pi;

   }

}

public class ReadOnly

{

     public static void main(String args[])

    {

   Circle c1= new Circle();

 //       c1.setPi(4.14); // //ReadOnly  class is read only i.e we can’t perform   // any  other operation in this class because setter method is not availble here // to  set/change the value. It is readable only.

     System.out.println(c1.getPi());

   }

}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

* Program that shows how to make our class as write only class

package encapsulation;

class Circle

{

     private  double pi=3.14;

     public void setPi(double pi)

     {

         this.pi=  pi;

   }

}

public class WriteOnly

{

     public static void main(String args[])

    {

   Circle c1= new Circle();

   c1.setPi(4.14);

 System.out.println(c1.getPi());

//WriteOnly  class is write only i.e we can’t perform any     other operation in this class because getter method is not availble here to get the value. It is writeable only.

           }

}

  1. Abstraction

Abstraction is nothing but providing only essential data to the user and hiding remaining all data. In the process of abstraction user knows only functionality of something, he don’t know about internal implementation of that thing.

E.g whatsApp. When we want to send something on whatsApp, may be images,audio,video. We just attached that attachment in chatbox of that particular user and just click on send button, within a seconds that message is send to the receiver, means we just know that whatsApp is media through which we can send something to other person. We don’t know how it could send means when we say send where our message goes, on what server it gets received how it goes to appropriate user. In short, we know only functionality of whatsApp and don’t know how it gets implement. That is nothing but abstraction.

We can achieve abstraction in java by declaring class as a abstract class by using abstract keyword and we can make a class abstract only when it consists at least one single abstract method(the method which has only declaration and don’t have any implementation body). We can provide the implementation of that abstract method in another normal class which inherits that abstract base class.

Abstract class consists of abstract as well as concrete method (the method which has implementation body/definition where it is declared). We can’t instantiate(object creation) abstract class.

*Why we can’t create the object of abstract class

Because, abstract class consists of abstract method which as no implementation body, if you create an abstract class then you have to call that abstract method as well with that object, which will raise an error because abstract method has no definition.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

//Program to show the use of abstraction

package abstraction;

abstract class GeetaApp1                                //this GeetaApp1 class consists of           

                         //abstract method therefore we declared that class as abstract

{

         public abstract void call();  //we don’t know the implementation of this //method thats why we make that methods as abstract. And abstract methods //can be implemented in another class

         public abstract void video();

         public abstract void sound();

 

         public void gallery()  //concrete method=> the method which has                                                                                ///implementation body

         {

           System.out.println(“here is my gallery..”);

         }

}  //GeetaApp class complete

 

 

 

abstract class RahulApp1 extends GeetaApp1 //we made this class as       

                                                                             //abstract because we  

                            //extends/use GeetaApp1 class here which is also abstract

{

        

         public void sound()  //concrete method

         {

                 System.out.println(“welcome to the sound folder”);

         }

        

}

 

 class SanjayApp1 extends RahulApp1 //this class is not declared as abstract

                                        //because we extends RahulApp1 here which does

// not consists any abstract method so no need to make this class abstract

{

         public void call()

         {

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

                     System.out.println(“are u trying to call someone..?”);

         }

         public void video()

         {

                     System.out.println(“Are u exicited to watch my video”);

         }

}

public class MainAbs {

public static void main(String[] args) {

                     // TODO Auto-generated method stub

//here we can create reference variable i.e a of RahulApp1 class and object of //SanjayApp1 class

         RahulApp1 a= new SanjayApp1();             

                     a.call();

                       a.video();

                       a.sound();

                       a.gallery();

    } 

}

To Be Continued……..

 

Author:-

Pooja Nandode-Bhavsar

Call the Trainer and Book your free demo class for now!!!

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

 

  

 

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*