Object-Oriented Programming

Object-Oriented Programming is a very vital topic in java as well as any other high-level programming language. The main purpose of object-oriented programming is to increase the flexibility and maintainability of programs.

The core concepts are:

  1. Class
  2. Object
  3. Method
  4. Constructor
  5. Inheritance
  6. Encapsulation
  7. Polymorphism
  8. Abstraction
  9. Interface

1. Class:
A Class is a blueprint that states the properties and behavior of an object.
Example:

class Car{
  String brand= "Toyota";
  String model= "Premio";

  public static void main(String[] args) {
    Car myCar= new Car();
    System.out.println(myCar.brand);
    System.out.println(myCar.model);
  }
}

2. Object:
An Object is an instance of a class. In the above example, myCar is an object. An object can be initiated by using new keyword before the class. An object can contain both properties and function.

3. Method:
Method is the activity of an object. Different object can have different activities.
Method can be called by 2 ways.
a) object.method() and
b) class.method()
When object is initialize, then method should be called by object.method() approach.
When method need to called from class directly, then “static” keyword need to be used before method name. Generally class.method() approach applied when there is no constructor.

Example:

a) object.method() approach:

class Main {
    public static void main(String[] args) {
        Human human=new Human();
        human.sing("Hrittik");
        human.dance("Katrina");
    }

}
class Human{
    public String name;
    //method
    public void sing(String name){
        System.out.println(name+" can sing");
    }
    //method
    public void dance(String name){
        System.out.println(name+" can dance");
    }
}

b) class.method() approach:

class Main {
    public static void main(String[] args) {
        Human.sing("Hrittik");
        Human.dance("Katrina");
    }

}
class Human{
    public String name;
    //method
    public static void sing(String name){
        System.out.println(name+" can sing");
    }
    //method
    public static void dance(String name){
        System.out.println(name+" can dance");
    }
}
Output: 
Hrittik can sing
Katrina can dance

4. Constructor:
Constructor is a special method in java. When method name is same as Class name, then its said as constructor. Constructor has no void or return type. It is used to initialize the objectโ€™s state

When is a Constructor called ?
Each time an object is created using new() keyword at least one constructor (it could be default constructor) is invoked to assign initial values to the data members of the same class.

There are two types of constructors in Java:
1. Default constructor
2. Parameterized constructor

1. Example of default constructor:

class Main {
    public static void main(String[] args) {
        new MyConstructor();

    }

}
class MyConstructor{
    public MyConstructor(){
        System.out.println("Calling default constructor");
    }
}
Output: Calling default constructor

2. Example of parameterized constructor:

class Main {
    public static void main(String[] args) {
        new MyConstructor("calling default constructor");

    }

}
class MyConstructor{
    public MyConstructor(String data){
        System.out.println(data);
    }
}
Output: calling default constructor

5. Inheritance:
Inheritance is a process where a class can acquire the method and properties of another class.
The class which inherits the properties of other is known as subclass and the class whose properties are inherited is known as parent class.

Example:

class Main {
    public static void main(String[] args) {
        Animal animal=new Animal(); //Animal referenced and animal object
        animal.run();
        Animal chitah=new Chitah(); //Animal referenced but chitah object
        chitah.run();
        Animal human=new Human(); //Animal referenced but human object
        human.run();

    }

}
class Animal{
    public void run(){
        System.out.println("Animal can run");
    }
}
class Chitah extends Animal{
    public void run(){
        System.out.println("Chitah can run fast");
    }
}
class Human extends Animal{
    public void run(){
        System.out.println("Human can run slower");
    }
}

Output:

Animal can run
Chitah can run fast
Human can run slower

6. Encapsulation:
Encapsulation is a way to data hiding process where the variables of a class are hidden from other classes and can be accessed only through the defined public methods that are known as getters setters.

Example:

class Main {
    public static void main(String[] args) {
        Student student=new Student();
        student.showData("Babul","20");

    }

}
class EncapTest{
    private String name;
    private String age;
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getAge(){
        return age;
    }
    public void setAge(String age){
        this.age=age;
    }

}

class Student{
    public void showData(String name, String age){
        EncapTest encapTest=new EncapTest();
        encapTest.setName(name);
        encapTest.setAge(age);
        System.out.println(encapTest.getName());
        System.out.println(encapTest.getAge());
    }
}

The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred to as getters and setters.
In the above code, EncapTest class is used in Student class and student properties are set to Student class. Finally, user input is taking from the main method.
Although student name and age properties are private in EncapTest class, those can be accessed throw the public getter setter from Student class.
Therefore, any class that wants to access the variables, need to access them through these getters and setters.

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *