Skip to main content

Object Oriented Concepts in Java (OOPS Concepts)

Java OOPS Concepts 

    Object oriented programing language provides many concepts, such as Inheritance, Data Binding, Polymorphism etc.



Object:
    Any entity which has state and behavior is known as object. It can be physical or logical part. For example, a chair, pen, keyboard etc.
    An object can be define as instance of class. An object contains an address and takes up some space in memory. Objects can communicate with each other without knowing the details of each others data or code. Only the type of response and message acceptance is important. The representation of object is same as below:

Class:
    Class is a collection of object. It is logical entity. Class can also be define as a blueprint from which you can create individual object. Class doesn't consume any space. Sample example of java class declaration is as follows:
    class SampleDemo{
        // Statements or logical code
        // variable declaration with its data types 
        // methods delaration and definitions
        public static void main(String args[]){
            // Statements or logical code
            // variable declaration with its data types 
            // methods delaration and definitions
        }
    }

  • The first line 'class SampleDemo' declares a class, which is object oriented construct. 
  • Every class having the opening braces '{' and the closing braces '}' and it is call delaration of class.
  • Every class has the 'main()' method. It is a starting point of execution of program. 
  • The public is a keyword call access specifier which declare that it is unprotected and access from any other class. (We will discuss access specifiers in later sections)
  • The static keyword is declare that method is one part of entire class not a part of any object of class. (We will discuss this in later sections)
  • The type modifier void is defines that methods does not returns any value. 

Inheritance:
    Inheritance is the process by which objects of one class can acquires the properties of object of another class in other words, when one object/class can acquires all the properties of parent object/class, it is known as inheritance. It provides code reusability and use to achieve runtime polymorphism.
Ploymorphism:
    If any object have more than one form in other words, if one task is perform in different ways, it is known as polymorphism. In java method overloading and method overriding is use to achieve polymorphism. An example, an operation may clearly different behavior in different instance. The main behavior is depend on type of a data use in operation.

Abstraction:
    Abstraction refers to the act of representing essential features without including the background details or explanations. In other word, Hiding internal details and showing functionality is known as abstraction. Classes use the concept of abstraction and define as a list of abstract attributes such as size, weight, cost and methods that operates on the attributes. We use abstract class and interface to achieve abstraction.

Encapsulation:
    Binding or wrapping of code/method and data together into a single unit known as encapsulation. Data encapsulation is most striking features of class. The data is not accessible outside the world, and only methods which are wrapped in the class can access it. These method can provide interface between object's data and program. Java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Coupling:
    Coupling refers to the knowledge or information or dependency of another class.  If class has the detail information of another class, there is strong coupling. In java, we use public, private, protected and default modifiers to display the visibility of class, method and variable/field. We can also use interface for weaker coupling because there is no concreate implementation.

Association:
    Associations represents the relationship between the objects. One objects can associates with one object or many objects. There are four type of association between objects:
  •     One to One
  •     One to Many
  •     Many to One
  •     Many to Many

Comments

Popular posts from this blog

Introduction of Java

 What is Java? Java is an object-oriented programming language developed by Sun Microsystem in 1995. It is a combination of C and C++ features with some essential and additional concepts. Java is suited for both standalone and web application development. Java can run on any hardware OR software environment which make it platform independent. Java Example:                                                                                           class Simple{     public static void main(String args[]){          System.out.println("Welcome In Java World");     } } In above example the 'Simple' is a Class name, public is access specifier , Main is a method with argument, St...

Java Package

 Introduction      A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages. Simple example of java package The package keyword is used to create a package in java. //save as Simple.java   package mypack;   public class Simple{    public static void main(String args[]){       System.out.println("Welcome to package");      }   }   How to access package from another package? There are three ways to access the package from outside the package. 1. import package.*; 2. import package.classname; 3. fully qualified name. 1. Using packagename.*      If you use package.* the...

Java Classes, Object and Methods, Constructors

 Defining a class     A class is a user-defined data type or group of object with a template or blueprint that serves to define its properties. It is a logical entity. Once a class type has been define, we can create " Variables " which are similar to basic type declaration. In java, variables are termed as instance of class, which actual object. The basic form class defination is as follows: class classname [extends superclassname]{     [fields declaration]     [method declaration] } The classname and superclassname are valid java identifiers. The keyword extends indicate that superclassname properties extended into classname. It is a pert of inheritance. (We will discuss inheritance in later part.) Everything inside square brackets is optional that means we can use as per the requirements. For example, class Empty{ } In above example, the Empty class is class name. This class does not have any properties and therefor cannot do anything. A java cla...