OOPs Concepts

Language Elements

Exceptions

Java String Class

Java Array Class

Collections

Java Example Codes

Core Java Interview Questions

Object Oriented Concepts in Java (OOPs Concepts)


Object oriented programming uses Classes and Objects to design a program, using core concepts of Abstraction, Encapsulation, Polymorphism, and Inheritance.

Abstraction

In Java, Abstract Classes and Interfaces are used to achieve abstraction, hiding any complex implementaion details and exposing to external classes only necessary information. An abstract class is declared with key word abstract, and can contain one or more abstract methods (just names), and can as well contain regular methods, with all implementation details. For any use, such class should be extended and abstract methods need to be implemented. An Interface provide complete abstraction for all the methods. Only method names and fields (or variables) are declared. Interface needs to be implemented by a class, to make use of it.

As an example of abstraction, consider when we place an order for the laptop. We are here not interested in understanding the fine details of the implementation of each and every component inside the laptop. Those technical details and all implementation information is relevant for engineers of manufacturer who construct it, and is abstracted from us. We are simply interested in some selected features laptop need to have, which are important for our use.

Encapsulation

Encapsulation controls the access, modifications and use of code declared in a class, by other code from outside the class. Encapsulation in Java is achieved using access modifier private, protected, public and default (without any declaration). Encapsulation can be for a class or interface or one ore more it field and/or methods delcarations.

Use public for class names, fields and methods which can be accessed from inside and outside that class. Protected for classes, fields and methods that can be accessed from inside class, classes in same package and subclasses in different package. Private for classes, fields and methods which are only accessible within the class they are declared. Default classes, fields and methods are those declared without any key word and are accessibly only inside of class and by classes within the same package.

We can create fully encapsulated class by making the entire data member as private and creating getter and setter methods to access that data member. POJO class is the best example of fully encapsulated class.

Polymorphism

Polymorphism is the ability of a method to behave differently ( multiple implementations), depending upon the object calling it. Different type of polymorphism in java include:

  • Method Overloading (compile time polymorphism)

  • Two or more methods of same name in a base class, but the argument list or parameters are different.

  • Method Overriding (run time polymorphism)

  • Child class has the same method as of base class, and it overrides the parent class method without modifying the source code of the base class.

    Inheritance

    Inheritance is a new class derived from an already defined class. It allows to reuse code from an existing class by the new derived class, which inherits the states and behaviors from the base class, and can add its own additional variables and methods. Java does not support multiple inheritance, i.e. a subclass can have only one super class (or base class).

    In Java, inheritance is implemented using the extends keyword to create a subclass that inherits the properties and behavior of a superclass.





    Copyright © by Zafar Yasin. All rights reserved.