QA'S approach 2 Java - OOPS concepts
OOPS concepts in Java
Experts in Software Programming thought of combining the Data and Operations. Therefore, the birth of Object Oriented Programming which is commonly called OOPS.
Manipulating the objects to get results is the goal of Object Oriented Programming.
Features of OOP is
1)Object
2)Class
3)Encapsulation
4)Inheritance
5)Abstraction
6)Polymorphism
7)Exception Handling
8)Message Parsing
These features neither belongs to java nor to any other language. These are global features. Any language can adopt these features to become OOPL.
Object : A real world entity/substance is called Object
Object is having 3 things
1) Identity / name
2) Properties / variables
3) functionality / methods
Objects will participate in the process of communication using functionality(methods). In the process of communication, Objects will share properties information.
Eg: QTP application is developed based on OOP's concept.
In Descriptive programming used in QTP, we work around browser objects.
Browser().Page().Frame()
Browser object calls Page object, Page object calls Frame object.
Class Browser
{
Page Page;
......
}
Class Page
{
Frame Frame;
......
}
Eg: QTP application is developed based on OOP's concept.
In Descriptive programming used in QTP, we work around browser objects.
Browser().Page().Frame()
Browser object calls Page object, Page object calls Frame object.
Class Browser
{
Page Page;
......
}
Class Page
{
Frame Frame;
......
}
Class :
Complete representation of real world entity(object).
It declares & defines what data variables the object will have and what operations can be performed on the class's object
Class can also be defined as user defined data type but it also contains functions in it.
Encapsulation :
Protecting our Data. Encapsulation = Abstraction + Data Hiding
This is the practice of keeping fields within a class private, then providing access to them via public
methods. It’s a protective barrier that keeps the data and code safe within the class itself. This way, we
can re-use objects like code components or variables without allowing open access to the data
system-wide
Encapsulation is the concept of protecting object information by placing properties and funcationlity as a block...
class / Encapsulation
{
properties
+
methods
}
Encapsulation ---> theory
Class ---> technical implementation.
So, let us talk about defining access to data members and member functions in a general scenario
let us assume that you have a car and an Internet servicing bank account.If your neighbour ask your car for a ride,what do you say?(probably yes ).But when your neighbour ask your sensitive bank details ,what do you say ?(probably No). here we are defining the access to data members.Which is nothing but encapsulation.
We implement encapsulation in programming with access modifiers like public,private,default and protected
We can achieve encapsulation in Java by:
Declaring the variables of a class as private.
Providing public setter and getter methods to modify and view the variables values.
So that Private Data is only accessible from outside using the public methods defined in the same class.
The fields can be made read-only (If we don’t define setter methods in the class) or write-only (If we don’t define the getter methods in the class).
Encapsulation essentially has both i.e. information hiding and implementation hiding.
Data hiding is achieved via access modifiers- Public, Private, Protected, Default
Information hiding
class InformationHiding
{
//Restrict direct access to inward data
private ArrayList items = new ArrayList();
//Provide a way to access data - internal logic can safely be changed in future
public ArrayList getItems(){
return items;
}
}
Implementation hiding
interface ImplemenatationHiding {
Integer sumAllItems(ArrayList items);
}
class InformationHiding implements ImplemenatationHiding
{
//Restrict direct access to inward data
private ArrayList items = new ArrayList();
//Provide a way to access data - internal logic can safely be changed in future
public ArrayList getItems(){
return items;
}
public Integer sumAllItems(ArrayList items) {
//Here you may do N number of things in any sequence
//Which you do not want your clients to know
//You can change the sequence or even whole logic
//without affecting the client
}
}
Inheritance :
Creating new Object with the help of existing object. It follows IS-A relationship.
Cow is an Animal.
Cow is an Animal.
Abstraction :
Hiding irrelevant data
Hiding unnecessary details of Object and shows only essential features to communicate.
There are many ways to achieve abstraction in object oriented programming, such as encapsulation and inheritance.
For example Hibernate is an abstraction over JDBC.
Abstraction using Encapsulation
Properties in one class are not directly accessible by funtions/methods outside of the class, since they are not global.
The variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class
There are many ways to achieve abstraction in object oriented programming, such as encapsulation and inheritance.
For example Hibernate is an abstraction over JDBC.
Abstraction using Encapsulation
Properties in one class are not directly accessible by funtions/methods outside of the class, since they are not global.
The variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class
Class level abstraction
a) Abstract Class
b) Interface
How are Abstraction & Encapsulation implemented?
Abstraction is implemented in Java using interface and abstract class while
Encapsulation is implemented using private, package-private and protected access modifier.
Polymorphism :
One Object is showing different behavior while communicating with different objects.
1.Overloading
2.Overriding
Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.
Compile time polymorphism is achieved by method overloading.
Runtime polymorphism is implemented when we have “IS-A” relationship between objects. This is also called as method overriding because subclass has to override the superclass method for runtime polymorphism. If we are working in terms of superclass, the actual implementation class is decided at runtime. Compiler is not able to decide which class method will be invoked. This decision is done at runtime, hence the name as runtime polymorphism or dynamic method dispatch.
The best example for polymorphism is Milk.It has the ability to exist in more than one form as cheese,curd,ghee etc,In java we implement polymorphism by method overloading and method overriding
Message Parsing:
A single object by itself may not be very useful. An application contains many objects. One object interacts with another object by invoking methods on that object. It is also referred to as Method Invocation
Association
Association is the OOPS concept to define the relationship between objects. Association defines the multiplicity between objects. For example Teacher and Student objects. There is one to many relationship between a teacher and students. Similarly a student can have one to many relationship with teacher objects. However both student and teacher objects are independent of each other.
In this OOP concept, all object have their separate lifecycle, and there is no owner.
Aggregation
Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.
Car has-a Engine ->HAS-A Relationship
However, there is ownership such that child object can’t belong to another parent object. For example consider class/objects department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed
If a class have an entity reference which can seen from below program
Employee has an entity reference address, so relationship is Employee HAS-A address.
When use Aggregation?
Code reuse is also best achieved by aggregation when there is no is-a relationship.
Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
Composition
Composition is a special case of aggregation. Composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship can’t exist on it’s own, then it’s a case of composition. For example, Car has-a Engine. Here Engine can’t exist without Car.
It is also called "death" relationship. Child objects do not have their lifecycle so when parent object deletes all child object will also delete automatically
2.Overriding
Polymorphism is the concept where an object behaves differently in different situations. There are two types of polymorphism – compile time polymorphism and runtime polymorphism.
Compile time polymorphism is achieved by method overloading.
Runtime polymorphism is implemented when we have “IS-A” relationship between objects. This is also called as method overriding because subclass has to override the superclass method for runtime polymorphism. If we are working in terms of superclass, the actual implementation class is decided at runtime. Compiler is not able to decide which class method will be invoked. This decision is done at runtime, hence the name as runtime polymorphism or dynamic method dispatch.
The best example for polymorphism is Milk.It has the ability to exist in more than one form as cheese,curd,ghee etc,In java we implement polymorphism by method overloading and method overriding
Message Parsing:
A single object by itself may not be very useful. An application contains many objects. One object interacts with another object by invoking methods on that object. It is also referred to as Method Invocation
Exception Handling
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at runtime.
Association is the OOPS concept to define the relationship between objects. Association defines the multiplicity between objects. For example Teacher and Student objects. There is one to many relationship between a teacher and students. Similarly a student can have one to many relationship with teacher objects. However both student and teacher objects are independent of each other.
In this OOP concept, all object have their separate lifecycle, and there is no owner.
Aggregation
Aggregation is a special type of association. In aggregation, objects have their own life cycle but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a case of aggregation.
Car has-a Engine ->HAS-A Relationship
However, there is ownership such that child object can’t belong to another parent object. For example consider class/objects department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed
If a class have an entity reference which can seen from below program
class Employee{
int id;
String name;
Address address;//Address is a class
...
}
Employee has an entity reference address, so relationship is Employee HAS-A address.
Employee has an entity reference address, so relationship is Employee HAS-A address.
Why use Aggregation?
For Code Reusability.
When use Aggregation?
Code reuse is also best achieved by aggregation when there is no is-a relationship.
Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.
Composition
Composition is a special case of aggregation. Composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship can’t exist on it’s own, then it’s a case of composition. For example, Car has-a Engine. Here Engine can’t exist without Car.
It is also called "death" relationship. Child objects do not have their lifecycle so when parent object deletes all child object will also delete automatically
Comments
Post a Comment