Oops and enums

32
OOPS, ENUMS, Inner Classes, GarbageCollection Satyam Shrivastav http:// programmingpoints.blogspot.in / 1

Transcript of Oops and enums

OOPS, ENUMS, Inner Classes, GarbageCollection

Satyam Shrivastavhttp://programmingpoints.blogspot.in/

1

ASSOCIATION

Association is a relationship between two objects. Objects might not be completely dependent on

each other. One-to-many, many-to-one, many-to-many all

these words define an association between objects

Example: A Student and a Faculty are having an association.

Satyam Shrivastav http://programmingpoints.blogspot.in/

2

AGGREGATION

A directional association between objects. Aggregation can be considered as a “has-

a” relationship. Child object can also survive or exist without the

enclosing class.  For Example, Room has a table, but the table can

exist without the room.

Satyam Shrivastav http://programmingpoints.blogspot.in/

3

COMPOSITION

A restricted aggregation is called composition.

 The member object (part) cannot exist without the containing class. 

For example, A class contains students. A student cannot exist without a class. There exists composition between class and students.

Satyam Shrivastav http://programmingpoints.blogspot.in/

4

UML Diagrams of Relationships

AssociationAssociation is denoted by simple

arrow

Aggregationaggregation is denoted by empty

diamond head arrow

Compositioncomposition is denoted by filled

diamond head arrow

Satyam Shrivastav http://programmingpoints.blogspot.in/

5

Difference Between Composition And Aggregation When there is a composition between two objects,

the composed object cannot exist without the other object. 

In case of Aggregation, Though one object can contain the other object,

there is no condition that the composed object must exist.

For Ex: Facebook has-a-User i.e. Aggregation Every User has a different Session i.e.

Composition.

Satyam Shrivastav http://programmingpoints.blogspot.in/

6

METHODS OVERRIDING

Overriding is a feature of OOP languages like Java that is related to run-time polymorphism.

Method overriding is when a child class redefines the same method as a parent class, with the same parameters.

The key benefit of overriding is the ability to define behavior that is specific to a particular

subclass type.

Satyam Shrivastav http://programmingpoints.blogspot.in/

7

The rules for overriding a method are as follows:

The argument list must exactly match that of the overridden method.

Overriding method CAN throw any unchecked runtime exception.

You cannot override a method marked final. You cannot override a method marked static. If a method can't be inherited, you cannot

override it.

Satyam Shrivastav http://programmingpoints.blogspot.in/

8

METHOD OVERLOADING

Overloading is also a feature of OOP languages like Java that is related to compile time (or static) polymorphism.

Method overloading is defining several methods in the same class, that accept different numbers and types of parameters.

In this case, the actual method called is decided at compile-time, based on the number and types of arguments.

Satyam Shrivastav http://programmingpoints.blogspot.in/

9

Overloading Rules

Overloaded methods MUST change the argument list.

Overloaded methods CAN change the return type.

Overloaded methods CAN change the access modifier.

Overloaded methods CAN declare new or broader checked exceptions.

A method can be overloaded in the same class or in a subclass.

Satyam Shrivastav http://programmingpoints.blogspot.in/

10

ENUM,INNER CLASSES

Satyam Shrivastav http://programmingpoints.blogspot.in/

11

Enum

An enumeration, or “enum” is simply a set of constants to represent various values.

An enum type is a special data type that enables for a variable to be a set of predefined constants.

The variable must be equal to one of the values that have been predefined for it.

enums extend java.lang.Enum and implement java.lang.Comparable.

Hence, enums can be sorted. Enums override toString() and provide valueOf().

Satyam Shrivastav http://programmingpoints.blogspot.in/

12

Defining Enum

Old way of doing it:public final int SPRING = 0;public final int SUMMER = 1;public final int FALL = 2;public final int WINTER = 3;

New way of doing it:enum <enumname>{}

Satyam Shrivastav http://programmingpoints.blogspot.in/

13

Advantages

Enums provide compile-time type safety. Enums provide a proper name space for the

enumerated type. Enums are robust. Enum printed values are informative Because enums are objects, you can put them in

collections. Because enums are classes, you can add fields

and methods.

Satyam Shrivastav http://programmingpoints.blogspot.in/

14

Inner Class

The class defined inside another class or interface is called inner class

We can also create an interface in another class or interface.

For example

class Example{class Sample{}

}

class Example{interface Sample{}

}

interface Example{class Sample{}

}Satyam Shrivastav

http://programmingpoints.blogspot.in/15

Need of Inner Class

Inner class is used for creating an object logically inside another object with clear separation of properties region.

A inner class has access to the variables and methods of the outer class, even if they are declared private.

Nested classes can be hidden from other classes in the same package.

Satyam Shrivastav http://programmingpoints.blogspot.in/

16

Nested class (static inner class)

