OOPS : Feature of Object Oriented Programming

  1. Inheritance
  2. Encapsulation
  3. Polymorphism or Dynamic Method dispatch
  4. Abstraction

Concept :-

Inheritance:  It is ability to create class which can share state(attribute , behaviour and functionality) of an existing class.

Basic Understanding :-

 Is it possible to override main method of any class?

Explanations :- No because if you want to override any method than it should not be static and main method is basically the entry point for JVM to execute that is why it is declared as

public static void main (String[] ….)

It is void because JVM will not responsible for returning anything.

Encapsulations (Data Hiding):-

Encapsulation is basically a style to hide the data.

How can I define Encapsulation in Java?


There are many ways we can define encapsulation two main popular way are as follows :-

1 . We should define all the attribute as private and then access it through getter/setter. The object should implement Serializable interface because usually these objects are used to carry data set from one layer to another layer.

2 . We should define all the attribute as public and no method at all. The object should implement Serializable interface because usually these objects are used to carry data set from one layer to another layer.

Case Study

Above we have mentioned two way to implement encapsulation the question is in which scenario we can use 1st Option and in which scenario we should have to use 2nd Option.

Solution : Try to create same object with Serialization property and check with the size of the object, Other thing is that consider Security and multithreaded environment.

Polymorphism(Dynamic method dispatch):-

The java method call is resolved at runtime. In Polymorphism method name should be same.

Remember following points whenever we are implementing Polymorphism :-

  1. Overloaded methods must change the argument list (either number of argument or data type)
  2. Overloaded methods may or may not change return type
  3. Overloaded methods may or may not change access modifier
  4. Overloaded methods may or may declare new or broader checked exceptions (So in 1st method methodA i am throwing NullPointer than in 2nd method i may throw RuntimeException if required)
  5. Another point to remember is that method can be overloaded in the same class or in the subclass


Abstraction is basically act of representing essential features without including the background details or explanations which may indicate that how should it work. In abstraction the underlying implementation is not known during design phases.

Approach to design any Abstraction:-

It is always advisable to define abstraction as a Subsystem which can be come up as a Component during implementation phase. For example :-

Suppose our System has a requirement to connect ERP System for retrieving Sell’s or manufacturing related reports. In current state I don’t know about the complete details so for this particular functionality is abstraction for me.

I will define a Subsystem with exposed interface. The exposed interface should define the functionality of known feature and as it evolve it will come up with a complete component for me.

Case Study:-

Problem Statement:

We want to create a Login Module. The feature of the login module is as follows :-

  1. There is a provision of entering User Id and Password.
  2. User Id and Password should be validated.
  3. User can also register for their User Id and Password.

Solution: On seeing above example we don’t know whether User can be validated through file system or Active directory or database or any social media so this is a candidate for Abstraction.


Encapsulated Object : LoginVo :: It is implemented using 1st approach :-

package com.easycasestudy.oops.encapsulations;
import java.io.Serializable;
public class LoginVo implements Serializable{

