Your ultimate guide

Java & C#_Object Oriented Programing

OOPS (Object Oriented Programming System):
  • It is based on the concept of objects that contain data and methods,
  • It works on the principles of:
    1. Objects
    2. Class
    3. Inheritance
    4. Polymorphism
    5. Abstraction
    6. Encapsulation




Objects: Objects can be defined as an instance of a class.
    Syntax: ClassName Reference_Variable = new Constructor()
    Example: (class Testing)
        Testing obj = new Testing();




Class: Class is a collection of variables and methods. A class can also be defined as a blueprint from which you can create an individual object.
    Types of Variables:
    1. Instance Variables
    2. Static Variables
    3. Local Variables

    1. Instance Variables:
        Declaration: Inside the class and outside the method (main()).
        Object Creation: We must create an object to access the instance variables.

        Example:
    1. public class InstanceVariables {
    2.    //Instance variable
    3.    int a = 1234;
    4.    public static void main(String[] args) {
    5.        //creating object
    6.        InstanceVariables obj = new InstanceVariables();
    7.        System.out.print(obj.a);
    8.    }
    9. }

    2. Static Variables:
        Declaration: Inside the class and outside the method, It will be declared using the "static" keyword.
        Object Creation: No need to create the object to access static variables.

        Example:
    1. public class StaticVariables {
    2.    //declaring the static variable
    3.    static int a = 1234;
    4.    public static void main(String[] args) {
    5.        System.out.print(a);
    6.    }
    7. }

    3. Local Variables:
        Declaration: Inside the method (Either main method or user-defined methods).
        We cannot access the local variable outside the method.

        Example:
    1. public class LocalVariables {
    2.    void test() {
    3.        int a = 10;
    4.        System.out.println("User Defined Method : " +a)
    5.    }
    6.    public static void main(String[] args) {
    7.        int b = 20;
    8.        System.out.println("Local Variable : " +b);
    9.        //We cannot access the local variable outside the method
    10.        //Error: System.out.println("test method variable : " +a);
    11.    }
    12. }

    Types of Methods:
        Methods: A set of statements that are grouped together to perform an operation.
    1. Predefined or Built-in or Standard-Library methods
    2. User-defined methods

    1. Predefined methods:
    • The methods that are already defined in the Java class libraries are known as Predefined methods.
    • We can directly use these methods just by calling them in the program at any point.
    • Some pre-defined methods are length(), equals(), compareTo(), sqrt(), print(), etc.,

    2. User-defined methods:
    • The method written by the user is known as a User-defined method.
      • User-defined methods are of two types:
        1. Instance Method or Non-static Method
        2. Static Method
        Instance Method: 
    • We can use both instance and static variables inside the Instance method.
    • The instance method can be invoked using an object of the class.
            Example:
      1. public class InstanceMethod {
      2.    //Instance method
      3.    //Method with returns values
      4.    public int addition(int a, int b) {
      5.        int result = a+b;
      6.        return result;
      7.    }

      8.    //Method without return values
      9.    public void display(int value) {
      10.        System.out.println("Output: "+value);
      11.    }

      12.    public static void main(String[] args) {
      13.        InstanceMethod obj = new InstanceMethod();
      14.        int add = obj.addition(10, 20);
      15.        obj.display(add);
      16.    }
      17. }

        Static Method: 
    • To create a static method in Java, you prefix the keyword 'static' before the name of the method.
    • Inside a static method, we are only permitted to use static variables.
    • We can not use instance variables inside the static method.
    • The static method can be invoked simply by calling the invoke method.
            Example:
      1. public class StaticMethod {
      2.    //Method with returns values
      3.    public static int addition(int aint b) {
      4.        int result = a+b;
      5.        return result;
      6.    }

      7.    //Method without return values
      8.    public static void display(int value) {
      9.        System.out.println("Output: "+value);
      10.    }

      11.    public static void main(String[] args) {
      12.        int add = addition(10, 20);
      13.        display(add);
      14.    }
      15. }



Next Page

No comments:

Post a Comment