Weekly update

This week i learned about Java as it happens to be in my course this semester.

OOP(object oriented programming)

  • Java is an object oriented programming language.

  • We create classes(Blue print of objects),the instances of classes are objects.

  • OOP promotes code reusability, maintainability, and scalability (by providing a structured approach to modeling real-world entities and their interactions.

Further,we have 4 concepts of oops(Abstraction,Encapsulation,Polymorphism,Inheritance**)**

  1. Abstraction

    Abstraction is the process of simplifying complex systems by focusing on essential characteristics while hiding irrelevant details.In OOP, abstraction allows programmers to define abstract classes or interfaces that specify a set of methods without implementing them. This enables the creation of generic templates for classes, promoting code modularity and flexibility.

  2. Encapsulation

    Encapsulation is the bundling of data and methods that operate on that data into a single unit, known as a class. Through encapsulation, the internal state of an object is protected from external manipulation, and access to the object's data is restricted to methods defined within the class. This enhances data security, promotes code maintainability, and facilitates the implementation of the "information hiding" principle.

  3. Inheritance

    Inheritance is a mechanism that allows a class (subclass) to inherit properties and behaviors from another class (superclass). Subclasses inherit the attributes and methods of their superclass, enabling code reuse and promoting the hierarchical organization of classes. Inheritance facilitates the implementation of the "is-a" relationship, where a subclass is considered to be a specialized version of its superclass. It fosters code modularity, reduces redundancy, and enhances the extensibility of software systems.

Polymorphism

Polymorphism allows you to write functions or methods that can work with objects of different classes, and these objects can behave differently based on their individual implementations of the same method. This makes your code more flexible and adaptable to different situations.


The syntax is rather easy.But you have to make objects to access classess and their methods,the first case i encountered was taking input through scanner class.

//Wap to find area and perimeter of a rectrangle
import java.util.Scanner;
class rect{
public static void main(String [] args){

    Scanner sc = new Scanner(System.in);// We created an object sc

    int length,breadth;
    System.out.println("Enter the length and breadth");
    length = sc.nextInt();               //Using sc to access methods of 
    breadth= sc.nextInt();               //Scanner class

    int area = length*breadth;
    System.out.println("Area is "+area);
    int peri = 2*(length+breadth);
    System.out.println("Perimeter is "+peri); 
}
}

As you can see,to access the methods of a class you need an (instance of the class)/object.

Exception-the class is static,then it does not need an instance.

If you need to make a function/method and pass its reference in the main method or any other method,make sure it is a static method if you do not want to create an instance.Or simply create an instance in the main method and access the method.