Tuesday, February 3, 2015

Features of Object Oriented Programming

Posted by Sona Mathews
•    Classes and Objects
•    Abstraction and Encapsulation
•    Inheritance
•    Reusability
•    Polymorphism
Features of Object Oriented Programming : Java effect
Features of Object Oriented Programming : Java effect

Classes in Java: A class is a template or a blueprint from which individual objects are created. Class is thought of as a datatype and object is variable for that datatype.

  Class Person
   (Attributes)                 
name
   age
eating()
walking()
                                                 (Behavior)
(person)
James

(person)
Kevin

(person)
smith
             (Object)                        (Object)                      (Object)

Objects in Java: Objects are used to represent the real world entity and are distinctly indentified. Objects are an instance of a class. Object represents the run time entities in object oriented system. An object can be anything that we see in the real world-car, chair, book, student, apple etc.
•    Three characteristics of objects are:
•    Objects have unique identity, attributes and behaviors.
•    An object is formed by binding Data and Methods together.
•    Message Passing concept is used by objects to communicate with each other.

Abstraction: It is a process that identifies the essential features without including the internal or background details. Complexity is managed by the use of abstraction. For Example: Consider a TV remote which is an object and has features like change volume, change channel etc. Users interact with TV by using the interfaces without going into the background or internal details.

Encapsulation: It is a mechanism of wrapping up of data members (fields, instance variables) and member function (methods, functions) in order to keep it safe from unauthorized access. Data Hiding is accomplished by using encapsulation.

Inheritance: It is the process of inheriting parent class properties by child class. It is like deriving a new class from existing class without changing it. A new class is called-child/sub/derived for the existing class-parent/super/base respectively.

 Reusability: Inheritance provides an extension to Reusability. Once a class has been written the derived class can reuse the features of base class rather than writing from the scratch. For Example: The derived class from Employee base class can reuse the common features (id, name, salary).

Polymorphism: It can be defined as the ability to use the same name for many objects that performs technically different tasks. It is the ability to take many forms. It reduces the complexity by using the same name to perform different tasks.

First Java Program to Print Hello:

•    Know let’s begin with a simple java program to print Hello.


•    Use text editor notepad which is for beginners and you can also use NetBeans which is an IDE (Integrated Development Environment) for typing the program.

•    Compile the program – The Next step is to compile the program using Java compiler (javac).
C :\> javac Hello.java // type this at command prompt

•    To Execute – Last step is to use the Java Interpreter (java) for executing the program.

C :\> java Hello // for runtime execution of program

Output : hello

•    Explanation:

•    Class: Here class is a keyword which is used to declare the class in the program.
•    Hello: This is the name of the class which is to be used in the program.
•    Public: public is a keyword or an access specifier that indicates that the method been declared is visible to all and can been access from anywhere in the program.
•    Static: static is a keyword that allows main() to be called before any object of the class been created.
•    Void: void is a returntype that indicates that the main() being declared does not return a value.
•    String args[]: It is the only parameter in the main method. args[] is the array of objects of the class type String.
•    System.out.println: System here is the class. The out object represents the standard output device and println is the method which is used to display the output in the new line.

Data types and Variables in Java:

Data type: The data type tells what operations can be performed on data and how much memory is allocated to store data. The data types are classified into two broad categories –
•    Primitive type (or built-in type or value type)
•    Non-Primitive type (reference or derived type).

                    


                     PRIMITIVE DATA TYPES
1.    Integer Type – byte, short, int, long.
2.    Floating Point Type – float, double.
3.    Character Type – char.
4.    Boolean Type – Boolean.

                  

                  NON-PRIMITIVE DATA TYPES

1.    String
2.    Array
3.    Class

Integers: Java defines four integer types – byte, short, int, and long. All of these are positive, signed, and negative values. Java does not support unsigned integers.

Integer Types
Name
Size
Range
Default Value
short
16bit (1 byte)
-32,768 to 32,767
0
long
64bit (8 bytes)
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0L
int
32bit (4 bytes)
–2,147,483,648 to 2,147,483,647
0
byte
8bit (1 byte)
-128 to 127
0

•    Byte is the smallest integer type in Java. Byte type of a variable is useful when we are working with a stream of data from a file or network.
•    Short is the least used data type in Java. Short is applicable to 16-bit computers

Floating Point Data types: Floating point numbers are also known as real numbers and are used with expressions that require fractional precision. The examples are – square root, sine cosine etc. There are two kinds of floating point – float and double.


Name
Size
Range
Default value
float
32 bit (4 bytes)
-3.4E38 to +3.4e38
0.0f
double
64 bit (8 bytes)
-1.7E308 to +1.7E308
0.0d

Character Data type: char is used to store the characters in Java. In Java char is a 16-bit type. The Range of char is 0 to 65,536. There are no negative chars. The default value of char is ‘\u0000’.

Boolean Data type: It can have only one or two possible values i.e. True or False. This type is returned by all the relational operators. True and False values are literals. The default value of Boolean is false.

Variables in Java

 Introduction: A Variable is a symbolic name which is used to store the value. It is capable of storing only one value. The value of a variable changes during the program execution.
Variable Declaration: All the variables must be declared before the can be used in the program.

Syntax:          data_type var_name1, var_name 2,……var_name n;
Variable Initialization: once the variable is declared, it must be initialized.
 Syntax: Data_type var_name = value;

Conventions used for variables:

•    Variable name should be unique.
•    Variable name must be a character.
•    It cannot start with a digit.
•    No spaces required in a variable name.
•    Cannot use special character except (_) underscore
•    Cannot use keywords which are reserved words.

0 comments:

Post a Comment