OOPs

Java OOP ConceptReference URL
Object Oriented Programming is an approach to organize and develop the program, which attempts to
eliminate some of the drawbacks of Conventional Programming methods . All the languages are not
suitable with OOP concept. Language that support OOP concept / features include C++, ADA, Object
Pascal, Java. C++ is basically a procedural language that follow only some features of OOP. Java is a
core Object Oriented Programming Language.
There are following features of OOP followed by Java:

Objects and Classes
Object is similar like an entity such as table, chair, pen etc. Every entity has some its behavior and
attributes.

A person is also an object that has some behaviours (methods) such as To walk, To breathe, To listen,
To talk, To see etc. that are accomplished by some of its attributes i.e. Nose, Eye, Ear, Legs etc.
They may represent any living or non living entity like a person, a place, a bank account, a record of
student etc. Program objects should be chosen in a manner they match closely with real word objects.
When a program is executed, the objects interact by sending message from one to another. To create an
object in Java, we use following syntax.

class <Class name>
{
    ………………… //Class body
    ………………… //Class body
    ………………… //Class body
}

e.g. Student s;
Class is a building block of Java. It is a logical way to group together Fields that hold values and
associated methods that operate on these fields into a single unit. To create a class in Java, we use
following syntax.
<Class name> <Object name>;
example

class Student
{
      int roll;
      String name;
      public void get()
      {
           roll=20;
           name="Jatin";
      }
}

Data Abstraction and Encapsulation
The process of linking / binding data and associated code / procedures to make them a compound unit,
is called data encapsulation. When data and code is associated with one another, they create objects.
These objects support the Encapsulation mechanism. In this process, the data OR code may be public /
private / protected / friendly etc. Private code is only accessible by the particular part of that object,
whereas public code is accessible by any other object.

Abstraction is a very classic feature that defines only fundamental / vital information without involving
the Source code information or elaboration. When we use the social networking sites such as facebook,
orkut etc. OR any other sites; we only complete the particular task. We does not check the source code
of that webpage. Let we talk about our daily life, when we have softdrink; we only enjoy the taste of
that, we does not try to know which type of chemicals are used for preparing this.

Inheritance
Inheritance means Heredity i.e. Transfer some qualities and attributes of parents to their children.
Inheritance is a process to derive new classes from already existing classes. Existing class is called
Super / Base / Parent class and new class is known as Sub / Derived / child class. This process defines
the characteristics hold by Super class, inherited by the sub class. The basic way to adopt this concept
is that work again with the same qualities / standards.

e.g. If we talk about persons, every person has a name; If we talk about students, every student is a
person and it acquire the attributes of person class.
There are three type of inheritance:

1. Simple Inheritance

Simple Inheritance

Simple Inheritance

2. Multilevel Inheritance

Multilevel Inheritance

Multilevel Inheritance

3. Hierarchical Inheritance

Hierarchical Inheritance

Hierarchical Inheritance

Polymorphism
Polymorphism is very striking concept. In formal way Polymorphism means having or passing through
many stages of development. In technical language, Poly means many and morphism means the
process to change e.g. when we change one image to another image using computer animation tools, it
means we are changing the form of one image to further (Basic image to Advanced).

e.g. In our family and in our social network, usually we are known as different-different name.
We can use this concept to create and define multiple level of interface.

If we talk about Method Overloading, There is a group of methods having same name but differ in
argument / parameter list. Method overloading does not depend on return type of method. This is the
type of Multiple Polymorphism.

Method overriding is the type of Single Polymorphism.

Dynamic Binding
In our daily life, Binding means something that link together to any other thing. In java, Binding refers
to the linking of procedures call to the code to be executed in response to the call. In other words,
Binding is a process that links / connects the objects to procedures / methods / functions in a single unit
called class.

Message Communication
This is a process through which objects can interact / communicate with one another. People pass /
send messages to one another through many means such as Mobile, Letter etc. In the similar way
Object communicate with one another by sending and receiving information through requesting their
procedures / methods.

Passing message

Passing message

Figure: Passing message

what are the oops concept in java explain with real time examples ?
ANS :- We have the following oops concepts like
1.Abtsrction
2.Encapsulation
3.Inheritance
4.Polymorphism

