Phase ii [oops concepts]

OOPS CONCEPTS

oops is a programming concepts that uses objects to design program 

  • Use real world entities such as : pen, dog, chairs, table and any thing etc
  • oops has become the preferred programming approach by the software industries, as it offers powerful way to cope with the complexity of real world.
Basic concepts of oops :-
pillars of object oriented programming :- 

class and object 

Class

A group of objects who has similar properties and behaviour is known as class.
  • class is a Template used to create object .
  • class is blueprint from which object created.
  • not a real-world entity.
  • does not occupy memory.
Class can contain :- 
  1. Data members
  2. method
  3. constructor
  4. nested class
  5. interface.
Syntax :- 

         access_modifier class class_name{
                                           //Data members
                                           //method
                                           //constructor
                                           //nested class
                                           //interface
                  }

Object 

Object is a Instance of a class(result) and a Real world entity.

Properties :- 
  • Behaviour- Functionality.
  • state- data or value.
  • identity- ID, Uniqueness, detect by JVM uniquely.
Syntax :-
  • Class_name variables(instance) = new class_name();

 Inheritance

When child (derived) class acquire the qualities and characteristics of parent(base) class is called inheritance. One of the most important concept of oops. 

Benefits :- 
  1. It makes easier to create and maintain applications.
  2. Reuse code (speed up implementation). 
Disadvantages :-
  1. Tight coupling
Types :-
  1. single inheritance
  2. multilevel inheritance
  3. hierarchical inheritance
  4. multiple inheritance
  5. Hybrid inheritance .

Single inheritance :- Single inheritance is a type of inheritance in which a child (derived) class inherits the properties and methods of parent (Base) class.










Code :- 

public class Gym { //parent class ( Derived)
    void dumbbell(){
        System.out.println("I am for chest press");
    }
}
class Tools extends Gym{ //child class inherit (extends) qualities of parent
    void chest_press(){
        System.out.println("For chest press we need dumbbell");
    }
    public static void main(String[] args) {
        Tools sc  =  new Tools();
        sc.chest_press();
        sc.dumbbell();
    }
}

multilevel inheritance :- Multilevel inheritance is a type of inheritance that allows a child (derived) class to inherit from a parent (base) class, which itself inherits from another parent(base) class .This means that a child(derived) class has access to the members of both the parent (base) class and the grandparent class.

code :-

public class GrandMother {
    void G1(){
        System.out.println("I am grandmother");
    }
}
class mother extends GrandMother{
    void G2(){
        System.out.println("I am mother");
    }
}
class child extends mother{
    void G3(){
        System.out.println("I am child");
    }
    public static void main(String[] args) {
        System.out.println("----------------Grandparent class output----------------------");
        GrandMother c = new GrandMother();
        c.G1();
        System.out.println("-----------------parent class output----------------------");
        mother s = new mother();
        s.G1();
        s.G2();
        System.out.println("----------------child class output----------------------");
        child sc = new child();
        sc.G1();
        sc.G2();
        sc.G3();
    }
}

Hierarchical inheritance :- When a parent(Base) class is inherited by many child (Derived) class.


Code :-

public class GrandMother {
    void G1(){
        System.out.println("I am grandmother");
    }
}
class mother extends GrandMother{
    void G2(){
        System.out.println("I am mother");
    }
}
class child extends GrandMother{
    void G3(){
        System.out.println("I am child");
    }
    public static void main(String[] args) {
        System.out.println("----------------Grandparent class output----------------------");
        GrandMother c = new GrandMother();
        c.G1();
        System.out.println("-----------------parent class output----------------------");
        mother s = new mother();
        s.G1();
        s.G2();
        System.out.println("----------------child class output----------------------");
        child sc = new child();
        sc.G1();
        sc.G3();
    }
}


note :- We do not use multiple inheritance in Java because it creates ambiguity problem.





Polymorphism

Polymorphism is a core concept of oops in which we can use similar looking things in different ways.



In this example, this child is going to the gym, the same child is also eating food, the same child is also going to school and the same child is also taking sunlight, then what is explained by polymorphism so when same thing doing different things is called polymorphism.

