Page 345 - CA_Blue( J )_Class10
P. 345

14                     ENCAPSULATION AND




                                                                             INHERITANCE














                          Learning Objectives



                     14.1 Encapsulation                                 14.2 Access Specifiers
                     14.3 Scope of Variables                            14.4 Inheritance



                 As you  have learnt,  that object-oriented programming (OOP) has four basic  principles,  which  are encapsulation,
                 abstraction, inheritance, and polymorphism. You have also learnt about abstraction and polymorphism in the previous
                 chapters of this book. Now, we will learn about encapsulation and inheritance in this chapter.


                     14.1 ENCAPSULATION
                 Encapsulation is one of the basic principles of object-oriented programming. It is a process that depicts the idea of
                 bundling data and functions together. It keeps both data and functions safe from outside interference and misuse, thus
                 leading to the important OOP idea of data hiding.
                 Hence, the wrapping up of data members and member methods together into a
                 single unit is called encapsulation.
                                                                                              Methods       Variables
                 A real-life example is a capsule which protects the drug inside it from getting
                 contaminated with the dust particles outside.

                 Example of encapsulation using class:

                                class encapsulation_example                                  Class
                                    {
                                        int a;
                                                                                            Data Members
                                        String n;
                                        void input (int a1, String n1)
                                        {
                                            a=a1;
                                            n=n1;
                                        }                                                  Member Methods
                                        void display ()
                                        {
                                            System.out.println (a);
                                            System.out.println (n);
                                        }
                                    }


                                                                                                                       343
                                                                                           Encapsulation and Inheritance   343
   340   341   342   343   344   345   346   347   348   349   350