You are on page 1of 18

OOPS CONCEPTS

Presentation by, Balu Chandra S, Finware Technologies

CONTENTS
OOPS Intro Inheritance Encapsulation Abstraction Polymorphism Dynamic Binding

OOPS
OOPs stands for Object Oriented Programming System. Previously we used to follow the procedural programming paradigm while developing the software which makes it hard to add the new functionality at the maintenance phases. Even it was harder to test the module in the procedurally developed code because the coded modules were so tightly coupled. Even there was not even any chance to reuse the existing modules to decrease the coding efforts. Then there came the concept of the Object Oriented Programming which was totally based on the real life entities and scenarios. OOPs enables to program the real world objects and easily play with them in our application development scenarios. OOPs focuses on the relevant data of the real world object and forces to concentrate on the various functionalities effecting the states of the data

OOPs is a technique used to develop programs revolving around the real world entities. In OOPs programming model, programs are developed around data rather than actions and logics. In OOPs, every real life object has properties and behavior. which is achieved through the class and object creation. They contains properties (variables of some type) and behavior (methods). OOPs provides a better flexibility and compatibility for developing large_applications. There are five main pillars of an Object Oriented Programming Language : (1) Inheritance (2) Encapsulation (3) Abstraction (4) Polymorphism (5) Dynamic Binding

INHERITANCE
Inheritance is a process of creating new class and use the behavior of the existing class by extending them for reuse the existing code and adding the additional features as you need. It also use to manage and make well structured software. Inheritance allows a class (subclass) to acquire the properties and behavior of another class (superclass). In java, a class can inherit only one class (superclass) at a time but a class can have any number of subclasses. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time. Java uses extends keyword to extend a class. Real time example: Father & Son

Example for inheritance:


class A{ public void fun1(int x){ System.out.println("Int in A is :" + x); } } class B extends A{ public void fun2(int x,int y){ fun1(6); // prints "int in A" System.out.println("Int in B is :" + x " and "+y); } } public class inherit{ public static void main(String[] args){ B obj= new B(); obj.fun2(2); } }

ENCAPSULATION
Encapsulation is the ability to bundle the property and method of the object and also operate them. It is the mechanism of combining the information and providing the abstraction as well. Real time example: Medical Capsules i.e one drug is stored in bottom layer and another drug is stored in Upper layer these two layers are combined in single capsule. Encapsulation is the process of binding together the methods and data variables as a single entity. It keeps both the data and functionality code safe from the outside world. It hides the data within the class and makes it available only through the methods.

Java provides different accessibility scopes (public, protected, private ,default) to hide the data from outside. Example Program: Here we provide a example in which we create a class "Check" which has a variable "amount" to store the current amount. Now to manipulate this variable we create a methods and to set the value of amount we create setAmount() method and to get the value of amount we create getAmount() method . Code: class Check{ private int amount=0; public int getAmount(){ return amount; } public void setAmount(int amt){ amount=amt; } }

public class Mainclass{ public static void main(String[] args){ int amt=0; Check obj= new Check(); obj.setAmount(200); amt=obj.getAmount(); System.out.println("Your current amount is :"+amt); } }

ABSTRACTION
Abstraction refers to hiding non-essential features and showing the essential features The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). Eg: public class Vehicle { public String colour; public String model; }

POLYMORPHISM
As the name suggest one name multiple form, Polymorphism is the way that provide the different functionality by the functions having the same name based on the signatures of the methods. There are two type of polymorphism first is runtime polymorphism and second is compile-time polymorphism. Real time example: Person Person in Home act is husband/son, in Office acts Employer. in Public Good Citizon.

Polymorphism allows one interface to be used for a set of actions i.e. one name may refer to different functionality. Polymorphism allows a object to accept different requests of a client (it then properly interprets the request like choosing appropriate method) and responds according to the current state of the runtime system, all without bothering the user. There are two types of polymorphism : 1. Compile-time polymorphism 2. Runtime Polymorphism In compiletime Polymorphism, method to be invoked is determined at the compile time. Compile time polymorphism is supported through the method overloading concept in java.

Compile time example(Method overloading): class A{ public void fun1(int x){ System.out.println("The value of class A is : " + x); } public void fun1(int x,int y){ System.out.println("The value of class B is : " + x + " and " + y); } } public class polyone{ public static void main(String[] args){ A obj=new A(); // Here compiler decides that fun1(int) is to be called and "int" will be printed. obj.fun1(2); // Here compiler decides that fun1(int,int)is to be called and "int and int" will be printed. obj.fun1(2,3); } }

In rumtime polymorphism, the method to be invoked is determined at the run time. The example of run time polymorphism is method overriding. When a subclass contains a method with the same name and signature as in the super class then it is called as method overriding. Example(Method Overriding): class A{ public void fun1(int x){ System.out.println("int in Class A is : "+ x); } } class B extends A{ public void fun1(int x){ System.out.println("int in Class B is : "+ x); } }

public class polytwo{ public static void main(String[] args){ A obj; obj= new A(); // line 1 obj.fun1(2); // line 2 (prints "int in Class A is : 2") obj=new B(); // line 3 obj.fun1(5); // line 4 (prints ""int in Class B is : 5") } }

DYNAMIC BINDING
It is the way that provide the maximum functionality to a program for a specific type at runtime. There are two type of binding first is dynamic binding and second is static binding. Dynamic Binding refers to the case where compiler is not able to resolve the call and the binding is done at runtime only. Let's try to understand this. Suppose we have a class named 'SuperClass' and another class named 'SubClass' extends it. Now a 'SuperClass' reference can be assigned to an object of the type 'SubClass' as well. hod(); // SubClass version is called

If we have a method (say 'someMethod()') in the 'SuperClass' which we override in the 'SubClass' then a call of that method on a 'SuperClass' reference can only be resolved at runtime as the compiler can't be sure of what type of object this reference would be pointing to at runtime. ... SuperClass superClass1 = new SuperClass(); SuperClass superClass2 = new SubClass(); ... superClass1.someMethod(); // SuperClass version is called superClass2.someMethod(); // SubClass version is called

THANK YOU......

ANY QUERIES...???

You might also like