Object Oriented Programming in java

Object Oriented Program or Oops is a technique to create programs based on the real world object. In the Oops programming model programs are organized around objects and data rather than actions and logic.

What is Oops?
          An Object Oriented Programming organizes a program around its data, i.e., objects and a set of well defined interfaces to that data. An object-oriented programming can be characterized as data controlling access to code.

Why Oops?
          Object Oriented Programming enables programmers define not only the data, but also its associated behaviors that can be applied to the data. It also enables programmers to create relationship between one object and another. For example, Objects can inherit characteristics from other objects. To understand the importance of Oops first we need to understand the problem with Procedural / Structural programming practices.

Problems with Procedure Oriented Programming?
          Procedure Oriented Programming suitable for small scale projects. But as the program increases in size, along with maintenance burden also increases. The modules were tightly integrated changes in one part of the module it also effects the other modules. Team developments not truly achievable. The next big problem with procedural programming was with respect to re-usability of the code. It was very difficult to write re-usable code, with minimum maintenance.

Advantages of Oops:
          Oops emulates the real world in a software system. Some of the advantages of Objects Oriented Programming includes,
  • Real-world programming
  • Reusability of code
  • modularity of code
  • Resilience to change
  • Information hiding
Features of Oops:
Object:  An object is any thing that exists in the world. An object will have properties and actions. Variable and methods are available in object. Object is the super class of all the classes in java. Objects are two types,
         1. Mutable Objects: which objects content can be modified, those objects are mutable objects. For example, StringBuffer,..
         2. Immutable Objects: Which object content can't be modified, those object are immutable objects. For example, String,..

Class: A class is a model of creating an object (or) A class is a blue print of an objects. Class can not exists physically. In the below mentioned few more important points about Class,
  • Without a class we can not create an object
  • But a class can exists without object
  • Class also will have variables(data) and methods(code)
  • There are three class access modifiers: public, protected, private.
  • There are four access levels: public, protected, default, private.
  • Classes can also be modified with final, abstract and strictfp
  • A class can not be both final and abstract
  • A final class prevents inheritance i.e. A final class can not extends to another class.
Abstraction: shows only necessary details and hides unnecessary information from the user is what called abstraction. The another definition for abstraction is abstraction refers to the act of representing an essential features without including background details.

Encapsulation: The exact definition of the encapsulation is binding up of data and methods into a single unit is called as encapsulation. POJO class is a example for encapsulation or else simply a class is example for encapsulation.
                             (or)
          private variables and public methods is also called as encapsulation. For example, I have declared a class with private variables and public methods which will work on variables. Private variables we can not access directly to other classes, we can access through only the public methods.

Inheritance: normal definition for inheritance is deriving a new class from existing class is called as inheritance. The newly derived class is called as subclass and existing class is called as super class. The main advantage of using inheritance is code re-usability. Subclass has the access of super class properties and all, but super class doesn't have access to subclass properties.


Polymorphism: Polymorphism word came from two Greek words. Poly means many morphism means forms. So, adding of these two words as Polymorphism means many forms. In java there are two types of polymorphism is there. 

                   1. Compiletime polymorphism
                   2. Runtime Polymorphism

Message Passing: calling a method is called message passing.

Why java is object oriented language?
          Java obeys all the features on Oops.

What is the difference between object oriented programming and object based programming?
          Object oriented language follows all the features of Oops.
Ex: C++, Java
          Object based programming languages contains all the features of Oops except inheritance.
Ex: Smalltalk, Simula67,JavaScript...

Object is an instance of a class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.javatbrains;

public class Employee {
     // Properties
     int id;
     String name;

     // Methods
     public void demo(int id, String name) {
          System.out.println("Employee Id: "+ id);
          System.out.println("Employee Name: "+ name);
     }

     public static void main(String[] args) {
          // emp object is the instance of Employee class
          Employee emp = newEmployee();
          emp.demo(10, "JavaTBrains");
          System.out.println("Hashcode of the emp: "+emp.hashCode());
     }

}
OutPut:
     Employee Id: 10
     Employee Name: JavaTBrains
     Hashcode of the emp: 4072869
         
  • new operator(new Employee()) will create the object, objects are created JVM on dynamic memory.
  • Hashcode is produced or generated from memory address. Hashcode & memory address are not same. Hashcode is the reference of the object.
  • A class is called user defined datatype.
  • The variables which are stored in the object are called instance variables.
  • Method codes are stored in method area.
  • The unique code or id of every objects which is allotted by JVM.
  • When the instance variables are not initialized java compiler uses some default values to initialize them. It gives initial value on the data type.
Access Specifier:
          An access specifier is a keyword that represents, how to access the members of a class. There are four types of access specifiers.

      1. Private: A private member of a class is not available at any where outside of the class or makes a method or a variable accessible only with in the class. Which will declares the keyword as private.

      2. Public: A public members of a class are accessible from any where outside the class or makes a class, a method or a variable accessible from any other class. Which will declares the keyword as public.
      3. Protected: A protected members are available out side the class, but with in the package. Which will declares the keyword as protected.
      4. Default: If there is no specifier declared any front of the class or variable or method declaration, that treated as a default variable or default method or default class.


Opps Interview Questions and answers:

Q1: What is object oriented approach?

A: Object Oriented Programming approach is a Programming methodology to design computer programs using classes and objects.

Q2: What are the Oops features in java?

A: There are many features related to Object Oriented Approach. That are,
                      1. Class/Object
                      2. Encapsulation
                      3. Abstraction
                      4. Inheritance
                      5. Polymorphism

Q3: What is the difference between a class and object?

A: A class is a model for creating objects and does not exist physically. An object is any thing which exists physically. Both the class and object contains variables and methods.

Q4: What is the difference between Object Oriented Programming and Object Based Programming languages?

A: Object oriented Programming languages follow all the features of Object Oriented Programming System(Oops). Smalltalk, Simula-67, C++, Java are examples for Oops languages.
Object Based Programming languages follow all the features of OOPs, except inheritance. For example, JavaScript and VBScript will come under object based programming languages.

Q4: What is hash code?

A: Hash code is a unique identification number allotted to the objects by the JVM. This hash code number is also called reference number which is created based on the object in memory and is unique for all objects, except for String. 

Q5: How can you find hash code if an object?

A: The hashCode() method of 'Object' class in java.lang package is useful to find the hash code of an object. 

Q6: What is the use of constructor in java?

A: If you want to read about the Constructor related questions and answers. Please refer my post name as Constructor .

Q7: What is Overloading and Overriding in java?

A:  If you want to come to know what all the overloading and overriding related interview questions and answers follow with these link Overloading and Overriding in java.

Q8: What is Object Graph?


A: Object Graph is a graph showing relationship between different objects in memory.


If any one is facing other than these questions in interview, please share with me through "Contact Me Here" gadget or drop me an email to "admin@javatbrains.com".


Do you know? 

Comments

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example