The inner class defined at class level with static keyword is called static inner class.

Syntax:

Allowed Modifiers:private, protected, public, final, abstract,

strictfp Types of Members allowed:

* static variable * non-static variable* static block * non-static block* static method * non-static method* main method * constructor

class Example{static class A{}

}

Satyam Shrivastav http://programmingpoints.blogspot.in/

17

Inner class (non-static inner class)

The inner class defined at class level without static keyword is called non-static inner class.

Syntax:

Allowed Modifiers:private, protected, public, final, abstract,

strictfp Types of Members allowed:

* non-static variable* non-static block* non-static method* constructor

class Example{class

A{}}

Satyam Shrivastav http://programmingpoints.blogspot.in/

18

Method Local class (local inner class)

The inner class defined inside a method of outer class called method inner class.

Syntax:

Allowed Modifiers:final, abstract, strictfp

Types of Members allowed:* non-static variable* non-static block* non-static method* constructor

class A{void m1(){

class B{}}

}

Satyam Shrivastav http://programmingpoints.blogspot.in/

19

Anonymous class (argument inner class)

It is a nameless subclass of some other existed class/interface.

Like other inner classes it is not individual class.

Using anonymous class we can do 3 things at a time-1. Inner class creation as a subclass of outer

class.2. Overriding outer class method.3. Creating and sending its object as argument

or return type to another method.Satyam Shrivastav

http://programmingpoints.blogspot.in/20

Syntax:

Allowed Modifiers:no modifier is allowed

Types of Members allowed:* non-static variables* non-static blocks* non-static methods

new outerclassname(){//overriding outer class

methods}

Satyam Shrivastav http://programmingpoints.blogspot.in/

21

Cohesion,Coupling And Garbage Collection

Satyam Shrivastav http://programmingpoints.blogspot.in/

22

Cohesion

Defintion:- Cohesion means that a certain class performs a

set of closely related actions. Cohesion focuses on how single class is designed.

Types of Cohesion:- High Cohesion Low Cohesion

Satyam Shrivastav http://programmingpoints.blogspot.in/

23

High cohesion:-High cohesion is when you have a class that does a

well defined job.Low cohesion:-Low cohesion is when a class does a lot of jobs that

don't have much in common. Higher the cohesiveness of the class, better is the

OO design.

Satyam Shrivastav http://programmingpoints.blogspot.in/

24

Example:- You have a class that adds two numbers, but the

same class creates a window displaying the result.

This is a low cohesive class because the window and the adding operation don't have much in common.

The window is the visual part of the program and the adding function is the logic behind it.

Satyam Shrivastav http://programmingpoints.blogspot.in/

25

What is Coupling?

Coupling:- Coupling is the degree to which one class knows

about another class. It refers to how related are two classes / modules

and how dependent they are on each other.

Types of Coupling:- Tight coupling Loose coupling

Satyam Shrivastav http://programmingpoints.blogspot.in/

26

Tight coupling:- Tight coupling is when a group of classes are

highly dependent on one another.

Loose coupling:- Loose coupling would mean that changing

something major in one class should not affect the other.

Generally, good OO design should be loosely coupled and highly cohesive.

Satyam Shrivastav http://programmingpoints.blogspot.in/

27

What is Garbage Collection ?

Garbage collection is the process of identifying which objects are in use and which are not, and deleting the unused objects.

In Java, process of deallocating memory is handled automatically by the garbage collector.

This enables faster development with less code, eliminate memory leaks and other memory-related problems.

Satyam Shrivastav http://programmingpoints.blogspot.in/

28

The ways to make an Object eligible for GC:-

Even though the programmer is not responsible for distruction of Object. It is a good programming practice to make our object is eligible for the Garbage Collection, if it is no longer required.

Nullifying the reference variable :- Re-assigning the reference variable:- The Object created inside a method… are by

default eligible for GC.

Satyam Shrivastav http://programmingpoints.blogspot.in/

29

When the Garbage Collector Runs?

The garbage collector is under the control of the JVM. The JVM will typically run the garbage collector when it

senses that memory is running low. User can request the JVM for garbage Collection by Calling “System.gc()”. User can rely on ‘System.gc()’ to free up enough memory

without worrying for running out of memory. But the garbage collector will run before it throws an

OutOfMemoryException.

Satyam Shrivastav

http://programmingpoints.blogspot.in/30

Role of finalize() method in GC

Java has a mechanism to run some code just before your object is deleted by the garbage collector.

This code is located in a method named finalize() that all classes inherit from class Object. For any given object, finalize() will be called only

once (at most) by the garbage collector. Calling finalize() can actually result in saving an

object from deletion.

Satyam Shrivastav http://programmingpoints.blogspot.in/

31

“Thank You”

Satyam Shrivastav http://programmingpoints.blogspot.in/

32