Friday, 15 December 2017

Overloading and Overriding

Overloading-

public class Overloading {
public static void main(String[] args) {
Parent p = new Parent();
p.check();

Child c = new Child();
c.check();
c.test();

Parent pc = new Child();
pc.check(); // call only parent methods coz object refer by parent reference

//here pc reference point to Child obj, So we pc can be casted to Child. Now we can access both parent and Child obj mothods
((Child)pc).check();
((Child)pc).test();

p = c; // Child obj can be refered by parent reference
p.check();

}
}

class Parent {
void check() {
System.out.println("-- Check --");
}
}

class Child extends Parent {
void test() {
System.out.println("-- Test --");
}
}

Overriding - 

package com.qt;

public class OverridingTest {

public static void main(String[] args) {
ParentCls p = new ParentCls();
p.check(); // -- Check -- Parent

ChildCls c = new ChildCls();
c.check(); // -- Check -- Child
ParentCls pc = new ChildCls();
pc.check(); // -- Check -- Child
((ChildCls)pc).check(); // -- Check -- Child
ParentCls pp = c;
pp.check(); // -- Check -- Child
// We can use super inside child class overriden method to parent class methods by using child class object. 
}

}


class ParentCls {
void check() {
System.out.println("-- Check -- Parent");
}
}

class ChildCls extends ParentCls {
void check() {
System.out.println("-- Check -- Child");
}
}

Wednesday, 11 January 2017

Memory Management.

What is a “Memory leak” in Java?

In Java, when a developer wants to create and use a new object using, e.g. new Integer(5), he doesn’t have to allocate memory – this is being taken care of by the Java Virtual Machine (JVM). During the life of the application JVM periodically checks which objects in memory are still being used and which are not. Unused objects can be discarded and memory reclaimed and reused again. This process is called garbage collection and the corresponding piece of JVM is called a Garbage Collector or GC.

How can GC distinguish between the unused objects and the ones the application will use at some point in time in the future? The basic algorithm can be described as follows:
  1. There are some objects which are considered “important” by GC. These are called GC roots and are (almost) never discarded. They are, for example, currently executing method’s local variables and input parameters, application threads, references from native code and similar “global” objects.
  2. Any object referenced from those GC roots are assumed to be in use and not discarded. One object can reference another in different ways in Java, most commonly being when object A is stored in a field of object B. In that case, we say “B references A”
  3. The above process is repeated until all objected that can be transitively reached from GC roots are visited and marked as “in use”
  4. Everything else is unused and can be thrown away.


LDAP and Active directory

Active Directory( Microsoft's implementation) is a database based system that provides authentication, directory, policy, and other services in a Windows environment

LDAP (Lightweight Directory Access Protocol) is an application protocol for querying and modifying items in directory service providers like Active Directory, which supports a form of LDAP.


OOPS Concept.

OOPS Concepts are mainly 4 
1.Abstraction
2.Encapsulation
3.Inheritance 
4.Polymorphisam


Abstraction:-Hidding non-essential features and showing the
essential features
(or)
Hidding unnecessary data from the users details,is called
abstraction.
Real Time example:TV Remote Button 


Encapsulation: Writing Operations and methods stored in a single
class.This is Called Encapsulation

Real Time Example:Medical Capsuals
Inheritance: The New Class is Existing from Old Class,i.e SubClass is
Existing from Super Class.

Real Time Example: Father and Son Relationship

Polymorphisam: Single Form behaving differently in different Situations.

**
A Washing Machine and It's Power Button
What is the function that power button does? Switches the machine on (obviously). But did u ever imagined the inside mechanism. Doesn't matter unless it's functioning well. That's encapsulation. The object is wrapped and inner details are hidden. Only thing is that object can be called and used. User friendly!
Second, A Postman
He is not bothered what is inside your parcel. He's job is to read the address and deliver. It's abstraction. It is safe isn't it? Just get a job done by providing few details.

Association, Aggregation, Composition:
  • Association is a relationship where all objects have their own lifecycle and there is no owner.
Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers, but there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
Association - I have a relationship with an object. Foo uses Bar
public class Foo { 
    void Baz(Bar bar) {
    } 
};

  • Aggregation is a specialised form of Association where all objects have their own lifecycle, but there is ownership and child objects can not belong to another parent object.
    Let's take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department, the teacher object will not be destroyed. We can think about it as a “has-a” relationship.
For two objects, Foo and Bar the relationships can be defined
Association - I have a relationship with an object. Foo uses Bar
public class Foo { 
    void Baz(Bar bar) {
    } 
};
Composition - I own an object and I am responsible for its lifetime, when Foo dies, so does Bar
public class Foo {
    private Bar bar = new Bar(); 
}
Aggregation - I have an object which I've borrowed from someone else. When Foo dies, Bar may live on.
public class Foo { 
    private Bar bar; 
    Foo(Bar bar) { 
       this.bar = bar; 
    }
}
[Bean model classes in java application]
  • Composition is again specialised form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have its lifecycle and if parent object is deleted, all child objects will also be deleted.
    Let's take again an example of relationship between House and Rooms. House can contain multiple rooms - there is no independent life of room and any room can not belong to two different houses. If we delete the house - room will automatically be deleted.
    Let's take another example relationship between Questions and Options. Single questions can have multiple options and option can not belong to multiple questions. If we delete the questions, options will automatically be deleted.
Composition - I own an object and I am responsible for its lifetime, when Foo dies, so does Bar
public class Foo {
    private Bar bar = new Bar(); 
}
[Main business classes in java application]

Abstraction and Encapsulation

http://www.java67.com/2012/08/difference-between-abstraction-and-encapsulation-java-oops.html