DEV Community

Wathsara
Wathsara

Posted on

Java Object Oriented Programing (OOP)

What is OOP? Object-oriented programming (OOP) is a software programming model constructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods).
Object Oriented Programing features include the following.
1.Encapsulation: This makes the program structure easier to manage because each object’s implementation and state are hidden behind well-defined boundaries.*
2.Polymorphism: This means abstract entities are implemented in multiple ways.*
3.Inheritance: This refers to the hierarchical arrangement of implementation fragments.*
Object-oriented programming allows for simplified programming. Its benefits include re-usability, refactoring, extensibility, maintenance and efficiency.
What is a class in OOP? In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). Java provides us with its own set of pre-defined classes, but we are also free to create our own custom classes.

class Animal{

}
Enter fullscreen mode Exit fullscreen mode

The example above creates a Animal class. So in that we can declare the behaviors and the features of the Animals.
What is a constructor in OOP? A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values. In Java If we do not create a class constructor, Java provides one that does not allow you to set initial information.

class Animal {
    Animal(){
     }

}
Enter fullscreen mode Exit fullscreen mode

In the above example we created the Animal constructor. This constructor can be used when creating Animal class Instances. Also we can create the Constructors with the parameters too.

class Animal {
    int legs;

    Animal(int a){
        legs =a;
   }
}
Enter fullscreen mode Exit fullscreen mode

In the above example we created the instance variable named legs with the int data type. And also we created a constructor with a parameter which allows data types to be created with specified attributes. We add a int parameter named a in the class constructor. The value of the legs is equal the int value that is specified when we first use this class constructor.
Lets learn about the main method in Java. public static void main(String[] args) Java main method is the entry point of any java program. Its syntax is always public static void main(String[] args) . You can only change the name of String array argument, for example you can change args to s, myString or an variable as you wish.

 public class Animal {
    int legs;
    Animal(int a ){
    }

    public static void main(String[] args) {

    }

}
Enter fullscreen mode Exit fullscreen mode

When Java runs your program, the code inside of the main method is executes.
What is the use of Public in Main method? This is the access modifier of main method. It has to be public so that java runtime can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that main method has to be public. Let’s see what happens if we define main method as non public.
What is the use of Static in Main method? When java runtime starts, there is no object of the class present. That’s why main method has to be static, so that JVM can load the class into memory and call the main method. If main method won’t be static, JVM would not be able to call it because of there is no object of the class is present. Let’s see what happens when we remove static from java main method.
What is Void in Main method?Java programming mandates that every method signature provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void. This has been done to keep things simple because once main method is finished executing, Java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by Java Virtual Machine. If we try to return something from main method, it will give compilation error as unexpected return value.
What is a Object in OOP? In the class-based object-oriented programming paradigm, "object" refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures. Given below is a code that showing you how to create a class object in Java.

public class Animal {

    int legs;
    Animal(int a ){
        legs=a;

    }
    public static void main(String[] args) {
        Animal x = new Animal(4);

    }

} 

Enter fullscreen mode Exit fullscreen mode

In the example above, we create a Animal object named x. When creating x, we used the class constructor and specified a value for the required int parameter a. In there the no of legs has to the Animal x is 4.
What are methods? A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object is mostly made up of data and behavior, which form the interface that an object presents to the outside world. A method is a predefined set of instructions. Methods are declared within a class. Java provides some predefined methods available to all classes, but we can create our own as well.

public class Animal {


    int legs;
    Animal(int a ){
        legs=a;

    }

    void run(){

    }

    public static void main(String[] args) {
        Animal x = new Animal(10);

    }

}
Enter fullscreen mode Exit fullscreen mode

In the example above, we added a method called ruc. When the method is used, it will print out Animals Can Run. So now the problem is how to use the method in Java. to use the the method, we use the object that we created previously. So we have to call the method run using the object in the main method. the code given below shows how to do it.

public class Animal {


    int legs;
    Animal(int a ){
        legs=a;

    }

    void run(){
        System.out.println("Animals Can Run");

    }
    public static void main(String[] args) {
        Animal x = new Animal(10);
        x.run();

    }

}
Enter fullscreen mode Exit fullscreen mode

Methods are also like the Constructors we can create them with the parameters.
as a example

public class Animal {


    int legs;
    Animal(int a ){
        legs=a;

    }

    void run(int speed){
        System.out.println("Animals Can Run with the speed of "+speed);

    }
    public static void main(String[] args) {
        Animal x = new Animal(10);
        x.run(50);

    }

}

Enter fullscreen mode Exit fullscreen mode

This will give the output as Animal Can Run the speed of 50.
One of the major concepts in the OOP is Inheritance. what is inheritance? Inheritance refers to a feature of Java programming that lets you create classes that are derived from other classes. A class that's based on another class inherits the other class. The class that is inherited is the parent class, the base class, or the superclass.

 public class Animal {


    int legs;
    Animal(int a){
        legs=a;

    }

    void run(int speed){
        System.out.println("Animals Can Run with the speed of "+speed);

    }
    public static void main(String[] args) {
        Animal x = new Animal(10);
        x.run(50);

    }


}

class Dog extends Animal{

    public Dog(int a) {
        super(a);
    }
}
Enter fullscreen mode Exit fullscreen mode

In the example above, the extends keyword is used to indicate that the Dog class inherits the behavior defined in the Animal class. This makes sense, since a Dog is a type of Animal. In here we had to create a constructor of the dog class As the super class of the Dog class has a int parameter constructor.

Top comments (0)