Thursday, 9 July 2015

JAVA Basics

JAVA Basics:-

 Class:-
         Class is a blueprint of an object that contains variables for storing
          Data and functions to performing operations on these data. Class
          will not occupy any memory space and hence it is only
          logical representation of data.

Object:-
               Objects are the basic run-time entities in an object oriented system.
           Object is instance of Class .
          "Object is a Software bundle of related variable and methods."

          

DO you think that only definition is sufficient for understanding the anything? I think No.
then now we should come to think about Class and Object.
Just think about a Alien.And suddenly you see a Alien.Here i want to say that Class is only logical thing BUT object logical as well as physical both.Object occupy space.

Wednesday, 8 July 2015

Java Coupling and Cohesion


Java Coupling and cohesion Differences with Example


"Object-oriented programming has two main objectives: to build highly cohesive classes and to maintain loose coupling between those classes. High-cohesion means well-structured classes and loose coupling means more flexible, extensible software."

"Cohesive means that a certain class performs a set of closely related actions. A lack of cohesion, on the other hand, means that a class is performing several unrelated tasks. Though lack of cohesion may never have an impact on the overall functionality of a particular class or of the application itself the application software will eventually become unmanageable as more and more behaviours become scattered and end up in wrong places."

"Whenever one object interacts with another object, that is a coupling. In reality, what you need to try to minimize is coupling factors. Strong coupling means that one object is strongly coupled with the implementation details of another object. Strong coupling is discouraged because it results in less flexible, less scalable application software. However, coupling can be used so that it enables objects to talk to each other while also preserving the scalability and flexibility."

"In OO programming, coupling is unavoidable. Therefore, the goal is to reduce unnecessary dependencies and make necessary dependencies coherent."

Example :

Coupling is the unnecessary dependance of one component upon another
component's implementation. An example would be if you had a Dog class
that should be able to bark and jump. You could write it like this:

class Dog
{
void doAction(int number)
{
if(number==1)
//Jump code goes here
if(number==2)
//Bark code goes here
}
}

However, this way whoever used your class would have to follow your
convention that doAction(1) means jump and doAction(2) means bark. A
less-coupled way would be to write separate bark() and jump() methods.

Cohesion occurs when components are wired together in a smart, logical
way. If you have ever used a Controller class, that's a great example
of achieving cohesion. Highly cohesive components interact with each
other semantically, in other words, they tell each other what they want
to do. In a bank example, an ATM object should be able to tell an
Account object to getBalance(), or withdrawFunds(), etc. These are
sensible ways for the ATM and Account to interact, not specific ways
that are only useful in one program.

So good OOD requires that components can interact powerfully, but
independently of eachother's implementation details. (Think of
System.out.println() - most people have no idea how that works, but
when you want to output some text, you can just use the method.)

Encapsulation is the use of hiding implementation details within your
code. Changing the above Dog code from doAction() to jump() and bark()
would be a step toward good encapsulation. This is getting
frighteningly nerdy, but I think you could say "A program with high
cohesion and low coupling exhibits good encapsulation". In other words,
encapsulation is the goal you're trying to reach when you reduce
coupling. A class with good encapsulation, in turn, lends it self to
being a cohesive part of a system.

Thread basic

Application :

Application is a program which is designed to perform a specific task. For example, MS Word, Google Chrome, a video or audio player etc.

Process :

Process is an executing instance of an application. For example, when you double click MS Word icon in your computer, you start a process that will run this MS word application. Processes are heavy weight operations that they require their own separate memory address in operating system. Because of the processes are stored in separate memory, communication between processes (Inter Process Communication) takes time. Context switching from one process to another process is also expensive.

Thread :

Thread is a smallest executable unit of a process. Thread has it’s own path of execution in a process. For example, when you start MS word, operating system creates a process and start the execution of a primary thread of that process. A process can have multiple threads. Threads of the same process share the memory address of that process. i.e threads are stored inside the memory of a process. As the threads are stored in the same memory space, communication between threads (Inter Thread Communication) is fast. Context switching from one thread to another thread is also less expensive.