Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Object oriented programming Inheritance Polymorphism Global Varaibles Memory Encapsulation allocation Data types Type casting

here in the OOP programming model programs are organized around objects and data rather than actions and logic. Objects represent some concepts or things and like any other objects in the real Objects in programming language have certain behavior, properties, type, and identity. In OOP based language the principal aim is to find out the objects to manipulate and their relation between each other. OOP offers greater flexibility and compatibility and is popular in developing larger application. Another important work in OOP is to classify objects into different types according to their properties and behavior. So OOP based software application development includes the analysis is a object oriented programming and to understand the functionality of OOP in Java, we first need to understand several fundamentals related to objects. These include class, method, inheritance, encapsulation, abstraction, polymorphism etc Inheritance - This is the mechanism of organizing and structuring software program. Though objects are distinguished from each other by some additional features but there are objects that share certain things common. In object oriented programming classes can inherit some common behavior and state from others. Inheritance in OOP allows to define a general class and later to organize some other classes simply adding some details with the old class definition. This saves work as the special class inherits all the properties of the old general class and as a programmer you only require the new features. This helps in a better data analysis, accurate coding and reduces development time. Abstraction - The process of abstraction in Java is used to hide certain details and only show the essential features of the object. In other words, it deals with the outside view of an object (interface). Encapsulation - This is an important programming concept that assists in separating an object's state from its behavior. This helps in hiding an object's data describing its state from any further modification by external component. In Java there are four different terms used for hiding data constructs and these are public, private, protected and package. As we know an object can associated with data with predefined classes and in any application an object can know about the data it needs to know about. So any unnecessary data are not required by an object can be hidden by this process. It can also be termed as information hiding that prohibits outsiders in seeing the inside of an object in which abstraction is implemented.

Polymorphism - It describes the ability of the object in belonging to different types with specific behavior of each type. So by using this, one object can be treated like another and in this way it can create and define multiple level of interface. Here the programmers need not have to know the exact type of object in advance and this is being implemented at runtime TYPECASTING Type Casting refers to changing an entity of one datatype into another. This is important for the type conversion in developing any application . If you will store a int value into a byte variable directly, this will be illegal operation. For storing your calculated int value in a byte variable you will have to change the type of resultant data which has to be stored. This type of operation

public class conversion{ public static void main(String[] args){ boolean t = true; byte b = 2; short s = 100; char c = 'C'; int i = 200; long l = 24000; float f = 3.14f; double d = 0.000000000000053; String g = "string"; System.out.println("Value of all the variables like"); System.out.println("t = " + t ); System.out.println("b = " + b ); System.out.println("s = " + s ); System.out.println("c = " + c ); System.out.println("i = " + i ); System.out.println("l = " + l ); System.out.println("f = " + f ); System.out.println("d = " + d ); System.out.println("g = " + g ); System.out.println(); //Convert from boolean to byte. b = (byte)(t?1:0); System.out.println("Value of b after conversion : " + b); //Convert from boolean to short. s = (short)(t?1:0); System.out.println("Value of s after conversion : " + s); //Convert from boolean to int. i = (int)(t?1:0); System.out.println("Value of i after conversion : " + i); //Convert from boolean to char. c = (char)(t?'1':'0');

System.out.println("Value c = (char)(t?1:0); System.out.println("Value //Convert from boolean to l = (long)(t?1:0); System.out.println("Value //Convert from boolean to f = (float)(t?1:0); System.out.println("Value //Convert from boolean to d = (double)(t?1:0); System.out.println("Value //Convert from boolean to g = String.valueOf(t); System.out.println("Value g = (String)(t?"1":"0"); System.out.println("Value int sum = (int)(b + i + l System.out.println("Value } }

of c after conversion : " + c); of c after conversion in unicode : " + c); long. of l after conversion : " + l); float. of f after conversion : " + f); double. of d after conversion : " + d); String. of g after conversion : " + g); of g after conversion : " + g); + d + f); of sum after conversion : " + sum);

Download Type Casting Example

INHERITANCE inheritance means to take something that is already made. It is one of the most important feature of Object Oriented Programming

