Summary of Object-Oriented Programming (OOP) Concepts in Java

Object-Oriented Programming (OOP) uses "objects" to model realworld objects. 

Object-Oriented Programming (OOP) consist of some important concepts namely Encapsulation, Polymorphism, Inheritance and Abstraction. These features are generally referred to as the OOPS concepts. 

If you are new to object oriented approach for software development, you can first read about object oriented approach in little more detail @ object-oriented-systems-development

An object in OOP has some state and behavior. In Java, the state is the set of values of an object's variables at any particular time and the behaviour of an object is implemented as methods.

Class can be considered as the blueprint or a template for an object and describes the properties and behavior of that object, but without any actual existence. An object is a particular instance of a class which has actual existence and there can be many objects (or instances) for a class.

Static variables and methods are not purely object oriented because they are not specific to instances (objects) but common to all instances. 

 

Encapsulation

Encapsulation is the process of wrapping up of data (properties) and behavior (methods) of an object into a single unit; and the unit here is a Class (or interface). 

Encapsulate in plain English means to enclose or be enclosed in or as if in a capsule. In Java, everything is enclosed within a class or interface, unlike languages such as C and C++ where we can have global variables outside classes. 

Encapsulation enables data hiding, hiding irrelevant information from the users of a class and exposing only the relevant details required by the user. We can expose our operations hiding the details of what is needed to perform that operation. 

 

Inheritance

Inheritance describes the parent child relationship between two classes.

A class can get some of its characteristics from a parent class and then add more unique features of its own. For example, consider a Vehicle parent class and a child class Car. Vehicle class will have properties and functionalities common for all vehicles. Car will inherit those common properties from the Vehicle class and then add properties which are specific to a car.

In the above example, Vehicle parent class is known as base class or superclass. Car is known as derived class, Child class or subclass.

Java supports single-parent, multiple-children inheritance and multilevel inheritence (Grandparent-> Parent -> Child) for classes and interfces.

Java supports multiple inheritance (multiple parents, single child) only through interfaces. This is done to avoid some confusions and errors such as diamond problem of inheritance. 

 

Plymorphism

The ability to change form is known as polymorphism. Java supports different kinds of polymorphism like oveloading and overriding.

 

Overloading

The same method name (method overloading) or operator symbol (operator overloading) can be used in different contexts.

Java doesn't allow operator overloading except that "+" is overloaded for class String. The "+" operator can be used for addition as well as string concatenation.

Overloading may be also called compile time polymorphism. 

 

Overriding (or subtype polymorphism)

We can override an instance method of parent class in the child class.

When you refer to a child class object using a Parent reference (e.g.  Parent p = new Child()) and invoke a method, the overriden child class method will be invoked. Here, the actual method called will depend on the object at runtime, not the reference type. 

Overriding is not applicable for static methods or variables (static and non-static). In case of variables (static and non-static) and static methods, when you invoke a method using a reference type variable, the method or variable that belong to the reference type is invoked.

Overriding may be also called runtime polymorphism.

 

Abstraction

In plain English, abstract is a concept or idea not associated with any specific instance and does not have a concrete existence.

Abstraction in Object Oriented Programming refers to the ability to make a class abstract.

Abstraction captures only those details about an object that are relevant to the current perspective, so that the programmer can focus on a few concepts at a time.

Java provides interfaces and abstract classes for describing abstract types.

  • An interface is a contract or specification without any implementation. An interface can't have behavior or state.

  • An abstract class is a class that cannot be instantiated, but has all the properties of a class including constructors. Abstract classes can have state and can be used to provide a skeletal implementation. 

Comments

when we extend any super class then sub class inherits all the members of super class except private member,, but when we create object of sub class then it include all super class and sub class data including private member ??? am i r8?

Was it useful?

This is an area of confusion to many. 

Java specification says clearly that "A subclass does not inherit the private members of its parent class.".

In inheritance, a class can get some of its characteristics from a parent class. Inherited fields can be accessed just like its normal fields (using this keyword). Note that when you don't provide this, a this is automatically added. A private field is not inherited means that it cannot be accessed (using this keyword) from a subclass like other methods.

However there is also a rule that when you create an object, it should call its super constructor. When this super constructor is called, the super class object is created with all fields, however only inherited fields can be accessed.

Below example should clear your doubts:

public class Parent {

  Parent(int i, int j) {
    this.i = i;
    this.j = j;
  }

  private int i;
  public int j;

  public static void main(String[] args) {
    Child c = new Child(5, 10);
    c.printIJ();
  }

  void printIJ() {
    System.out.println(i + " " + j);
  }
}

class Child extends Parent {
  Child(int i, int j) {
    super(i, j);
    // this.i=25;
    this.j = 15;
  }
}

I have commented out the line with this.i as that is not valid. i is a private field, hence not inherited and hence cannot be accessed using this keyword. However we can still access it using an accessible method indirectly as in this case. printIJ is accessible from subclass and printIJ can then access those private variables as printIJ  is a parent class method, which was inherited.

Another thing to understand is that private, protected and private are access modifiers and they control access to those variables. To understand better, refer to http://javajee.com/access-modifiers-in-java and http://javajee.com/quick-reference-example-code-for-access-modifiers.

Was it useful?

i know when we create an objects like Usman usm = new Usman(); then usman class object is created and it's memory address or reference is assigned to usm reference variable. but some time i confuse that when we create and object then what we say it ? usman type object is created or usm object is created of type usman ?

actually when we read most of books then it will create some confusion,, like in every book there is some new thing,,, like complete reference , we can say usm an object and in realily it holds object reference ,, but in rose india site , we cannot say usm as object?? plzz explain what will be right ?

Was it useful?

simple and nice explanations :)

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!