Here is the list of Java Design Patterns. I am putting all together for now, will try to add explanation and code samples shortly -
Creational Patterns
- Abstract Factory
- Builder
- Factory
- Prototype
- Singleton
Structural Patterns
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Behavioral Patterns
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
Java
Design Patterns, Java
Threads and Processes helps in concurrent execution of any application.
Process:
- an architectural construct : can affect the architecture of an application.
- processes are independend execution units
- they have their own state, address space
- use interprocess communication to communicate between threads
Threads:
- coding construct : doesn’t affect the architecture.
- a single process might contain multiple threds
- all threds within a process share process’s address space and state
- threads can communicate with each other directly
Java
process, threads
Q. What is System.out.println?
- System = final System class
- out = static PrintStream object
- println = method of a printstrem class
Q. What is difference between abstract class and interfaces.
- abstract class can provide implementation, interface has no implementation at all
- used using extends, used using implements
- both can not be instantiated
Read more…
Java
core java, interview
Q: What are basic OOPS Concepts?
A: - Inheritance : When a child class acquires all the methods and properties of parent class it is called inheritance. Java supports inheritance either using classes or interfaces.
Encapsulation: In above class, we have declared id as private and defined two methods that will act as mutator and accessors for this id propert. Thus we are encapsulateing objects property and methods with in an object, keeping it safe from outside world.
Polymorphism: In below scenario, we have added 2 declaration of a function add() to child class, one is for adding integers and another is to add float values. This kind of behavior when same name is used to perform different tasks as per the parameters passed to it is called polymorphism.
class Parent
{
private int id;
public Parent()
{
aMethod();
}
public void aMethod()
{
System.out.println("I am in Parent");
}
public int getId()
{
return this.id;
}
public void setId(int id)
{
this.id = id;
}
}
class Child extends Parent
{
public Child()
{
bMethod();
}
public void bMethod()
{
System.out.println("I am in Child");
}
public void add(int a, int b)
{
}
public void add(float a, float b)
{
}
}
In java polymorphism is achieved by function overloading. It is also called Compile time Polymorphism. Compile decides at compile time that which version of function is called depending up on the parameters passed.
function overriding is also type of polymorphism, some times it is called Run time polymorphism, because the methods version is decided depending on the reference type of object at run time.
Important Notes:
- return type is not part of method signature, so mere change of return types will not qualify a method to be polymorphic.
- if only parameter names are changed then that is not polymorphism.
public void aMethod(int a, long b)
{
System.out.println("int a, long b");
}
public void aMethod(long a, int b)
{
System.out.println("long a, int b");
}
For above methods to be polymorphic you need to use them as -
someobject.aMethod(1,2L);
someobject.aMethod(1L,2);
//someobject.aMethod(1,2); //compile time error
//someobject.aMethod(2,1); //compile time error
Java
encapsulation, inheritance, oops, polymorphism, runtime polymorphism
Lets see how we can sort our Employee objects using different criteria like id, name, salary.
SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.
Here I have created scenarios or you can say strategies OR classes for sorting as SortById, SortByName, SortBySalary. Each of above class implements comparator interface and overrides compare method, which takes two objects to compare and return eithre -1, 1 or 0.
Note: I have added few helper functions that really helps while coding like print - this takes array list of objects and prints in format I want instead of class id and all that. toString - overridden this function so I can return the class string as required
Here goes the code -
Read more…
Java
comparator, Java, sorting