+ All Categories
Home > Education > Object Oriented Principles

Object Oriented Principles

Date post: 12-Apr-2017
Category:
Upload: sujit-majety
View: 40 times
Download: 1 times
Share this document with a friend
23
JAVA SUJIT MAJETY | 2 : OBJECT ORIENTED PROGRAMMING CONCEPTS
Transcript
Page 1: Object Oriented Principles

JAVASUJIT MAJETY | 2 : OBJECT ORIENTED PROGRAMMING CONCEPTS

Page 2: Object Oriented Principles

Course Description

Naming Conventions

Data Types

Variables

Data Hiding

Abstraction

Encapsulation

IS-A relationship

HAS-A relationship

Method Signature

Polymorphism

Static

Dynamic

Constructors

Wrapper Classes

Page 3: Object Oriented Principles

Naming Conventions

It is a rule to follow while naming an identifier (e.g. class, package, variable, method, etc.).

By using them readability increases.

Class name: should begin with uppercase and should be a noun.

Interface name: should begin with uppercase and should be an adjective.

Method name: should begin with lowercase and should be a proverb, second word first

character should be capital.

Variable name: should begin with lowercase, second word first character should be a

capital.

Package name: should be completely in lowercase.

Constants name: should be completely in uppercase.

Page 4: Object Oriented Principles

Data Types

Java supports UNICODE character set. Hence, each character is represented in two bytes.

In java every variable has a type, every expression has a type and all assignments should be

checked by the compiler for type compatibility. Hence, java is treated as strongly typed

language.

We are having 8 primitive data types.

These data types fall under 3 categories.

Numeric Data types

Character Data types

Boolean Data types

Page 5: Object Oriented Principles

Data Types (cont.)

Numeric Data types

Integer Data types

Byte – 1 byte

Short – 2 bytes

Int – 4 bytes

Long – 8 bytes

Floating point Data types

Float – 4 bytes

Double – 8 bytes

Character Data types

char – 2 bytes

Boolean Data Types

Boolean – 1 bit

Page 6: Object Oriented Principles

Variables

Variable is name of a memory location.

Types of variables

There are three types of variables

Local variables

Variables that are declared inside a method

Instance variables

Variables declared in class outside the methods.

Static variables

Variables declared in class outside the methods with static keyword.

Page 7: Object Oriented Principles

Data Hiding

Providing security to the data. ie., No outsider can access the data.

By using the private modifier, we can implement the data hiding mechanism.

class User

{

private String password = “password”;

}

Page 8: Object Oriented Principles

Abstraction Is the process of hiding the unnecessary details and exposing only the essential

features of a particular object.

Hiding internal implementation details & just highlight the set of services what we are offering, is called “Abstraction”.

E.g. Car.

Logic of car is abstract to you.

You just know how to use it.

In the same way java methodsinternal details need not be known to you, still you can use it.

Page 9: Object Oriented Principles

Encapsulation Encapsulation is the ability of an object to be a container for related properties and methods.

E.g. Cars and their owners

All the functions of cars are encapsulated with owners

No one else can access it.

class Student{

private name;

public String getName(){

return name;

}

Public void setName(String name){

this.name=name;

}

}

Page 10: Object Oriented Principles

IS-A Relationship Is nothing but inheritance

By using extends keyword we can implement IS-A relationship.

The main advantage of the IS-A relationship is re-usability.

