Sei sulla pagina 1di 5

A nested class is a member of its enclosing class.

Non-static nested classes (inner classes) have


access to other members of the enclosing class, even if they are declared private. Static nested
classes do not have access to other members of the enclosing class. As a member of
the OuterClass, a nested class can be declared private, public, protected, or package
private. (Recall that outer classes can only be declared public or package private.)

Why use nested classes ?


o It is a way of logically grouping classes that are only used in one place
o It increases encapsulation
o It can lead to more readable and maintainable code
Stack and heap are the memories allocated by the OS to the JVM that runs in the
system.Stack is a memory place where the methods and the local variables are
stored. (variable references either primitive or object references are also stored in
the stack). Heap is a memory place where the objects and its instance variable are
stored.
The only situation where an int-valued expression can be assigned to a byte without
a type-cast is when the expression is a compile-time constant expression AND the
actual value of the expression is in the range of byte.

if (obj.getClass().isInstance(Statement.class)) {
doStuffWithStatements((Statement) obj));
}
if (obj instanceof C) {
//your code
}

All but Smalltalk allow classes to implement multiple protocols


// File Name: Singleton.java
public class Singleton {

private static Singleton singleton = new Singleton( );

/* A private Constructor prevents any other


* class from instantiating.
*/
private Singleton() { }

/* Static 'instance' method */


public static Singleton getInstance( ) {
return singleton;
}

/* Other methods protected by singleton-ness */

protected static void demoMethod( ) {


System.out.println("demoMethod for singleton");
}
}
// File Name: SingletonDemo.java
public class SingletonDemo {

public static void main(String[] args) {


Singleton tmp = Singleton.getInstance( );
tmp.demoMethod( );
}
}

Generalization is the process of extracting shared characteristics from two or more


classes, and combining them into a generalized superclass. Shared characteristics can
be attributes, associations, or methods.
In contrast to generalization, specialization means creating new subclasses from an
existing class. If it turns out that certain attributes, associations, or methods only apply to
some of the objects of the class, a subclass can be created. The most inclusive class in
a generalization/specialization is called the superclass and is generally located at the top
of the diagram. The more specific classes are called subclasses and are generally
placed below the superclass.

Composition is the design technique to implement has-a relationship in


classes. We can use java inheritance or Object composition for code reuse.

When resolving overloaded methods if exact method is not available the we do


not get any compile time error immediately, the compiler first promotes the
arguments to the next level and checks whether the matched method is available,
if its available then compiler considers it, otherwise promotes it to next level. This
process will continue untill all possible promotions are completed. If no match is
found we would get compiler error!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class Sample {
public static void m1(int i) {
System.out.println("Int arg");
}
public static void m1(float f) {
System.out.println("Float arg");
}
public static void main(String [] args) {
Sample s1 = new Sample();
s1.m1(10); // Int-arg
s1.m1(10L); //Float arg
s1.m1(10.5f); //Float arg
s1.m1(10.5); // compiler error "Cannot find symbol"
}
}

To illustrate this lets modify the above example a bit


In the above example you can notice a long is automatically promoted to Float
and hence we get output as Float arg , but a double cannot be downgraded to
float hence compiler error.
2. In overloading, child class will get more priority than object class.
Lets take a couple of examples to illustrate this point more clearly
1
2
3 class Test
4 {
public void m1(Object o)
5
{
6
System.out.println("Object here");
7
}
8
9
public void m1(String s)
10
{
11
System.out.println("String here");
12
}
13
14
public static void main(String[] args)
15
{
16
Test t = new Test();
17
t.m1("Harsha"); //String here
18
t.m1(new Object()); //Object here
19
t.m1(null); //String here
20
}
}

As you can see in above example, when we pass null in m1() String version
method got executed, since the child class methods gets more priority.
Nows lets take another example
1 class Test
2 {
public void m1(StringBuffer sb)
3
{
4
System.out.println("String Buffer");
5
}
6
7
public void m1(String s)
8
{
9
System.out.println("String here");
10
}
11
12
public static void main(String[] args)
13
{
14
Test t = new Test();
15
t.m1("Harsha"); // String version
16
t.m1(new StringBuffer("Harsha")); //StringBuffer Version
17
t.m1(null); // Compiler Error - reference to null is ambigious
18
}
19 }
20

As we can notice, in the above example we will get compile time error when we
try to pass null since both String and StringBuffer are subclasses of object.
3. In overloading method resolution is always taken care by compiler
based on reference type. Runtime object wont play any role.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class Animal
{
}
class Monkey extends Animal
{
}
class Test
{
public void m1(Animal a)
{
System.out.println("Animal version");
}
public void m1(Monkey m)
{
System.out.println("Monkey version");
}
public static void main(String[] args)
{
Test t = new Test();
Animal a = new Animal();
t.m1(a); //Animal Version
Monkey m = new Monkey();
t.m1(m); //Monkey version
a = new Monkey();
t.m1(a); // Animal version
}
}

In last method even when we pass a Monkey object the output is Animal
version since the reference type is of Animal and compiler only cares about
reference type.
In overriding method names and argument types must be matched i.e., method
signatures must be same.
Final methods cannot be overridden. Final Classes cannot be inherited. Abstract classes
cannot be instantiated. Abstract methods must be overridden. If a method is abstract
then its class must be abstract. But if a class is abstract all its methods need not be
abstract.
Attributes are like object variables. Operations are not methods but high level
operational functions.
Multiplicity is 0..1, 1, *, 0..*, 1..*.
Single line Association
Single line with hollow diamond Aggregation
Single line with filled diamond Composition

Sequence diagram shows time ordering of sequence of method calls/communication


patterns/message sequencing.
-------- indicate created. Rectangle indicates active.
Collaboration diagram is similar to Sequence diagram but focuses on Structural
Organization of objects rather than Time ordering of method calls/communication
patterns/message sequencing.
In my words, Sequence diagram Timeline, Collaboration Diagram Normal Diagram
Statechart diagram State diagram. Behavior of system.
Oval shape indicates Activity
Activity diagram extends State Chart Diagram
Component Diagram is a physical and replaceable part of the system that confronts to
and provides realization of set of interfaces like packages, libs, classes, database tables
etc.,
Deployment diagram shows components of runtime processing nodes, addresses static
deployment tools. Use cubes in this diagrams
CRC cards suitable for group discussions.
Very difficult for developers to come to efficient solution. So Design patterns.
Frameworks are partially filled systems which can be filed as per requirement.
Patterns are more abstract than frameworks.
Frameworks have many patterns but not vice versa.
Patterns normally dont have implementations.
Patterns should contain the following Name, Problem Context, Forces,
Solutions

Potrebbero piacerti anche