Abstraction:
hiding unnecessary data from the users and making the application as user friendly then it is called as
abstrcation
ex:Any mail account the user is simply filling the form and he don’t know ho it is developed.

Encapsulation:
Wrapping the data and code as a single unit is called as encapsulation.
ex: Capsule is the best real time example for it.In it we will have two types of drugs combined as single capsule.

Inheritance :
Acquiring the properties from one class(base class or parent class)to another class(derived class or
child class)is called as inheritance.
Ex:Inventing laptop from desktop.

Polymorphism:
Single entity behaving in different forms is called as polymorphism
ex:Mouse is for click the buttons and dragging the bars or placing the pointer in a particular place.

Abstraction:Hiding unneccessary data from user is called Abstraction. access specifiers like public,
private etc are used to provide different level in abstraction.

Encapsulation:Taking data and object in a single unity is called Encapsulation. A class is example for
Encapsulation.

Inheritance:creating anew class from existing class is called Inheritance. reusability is main advantage
in inheritance.

polymorphism:if single method performs different task is called polymorphism.

1) What is meant by Object Oriented Programming?
OOP is a method of programming in which programs are organised as cooperative collections of
objects. Each object is an instance of a class and each class belong to a hierarchy.

2) What is a Class?
Class is a template for a set of objects that share a common structure and a common behaviour.

3) What is an Object?
Object is an instance of a class. It has state,behaviour and identity. It is also called as an instance of a
class.

4) What is an Instance?
An instance has state, behaviour and identity. The structure and behaviour of similar classes are
defined in their common class. An instance is also called as an object.

5) What are the core OOP’s concepts?
Abstraction, Encapsulation,Inheritance and Polymorphism are the core OOP’s concepts.

6) What is an Abstract Class?
Abstract class is a class that has no instances. An abstract class is written with the expectation that its
concrete subclasses will add to its structure and behaviour, typically by implementing its abstract
operations.

7)What is JIT compiler?
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte
code that have similar functionality at the same time, and hence reduces the amount of time needed for
compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual
machine (JVM) to the instruction set of a specific CPU.

OOPS Concept :
Q1) What is polymorphism?
Ans) Polymorphism gives us the ultimate flexibility in extensibility. The abiltiy to define more than
one function with the same name is called Polymorphism. In java,c++ there are two type of
polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
When you override methods, JVM determines the proper methods to call at the program’s run time, not
at the compile time. Overriding occurs when a class method has the same name and signature as a
method in parent class.

Overloading occurs when several methods have same names with
• Overloading is determined at the compile time.
• Different method signature and different number or type of parameters.
• Same method signature but different number of parameters.
• Same method signature and same number of parameters but of different type

Example of Overloading
int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a) //error conflict with the method int add(int a)

Example: Overloading

Class BookDetails
{
	String title;
	String publisher;
	float price;
	setBook(String title)
	{}
	setBook(String title, String publisher)
	{}
	setBook(String title, String publisher,float price)
	{}
}

Example: Overriding

class BookDetails
{
    String title;
    setBook(String title){ }
}

class ScienceBook extends BookDetails
{
setBook(String title){} //overriding
setBook(String title, String publisher,float price){ } //overloading
}

Q2) What is inheritance?
Ans) Inheritance is the property which allows a Child class to inherit some properties from its parent
class. In Java this is achieved by using extends keyword. Only properties with access modifier public
and protected can be accessed in child class.

public class Parent
{
	public String parentName;
	public int parentage;
	public String familyName;
}

public class Child extends Parent
{
	public String childName;
	public int childAge;
	public void printMyName()
	{
		System.out.println(“ My name is “+ chidName+” “ +familyName)
	}
}

In above example the child has inherit its family name from the parent class just by inheriting the class.

Q3) What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is known as multiple inheritance.
Java does not allow to extend multiple classes but to overcome this problem it allows to implement
multiple Interfaces.

Q4) What is abstraction?
Ans) Abstraction is way of converting real world objects in terms of class. For example creating a class
Vehicle and injecting properties into it.
E.g

public class Vehicle
{
     public String colour;
     public String model;
}

Q5) What is encapsulation?
Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts
like a container encapsulating the properties. The users are exposed mainly public methods.The idea
behind is to hide how thinigs work and just exposing the requests a user can do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s