Types of polymorphism: 


  • Compile-Time Polymorphism (Static Polymorphism:
  1. Also known as static polymorphism.
  2. Achieved through function overloading or operator overloading.
  • Method Overloading:
  1. When multiple functions have the same name but different parameters (number or type of arguments).
  2. Some Important points to follow : Same class ,same name, different parameter.

    Example: 

    public class Polymor {
        //method overloading (compile time)
        public void manvik(int i){
            System.out.println("Hi i am first manvik ");
        }
        public void manvik(){
            System.out.println("Hi i am Secound manvik ");
        }
        public static void main(String[] args) {
            Polymor sc = new Polymor();
            sc.manvik();
            sc.manvik(5);
        }
    }

     Output: 

    Hi i am Secound manvik 

    Hi i am first manvik 

     

  • Operator Overloading (Note: Java doesn’t support operator overloading). 
  • Runtime Polymorphism (Dynamic Method Dispatch):
  1. Also known as dynamic polymorphism.
  2. Resolves function calls to overridden methods at runtime.
  3. Achieved through method overriding.
  • Method Overriding: 
  1. When a subclass provides a specific implementation of a method that is already defined in its superclass. 
  2.          Some Important points to follow : different class ,same name, different parameter.

     Example : 

    public class Polymor {
        public void manvik(int i){
            System.out.println("Hi i am first manvik ");
        }
    }
    class Poly_2 extends Polymor{
        public void manvik(){
            System.out.println("Hi i am Secound manvik ");
        }
        public static void main(String[] args) {
            Poly_2 sc = new Poly_2();
            sc.manvik();
            sc.manvik(5);
        }
    }
Output: 

Hi i am Secound manvik 

Hi i am first manvik 

Abstraction

Abstraction is a concept which provide feature where we show essential detail and functionality to user and non essential implementation detail are not displayed to user.


So inside this image there is an automobile engineer and a driver who is sitting inside the vehicle.
The driver in this image only knows the steering, the gearbox and some of the features of the vehicle.
And the one who is an automobile engineer knows the specifications of all the engines, he also knows the steering, he knows everything about the car. A driver who knows only the steering and a few features of his car, but an Automobile Engineer knows all the features of his car, steering engine and every specification. In the same way, a programmer knows the entire code but shows only what is essential to the user.

  • We can Achive Abstraction in to ways: 
  1. By using Abstract class
  2.  By using Interface
  • Abstract class :

  1. An abstract class is a class that cannot be instantiated directly (i.e., you cannot create objects of an abstract class). 
  2. It serves as a blueprint for other classes. 
  3. Abstract classes can have both abstract methods (methods without a body) and regular methods (with a body). 
  4.  Abstract class can achive o or 100 % Abstraction. 
  5. To use an abstract class, you need to create a subclass (a class that inherits from the abstract class) and provide implementations for the abstract methods
  •  Interface :
  1. An interface is a reference type in Java, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot have constructors. Classes implement interfaces, thereby inheriting their abstract methods. 
  2. It can be used to achieve loose coupling and An interface in Java is a blueprint of a class. Java Interface also represents the IS-A relationship. 
  3. Interface can achive 100 % Abstraction. 

  4.  Encapsulation


    Encapsulation in Java is one of the fundamental principles of Object-Oriented Programming , which helps in bundling data (variables) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some of the object's components, providing control over the values stored in these variables.
     
    In Java, encapsulation is implemented using access modifiers and getter and setter methods.

  • Key Points of Encapsulation:
  1. Data Hiding: The primary idea of encapsulation is to hide the internal implementation of the class and expose only the necessary methods to access or modify data. This is done by marking the variables of the class as private, preventing other classes from directly accessing them.
  2. Control Over Data: By controlling the access to variables through getter and setter methods, we ensure that any changes to the variables go through validation. This helps maintain the integrity of the data.
  3. Security: Encapsulation allows an object to hide its internal data from unauthorized access and modification, thereby enhancing security.
  4. Flexible and Easy Maintenance: Since the implementation is hidden, it becomes easier to modify or change it later without affecting the other parts of the code that depend on it.
  •  How Encapsulation is Achieved in Java:
  1. Private Data Members: Data members (variables) are declared private, which makes them accessible only within the class.
  2. Public Getter and Setter Methods: Public methods (getters and setters) are provided to access and update the value of private variables.
Example: 


// Class with encapsulated data
public class Employee {
    // Private data members
    private String name;
    private int age;

    // Public getter method for 'name'
    public String getName() {
        return name;
    }

    // Public setter method for 'name'
    public void setName(String name) {
        this.name = name;
    }

    // Public getter method for 'age'
    public int getAge() {
        return age;
    }

    // Public setter method for 'age'
    public void setAge(int age) {
        if(age > 0) { // Validation to ensure age is positive
            this.age = age;
        } else {
            System.out.println("Age cannot be negative.");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of Employee
        Employee emp = new Employee();

        // Setting values using setter methods
        emp.setName("John");
        emp.setAge(30);

        // Getting values using getter methods
        System.out.println("Employee Name: " + emp.getName());
        System.out.println("Employee Age: " + emp.getAge());
    }
}


  • Benefits of Encapsulation:
  1. Enhanced Security: Sensitive data can be hidden from outside classes, preventing unintended access or modification.
  2. Code Flexibility: The internal implementation can be changed without affecting other parts of the program.
  3. Data Integrity: Encapsulation allows for validation checks before modifying the data, ensuring that it remains valid.

 

 


Comments