Object Oriented Software Development with Java
Object Oriented technologies are a popular paradigm for developing applications. This approach models an application around a series of real world objects, such as an employee or a ship. In order to solve a problem, it is useful to think of the real world objects that make up the problem domain.
The OO approach is based on three distinct activities:
The OO approach is based on three distinct activities:
- Object Oriented Analysis (OOA): This is concerned with determining the functionality of the system, that is, what should the application do
- Object Oriented Design (OOD): This is concerned with how the architecture supports the functionality of the application
- Object Oriented Programming (OOP): This is concerned with the actual implementation of the application
Basic Object Oriented Programming Principles
- Data encapsulation
- Inheritance
- Polymorphism
Data encapsulation
is concerned with hiding irrelevant information from the users of a
class and exposing the relevant. The primary purpose of data encapsulation is
to reduce the level of software development complexity. By hiding the
details of what is needed to perform an operation, the use of that operation is
simpler. Data encapsulation is also used to protect the internal state of an
object. By hiding the variables that represent the state of an object,
modifications to the object are controlled through the methods. Any changes to
the state are verified by the code in the methods. Also, by hiding variables,
sharing of information between classes is eliminated. This reduces the amount
of coupling possible in an application.
Inheritance
describes the relationship between two classes such that one class re-uses the
capabilities of another class.
Polymorphism primary
concern is to make the application more maintainable and extendable
polymorphism behavior is where the behavior of one or identical methods is
dependent upon the object it is executing against. Polymorphism in Java has two types:
- Compile time polymorphism (static binding)
- Runtime polymorphism (dynamic binding).
Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism.
Compile Time Polymorphism
In Java, static polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters.At compile time, Java knows which method to invoke by checking the method signatures. So, this is called compile time polymorphism or static binding.for example :
int add(int a,int b){}
int add(float a,float b){}
Runtime Polymorphism
When a subclass overrides a method of super class or parent class, at runtime JVM decide which method to call ; this is called as runtime polymorphism. Any java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
Principal
|
What is it?
|
Why Use it?
|
How to Use?
|
Data encapsulation
|
Technique that hides information from
the
users of that class
|
To reduce the level of software
development
complexity
|
Use access modifiers such as public, private, and protected
|
Inheritance
|
Technique to allow a derived or child
class to use parts of a base or parent class
|
To promote the re-use of the software
|
Use the extends
keyword
|
Polymorphism
|
Technique which supports
different behavior of
methods that is dependent
on the object the method
is executing against
|
To make an application more
maintainable
|
Inherent to the Java language
|
Classes and Object
Class generally
represents a real-world object. A class is a pattern or template
for creating multiple objects with similar features. It defines the variables
and methods of the class. It declares the capabilities of the class.
An Object is an
entity that has a state and a defined set of operations that operate on that
state. The state is represented as a
set of object attributes. The state of an object is typically hidden from the
users of the object and is reflected in the value of its instance variables. Getter methods return the state of an object. Setter methods that can change the state of an object The behavior of an object is
determined by the methods it possesses. This is an example of data
encapsulation. An object is the instantiation of a class. Each instance of a
class has its own unique set of instance variables.
Objects in Java are always
allocated on the heap. The heap is an area of memory that is used for
dynamically allocated memory, such as objects. In Java, objects are allocated
in a program and then released by the JVM. This release of memory is called
garbage collection and performed automatically by the JVM. An application has
little control over this process. The primary benefit of this technique is the
minimization of memory leaks.
Object creation
Objects are created using the new keyword.When a new object is instantiated using the new keyword:
Object creation
Objects are created using the new keyword.When a new object is instantiated using the new keyword:
- Memory is allocated for the new instance of the class
- A constructor is then called to perform initialization of the object
- A reference to the object is returned
Each instance of a class has its own set of instance variables that are independent of each other.
Memory management
Java memory management is dynamic and automatic. When the new keyword is used, it automatically allocates memory on the heap.A reference variable may de-reference an instance of an object by:
- Being re-assigned to another object
- Setting it to null
When the garbage collector determines that there are no references to it, the object becomes eligible for removal from the heap by a garbage collection thread and its memory can be re-used for other objects. This garbage collection process is essentially beyond the control of the application.
0 comments:
Post a Comment