DEV Community

Cover image for Object Oriented Programming in Java
vindhya Hegde
vindhya Hegde

Posted on

Object Oriented Programming in Java

Object-oriented programming helps us with data security, code reusability, and also helps us to improve code readability.
Let us see and Understand different Object-oriented concepts with code examples.

1.Abstraction

alt text
->Means showing only essential parts and hiding the implementation details.
->Only the functionality will be provided to the user,
in other words user will have the information on what the object
does instead how it does.
->Here we will be hiding the details of our class from the outside world.What happens within the class will not be known to the outside users.

abstract class Car
{
    abstract void run();
}
class Baleno extends Car
{
    void run()
    {
        System.out.println("drive car safely");
    }
}
public class Main
{

    public static void main(String args[])
    {
       Car c = new Baleno();
       c.run();

    }

}

Enter fullscreen mode Exit fullscreen mode

2.Encapsulation

alt text
->Binding variables and methods under a single entity.
->Or It is a mechanism of wrapping the data(variables) and code acting on the data(methods) together in a single unit. Data (variables) present inside class can be accessed only through methods of the current class.Therefore it is also known as data hiding,
To achieve encapsulation in java
->Declare the variables as private : if we declare variables as private then it will not be visible to the outside world.
-> If we want to access these private variables then,
Provide public setter and getter methods to modify and view the variables values.

class Student
{

    private String sname;

    public String getName()
    {
        return sname;
    }

    public void setName(String name)
    {
        sname = name;
    }

}

public class Main
{

    public static void main(String args[])
    {
        Student f1 = new Student();
        f1.setName("Vindhya");
        System.out.println(f1.getName());

    }

}
Enter fullscreen mode Exit fullscreen mode

3.Inheritence

alt text
->Acquiring properties of one class to another class.
->If we consider parent class and child class ,then child class
will acquire the Properties of Parent class.
->Parent class is also known as Base class and Super class.
Child class is also known as derived class and subclass.
->In general children will acquire the properties of their parent
that is example for inheritance

class Base
{
   void method1()
   {
       System.out.println("Parent class");
   }   

}
class Derived extends Base
{
    void method2()
    {
        System.out.println("Child class");
    }
}
public class Main{
    public static void main(String args[])
    {
         Derived d = new Derived();
         d.method1();
         d.method2();
    }

}
Enter fullscreen mode Exit fullscreen mode

4.Polymorphism

alt text
->The ability of an Object to take many forms
->Performing the same task in different ways.
->here task means method.Polymorphism is a combination of two
different words poly means many and morphs means forms.
->Finally polymorphism gives the meaning of many forms

class Multiply{
    static int multiply(int a,int b)
    {
        return a*b;
    }
    static double multiply(double a, double b)
    {
        return a*b;
    }
}

public class Main{

    public static void main(String args[])
    {
        System.out.println(Multiply.multiply(2,3));
        System.out.println(Multiply.multiply(1.1,2.1));

    }

}

Enter fullscreen mode Exit fullscreen mode

Thank You

Top comments (0)