Components of class | Class | Object | Getter | Setter | toString | Custom method

Let's discuss Components of class | Class | Object | Getter | Setter | toString | Custom method :

The concept of object oriented programming (OOP) often difficult for the newly coming students in the programming. Now the new and more efficient way of programming is OOP, in which deal with the objects of the classes and code our desired program. There are some basic components are  involve in the OOP just as class, objects, member variable or fields, getter ,setter, toString method, and custom methods. There are lot of theatrical examples and explanations of the OOP which hardly understand. Let's Programming bring a practical scenario of OOP with the basic components of class.We hope that you learn more by this.
  


(note: please copy it and paste on by one class on your compiler and learn the components of the custom class)

Class No: 1
(The custom class)

public class getter_setter {
       
        /*What is member variable ?
         Ans: The variable which can be used throw out the class. Here we declare the variable.
         Remember member variable should be private (also it could be public for direct access form other class)  */
       
        // member variable of the class
                private int a;
                private int b;
       
                /*What is constructor ?
                 Ans: The constructor is used to initialized the member variable(to give first time value to member variable).
                 Remember constructor should be public   */
               
                // constructor of member variable
                public getter_setter (int z,int y)
                {
                        // int z and int y are the parameter for constructor by which the member variables are initialized
                        a=z;
                        b=y;
                }              
               
                /*What is getter ?
                 Ans: The getter is used to get the value of member variable. The member variables are private thats why they are not
                 directly access from other classes, so we used getter to access / used or display the member variable
                 Remember getter should be public   */
               
                //getter for a
                public int getA() {
                        return a;
                }
                //getter for b
                public int getB() {
                        return b;
                }
               
                /*What is setter ?
                 Ans: The getter is used to change the value of member variable. First time member variables are initialized by constructor
                 after that we can change the value of member variables by setter.
                 Remember setter should be public   */
               
                //setter for a        
                public void setA(int a) {
                        this.a = a;
                }
                //setter for b
                public void setB(int b) {
                        this.b = b;
                }
               
                /*What is toString ?
                 Ans: The toString method is the method for display all the member variables in the form of String
                 Remember getter should be public   */
               
                //toString method
                public String toString ()
                {
                        return  a+ " & "+ b;
                       
                }
                /*What is custom method ?
                 Ans: The custom method are the user define method which is create as per the need by programmer.
                 It may be public or private according to need  */
               
                // custom method
                public void add() {
                       
                        System.out.println(a+b);
                }
                       
                public void min() {
                        System.out.println(a-b);
                }
               
                public void mul() {
                        System.out.println(a*b);
                }
               
                public void div() {
                        System.out.println(a/b);
                }
        }
       

 Class No: 2
(The main class)

public class main {

        public static void main(String[] args) {

                // creating object from class, 10 and 20 are the parameter which is given to constructor of getter_setter class

                /*What is class ?
                 Ans: The class is a big packet of codes which is written for reusibility of specific codes */
               
                /*What is object ?
                 Ans: The object is declare for use the class and its method */
       
                //object of the class getter_setter            
                getter_setter m= new getter_setter(10, 20);
               
                System.out.println("What is class, object, getter/setter and custom method ?\n");
                // only for the display the value of member variable a and b
                System.out.println("Getter of a(member variable): "+m.getA());
                System.out.println("Getter of b(member variable): "+m.getB());
               
                // it is setter for the member variable a,means that the initial value of the a(member variable is 10) is over write and have 100
                m.setA(100);
                // it is setter for the member variable b,means that the initial value of the b(member variable is 20) is over write and have 200
                m.setB(200);
               
                System.out.println();
                // only for the display the value of member variable a and b
                System.out.println("Value of a is change by setter: "+m.getA());
                System.out.println("Value of b is change by setter: "+m.getB());
                System.out.println();
                // if we call only the object "m" to display then what is show, only toString method
                System.out.println("This is only call the name of object: "+m);
                System.out.println();
                //what is toString and what is the output of toString methods.
                System.out.println("This is the toString method call: "+m.toString());
               
                System.out.println();
                //how we can use the methods / functions of others class by object
                System.out.print("addition of memebr variables: ");
                m.add();// method calling where "m" is the name of object and "add" is name of method
                System.out.print("Subtraction of memebr variables: ");
                m.min();// method calling where "m" is the name of object and "min" is name of method
                System.out.print("Multiplication of memebr variables: ");
                m.mul();// method calling where "m" is the name of object and "mul" is name of method
                System.out.print("Division of memebr variables: ");
                m.div();// method calling where "m" is the name of object and "div" is name of method
               
        }
}

Comments

Popular posts from this blog

Operating System | Best Definition of Opetating System

Umbrella activities in Software Engineering