y y y

Simple Inheritance Multilevel Inheritance Multiple Inheritance

Java does not support multiple Inheritance


y

The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface. In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

class A{ int a; float b; void Show(){ System.out.println("b in super class: } } class B extends A{ int a; float b; B( int p, float q){ a = p; super.b = q; } void Show(){ super.Show(); System.out.println("b in super class: System.out.println("a in sub class: } public static void main(String[] args){ B subobj = new B(1, 5); subobj.Show(); } }

" + b);

" + super.b); " + a);

Output: C:\>java B b in super class: 5.0 b in super class: 5.0 a in sub class: 1 OPERATORS Operators are symbols that performs some operations on one or more than one operands. Once we declare and initialize variables, we can use operators to perform certain tasks like addition, subtraction etc. Simple Assignment Operator Arithmetic Operators + Additive operator (also used for String concatenation) Subtraction operator * Multiplication operator / Division operator

Remainder operator Unary Operators

Unary plus operator; indicates positive value (numbers are positive without this, however) Unary minus operator; negates an expression ++ Increment operator; increments a value by 1 -- Decrement operator; decrements a value by 1 ! Logical compliment operator; inverts the value of a boolean Equality and Relational Operators == Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Conditional Operators && Conditional-AND || Conditional-OR Ternary (shorthand for if-then-else statement) Type Comparison Operator instanceof Compares an object to a specified type Bitwise and Bit Shift Operators ~ Unary bitwise complement << Signed left shift >> Signed right sift >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR

?:

These operators follow some precedence to apply. The table below shows the list of operators that follow the precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. However, Operators on the same line have equal precedence. The rule to deal with equal precedence operators is that all binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operators postfix unary multiplicative additive shift relational equality bitwise AND bitwise exclusive OR bitwise inclusive OR logical AND logical OR ternary assignment

Precedence expr++,, expr-++expr, --expr, +expr, expr ~ ! */% +<< >> >>> < > , <= , >= instanceof == , != & ^ | && || ?: = , +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>, >=

As we discussed that the Java programming language are divided into two categories :
y y

Primitive Data Types Reference Data Types

Lets have a discussion about Reference Data Types in brief

In Java

a reference data type is a variable that can contain the reference or an address of dynamically created object. These type of data type are not predefined like primitive data type. The reference data types are arrays, classes and interfaces that are made and handle according to a programmer

in a java program which can hold the three kind of values as:

Whenever a variable is created, a reference to an object is also created using the name of a class for its type i.e. that variable can contain either null or a reference to an object of that class. It is not allowed to contain any other kinds of values. Such type is called reference types in Java. The object becomes an instance when the memory is allocated to that object using new keyword . In addition, array types are reference types because these are treated as objects in Java. For example:

Array Type: An array is a special kind of object that contains values called elements. The java array enables the user to store the values of the same type in contiguous memory allocations. The elements in an array are identified by an integer index which initially starts from 0 and ends with one less than number of elements available in the array. All elements of an array must contain the same type of value i.e. if an array is a type of integer then all the elements must be of integer type. It is a reference data type because the class named as Array implicitly extends java.lang.Object. The syntax of declaring the array is shown a Interface Type: Java provides an another kind of reference data type or a mechanism to support multiple inheritance feature called an interface. The name of an interface can be used to specify the type of a reference. A value is not allowed to be assign to a variable declared using an interface type until the object implements the specified interface. When a class declaration implements an interface, that class inherits all of the variables and methods declared in that interface. So the implementations for all of the methods declared in the interface must be provided by that class. For example, Java provides an interface called ActionListener whose method named actionPerformed() is used to handle the different kind of event . Java also provides a class called Thread that implements Runnable interface. Thus the following assignment can be allowed: Runnable r; r = new Thread();

float The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. double

integer point literals 0 1 123 -42000 Floating point literals

10.0003 48.9 -2000.15 7.04e12

IDENTIFIERS

Identifier is is a simple variable name which is defined as the value container. The type of value stored by identifier is defined by the special java keyword is termed as primitive data type

You might also like