class A{

public void m1(){

…….

}

class B extends A{

public void m2(){

……...

}

Page 11: Object Oriented Principles

HAS-A Relationship

HAS-A relation ship is also known as composition or aggregation.

There is no specific keyword to implement HAS-A relationship. We are using the new

keyword.

The main advantage of Has-A relationship is Re-usability.

And one disadvantage of HAS-A relationship is it increases dependency between the

classes and creates maintenance problems.

class car{

Engine e =new Engine();

}

class Engine{

// Engine specific functionality

}

Page 12: Object Oriented Principles

Method Signature

Method signature consists of name of the method & argument-list.

The first line written in the method definition is said to be the method signature.

public void m1 (int i, float p)

m1(int ,float)

The return type is not part of the method signature.

Compiler will always use method signature while resolving method calls.

With in the same class two methods with the same signature not allowed other wise we will get compile time error.

Page 13: Object Oriented Principles

Method Signature (cont..)

class A{

public void m1(int i){

}

public int m1(int i){

}

}

class Test{

public static void main(String[] args){

A a=new A();

}

Compile Error : method m1(int)

is already defined in class Test

Page 14: Object Oriented Principles

Inheritance

An object of a class acquiring the properties of an object of another class is called

inheritance.

Uses :

Method Overloading

Code Reusability

Types

Single

Multi Level

Hierarchical

Multiple

Page 15: Object Oriented Principles

Inheritance (cont..)

Single

Class A

Class B

Class C

Class CClass B Interface C

Interface BInterface AClass A

Multilevel

Hierarchical Multiple

Page 16: Object Oriented Principles

Polymorphism

Polymorphism is brought up from a greek word which means having more forms.

There are two types of polymorphism.

Static Polymorphism

Dynamic Polymorphism.

Static polymorphism is achieved with the help of method overloading.

Dynamic polymorphism is achieved with the help of method over riding.

Page 17: Object Oriented Principles

Static Polymorphism

Two methods are said to be overloaded iff method names are same but arguments

are different.

This concept in java simplifies the programming.

E.g. : System.out.println();

Page 18: Object Oriented Principles

Dynamic Polymorphism

Method overriding takes place between two IS-A relation dependant classes

having methods with the same signature and same name.

Method over riding is also known as “runtime polymorphism or dynamic

polymorphism or late binding.

Over riding method resolution is also known as “Dynamic method dispatch@.

Rules :

Method signatures must be matched.

Parent class final method cannot be over ridden.

Private methods are not visible in the child classes. Hence over riding concept is not applicable

for private methods.

Page 19: Object Oriented Principles

Differences

Property Overloading Overriding

Arguments Must be different Must be same

Method Signature Must be different Must be same

Return type No restrictions Must be same until 1.4

Private, static & final

methods

Can be overloaded Cannot be overloaded

Access Modifiers No restrictions We can’t decrease scope

Throws Clause No restrictions We can decrease the size and

level for checked exceptions

Method Resolutions Always taken care by

compiler based on reference

type.

Always takes care by jvm

based on runtime object.

Page 20: Object Oriented Principles

Constructors

A special method in a class, having the same name as class without any return type.

Constructor is executed when a class is instantiated, i.e. an object is created.

Constructors cannot be called explicitly.

Used to initialize an object i.e. initialize instance variables as well as class variables.

Compiler provides default constructor if the programmer doesn’t provide a constructor.

If a constructor is specified by the programmer, then compiler doesn’t provide the default constructor.

More than one constructor can be specified in a class, this concept is known as constructor overloading.

The default constructor initializes the instance variables and class variables with default values.

To calculate dynamically how much memory is needed by the object, constructor is used.

Page 21: Object Oriented Principles

Wrapper Classes

Java uses primitive types, such as int, char, double to hold the basic data types supported by the language.

Sometimes it is required to create an object representation of these primitive types.

These classes need to wrap the primitive types in a class.

To satisfy this need, java provides classes that correspond to each of the primitive types.

Basically, these classes encapsulate, or wrap, the primitive types within a class.

Thus, they are commonly referred to as type wrapper. Type wrapper are classes that encapsulate a primitive type within an object.

The wrapper types are Byte, Short, Integer, Long, Character, Boolean, Double, Float.

Page 22: Object Oriented Principles

Important Interview Questions

What is hash code?

How can you find the hash code of an object

Can you declare class as private? Why?

When is constructor called? Before or after creating the object?

What is constructor overloading?

Difference between float and double?

Why do we need wrapper classes?

What is boxing and unboxing?

Page 23: Object Oriented Principles

Questions?


Recommended