 private static final long serialVersionUID = 8596196840151174966L;
 private String userId ;
 private String password ;
 public String getUserId() {
 return userId;
 public void setUserId(String userId) {
 this.userId = userId;
 public String getPassword() {
 return password;
 public void setPassword(String password) {
 this.password = password;

Abstraction: Subsystem contains Abstraction, PlainLoginValidation, RDBMSLoginValidation classes and it is realized through AbstractionInterface

Code snippets:

package com.easycasestudy.oops.abstraction;
import com.easycasestudy.oops.config.Config;
import com.easycasestudy.oops.encapsulations.LoginVo;
public class Abstraction implements AbstractionInterface {
 public boolean validateUser(LoginVo loginVo) {
 boolean isValidUser = false;
 Config config = new Config();
 isValidUser = PlainLoginValidation.validateUser(loginVo);
 }else if(Config.configMap.get("AUTH_TYPE").equalsIgnoreCase("RDBMS"))
 isValidUser = RDBMSLoginValidation.validateUser(loginVo);
 return isValidUser;


package com.easycasestudy.oops.abstraction;

import com.easycasestudy.oops.config.Config;
import com.easycasestudy.oops.encapsulations.LoginVo;

public class PlainLoginValidation {
 public static boolean validateUser(LoginVo loginVo){
 return Config.configMap.get("User").indexOf(loginVo.getUserId()) != -1 &&
 Config.configMap.get("PWD").indexOf(loginVo.getPassword()) != -1 ; 


package com.easycasestudy.oops.abstraction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.easycasestudy.oops.config.Config;
import com.easycasestudy.oops.encapsulations.LoginVo;

public class RDBMSLoginValidation {

 public static boolean validateUser(LoginVo loginVo){
 boolean isValidUser = false;
 String jdbcDriver = Config.configMap.get("jdbc.driver");
 String jdbcURL = Config.configMap.get("jdbc.url");
 String dbUserId = Config.configMap.get("jdbc.userid");
 String dbPassword = Config.configMap.get("jdbc.password");
 String tableName = Config.configMap.get("jdbc.table");
 String userIdColumnName = Config.configMap.get("jdbc.userIdColumn");
 String passwordColumn = Config.configMap.get("jdbc.passwordColumn");
 String sql = "SELECT * FROM " + tableName + " WHERE " + userIdColumnName +" = ? AND " + passwordColumn + " = ? ";
 //STEP 2: Register JDBC driver

 //STEP 3: Open a connection
 System.out.println("Connecting to database...");
 Connection conn = DriverManager.getConnection(jdbcURL,dbUserId,dbPassword);

 //STEP 4: Execute a query
 System.out.println("Creating statement...");
 PreparedStatement pstmt = conn.prepareStatement(sql);
 pstmt.setString(1, loginVo.getUserId());
 pstmt.setString(2, loginVo.getPassword());
 ResultSet rs = pstmt.executeQuery();

 //STEP 5: Extract data from result set
 isValidUser = true;
 //STEP 6: Clean-up environment
 }catch(SQLException se){
 //Handle errors for JDBC
 }catch(Exception e){
 //Handle errors for Class.forName
 return isValidUser;

Interface :-
package com.easycasestudy.oops.abstraction;

import com.easycasestudy.oops.encapsulations.LoginVo;

public interface AbstractionInterface {
 public boolean validateUser(LoginVo loginVo);


UI Classes :

package com.easycasestudy.oops.ui;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.easycasestudy.oops.abstraction.Abstraction;
import com.easycasestudy.oops.abstraction.AbstractionInterface;
import com.easycasestudy.oops.encapsulations.LoginVo;

public class LoginUI {
 public static void main(String[] args) {
 JFrame frame = new JFrame("OOPS Concept");
 frame.setSize(300, 200);

 private static void placeComponents(JFrame frame) {

 JLabel userLabel = new JLabel("User Id");
 userLabel.setBounds(10, 10, 80, 25);

 JTextField userText = new JTextField(20);
 userText.setBounds(100, 10, 160, 25);

 JLabel passwordLabel = new JLabel("Password");
 passwordLabel.setBounds(10, 40, 80, 25);

 JPasswordField passwordText = new JPasswordField(20);
 passwordText.setBounds(100, 40, 160, 25);

 JButton loginButton = new JButton("Login");
 loginButton.setBounds(10, 80, 80, 25);

 JButton registerButton = new JButton("Register");
 registerButton.setBounds(180, 80, 80, 25);

 ActionListener loginButtonListener = new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 String message = null ;
 JButton source = (JButton) e.getSource();
 String userId = userText.getText();
 String password = passwordText.getText();
 message = validateUser(userId, password);
 if(message != null)
 JOptionPane.showMessageDialog(source, message);
 LoginVo loginVo = new LoginVo();
 AbstractionInterface abstraction = new Abstraction();
 boolean blnValidUser = abstraction.validateUser(loginVo);
 message = validateUser(blnValidUser);
 JOptionPane.showMessageDialog(source, message);
 registerButton.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
 JOptionPane.showMessageDialog((Component) e.getSource(),
 "Register button has been pressed");

 private static String validateUser(String userId, String password) {
 String message = null;
 if(userId.equalsIgnoreCase("") && password.equalsIgnoreCase(""))
 message = "Please enter User Id and Password";
 else if(userId.equalsIgnoreCase(""))
 message = "Please enter User Id";
 }else if(password.equalsIgnoreCase(""))
 message = "Please enter Password";
 return message;
 private static String validateUser(boolean blnValidUser) {
 String message = "";
 message = "Login Sucessful";
 message = "Please enter valid User Id and Password";
 return message;

Configuration Reading File

package com.easycasestudy.oops.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class Config {

 public static Map<String, String> configMap = new HashMap<String, String>();
 public void configureApps() {
 Properties prop = new Properties();
 InputStream input = null;
 String propFileName = "config.properties";
 try {

 input = getClass().getClassLoader().getResourceAsStream(propFileName);
 // load a properties file

 configMap.put("AUTH_TYPE", prop.getProperty("AUTH_TYPE"));
 configMap.put("User", prop.getProperty("User"));
 configMap.put("PWD", prop.getProperty("PWD"));
 configMap.put("jdbc.url", prop.getProperty("jdbc.url"));
 configMap.put("jdbc.userid", prop.getProperty("jdbc.userid"));
 configMap.put("jdbc.password", prop.getProperty("jdbc.password"));
 configMap.put("jdbc.driver", prop.getProperty("jdbc.driver"));
 configMap.put("jdbc.table", prop.getProperty("jdbc.table"));
 configMap.put("jdbc.userIdColumn", prop.getProperty("jdbc.userIdColumn"));
 configMap.put("jdbc.passwordColumn", prop.getProperty("jdbc.passwordColumn"));
 // get the property value and print it out

 } catch (IOException ex) {
 } finally {
 if (input != null) {
 try {
 } catch (IOException e) {

Configuration File

#Plain, RDBMS,Twitter,Facebook

#jdbc:oracle:thin:@localhost:1521:xe for Oracle
jdbc.url=<jdbc url>
jdbc.userid=<jdbc connection userid>
jdbc.password=<jdbc connection password>
#oracle.jdbc.driver.OracleDriver for Oracle