Introduction to OOP concept-1
Introduction to OOP concept-1
POP OOP
Process of dividing a program into number of Process of dividing a program into number of
methods. objects
Not suitable for solving complex problems Suitable for solving complex problems
1. Data Abstraction- It is the process of showing only the essential features of an object while hiding
the background details.
Eg- While withdrawing money from the ATM we only have to enter the pin, amount to be withdrawn.
Rest of the details remains hidden.
Ans. By defining a class and implementing only what is essential for creating an object.
2. Encapsulation- It is the process of wrapping data and functions into a single unit.
Eg: Consider ATM as an object, it combines data like Account number, balance, pin etc. and methods
like deposit, transfer, withdraw etc. into a single unit.
3. Inheritance- It is the process of transferring or sharing properties from once class to another.
Ans. By creating a base/super class and deriving the properties into a subclass using the keyword
extends.
4. Polymorphism- When the same object or method can take multiple forms.
Eg: Object mobile phone can be used for calling , texting, video conferencing etc.
Abstraction is hiding details by not implementing the non-essential in a class details whereas
Encapsulation is hiding data by wrapping the details within an object, thus securing from
unauthorized access.
Ans. When an object is created, it acts like a black box by only show. ing the essential details and the
internal mechanism is hidden from the user.
Eg.- Object pen has characteristics like colour, type, model and behaviour like used for writing.
Class as an object factory- A class can produce multiple objects of the same type.
Benefits of class
Ans. Objects of a class communicate with each other by passing messages through methods. This is
called message passing.
Class as a blueprint-A class acts as a blueprint that represents a set of objects that share common
characteristics and behaviour.
Class as a user defined data type- Class contains data members and methods that can be accessed
and used by creating an instance of class. It is also called user defined/composite data type as it
contains data members(variables) of different data types.
Object Class
the memory)
Unicode- The character set of Java. It is a 2 byte character set used to represent almost all characters
in languages around the world.
ASCII- is a code that uses numbers to represent characters. 0 to 127 [128 characters]
A-Z=65-90
a-z=97-122
0-9=48-57
Escape sequence:
It is a combination of characters tha has a special meaning to the Java compiler. It is preceded by a
slash followed by one or more characters.
Keywords- reserved words that convey a special meaning to the java compiler.
Rules:
Types of literals
5. boolean-true/false
Operators-special symbols that are used to perform predefined operations. Eg- +,−¿
Byte 1 byte 7
−2 ¿ +2 −1
7
0
Short 2 bytes 15
−2 ¿+2 −1
15
0
Int 4 bytes 31
−2 ¿ +2 −1
31
0
Long 8 bytes 63
−2 ¿+ 2 −1
63
0L
Non Null
primitive
Variable- named location in the memory that can store data temporarily.
Declaring variable:
Eg: class A{
once a variable is defined as constant, the value of the variable becomes constant and no new value
can be assigned to that variable.
Type conversion- converting variable of one type to another
Eg: int+float=float
long+double=double
int+char=int
Example;
c=(double)a/b;
Forms of operators:
ARITHMETICAL+ ,−,∗,/, % ,
LOGICAL∧¿ ,∨¿
3. TERNARY-Operates on 3 operands- ? :
Types of operators:
Logical operator-returns true/false after comparing the result of one or expressions. ¿∧,∨¿ ,!
Assignment- assigns a value from right side of an expression to left side operand- ¿
Short hand assignment +¿ ,−¿ ,∗¿ , %=,/¿
Operator precedence
˙
1.B-Brackets (), {},[ ], (.)
2. U- Unary ++,−−,+,−, !
3. N-new
6. L- Logical ¿∧,∨¿
7. T- Ternary ? :
Associativity- operators with same precedence are evaluated from either left to right or right to left.
Types of input:
nextLine()-accepts a sentence. [ reads sequence of characters including space until enter key is
pressed]
Errors
1. Syntax errors- Errors due to incorrect syntax in a program such as missing semicolon, brackets or
misspelt keyword. These errors are detected by java compiler.
2. Logical Errors- Errors due to incorrect logic by the programmer such as wrong use of operator.
These errors can neither be detected by the compiler nor by JVM.
3. Runtime Errors- Errors due to incorrect input while the program is running. Eg- division by zero,
square root of a negative number etc.
Eg Mat h . min(−2,−3)=−3
Eg Mat h . √ ( 4)=2.0
∫ r =¿
Write a java statement to generate random numbers within min value (m) and max value (n)
∫ r =¿
Note- Math class and it’s respective methods are defined in java.lang package which is imported by
default.
Math.sqrt(negative argument)=NaN
Types:
1. if statement- executes a block of statements if the condition is true otherwise control exits and
moves to the next statement .
Syntax:
if(condition/boolean expr.){
statements}
2. if….else – executes the statements in the if block if the test expression in if evaluates to true
otherwise the statements inside else block are executed.
Syntax::
If(condition/boolean expr){
Statements; }
else{
statements;}
3. if…else if- can be used to check multiple conditions. If the first condition evaluates to true, then the
rest of the conditions are skipped otherwise the control moves to the next condition.
Syntax:
If(condition/boolean expression){
Statement}
statement}
else{
statement}
Syntax:
if(boolean expression){
if(boolean expression){
statement}
else{
statement}
Syntax
switch(expr){
case label1:
statement
break;
case label2:
statement
break;
default:
statement}
switch v/s if
if switch
fall through-absence of break statement after one or more cases results in fall through.
Chapter-8 LOOPING/ITERATION
Loop- is a control statement used to perform repetitive tasks.
Types:
1. entry controlled- first condition is checked, later statements inside looping body are executed [for,
while]
2. exit controlled- first statements inside looping body are executed atleast once, later condition is
checked. [do…while]
Syntax:
The ‘while’ loop- when the number of iterations are not fixed
Syntax:
Intialisation;
The ‘do-while’ loop- used when the statements in loop body are to be executed atleast once.
Initiaisation;
do{ statement; update}while(test expr);
null/empty loop- a loop that does not contain any statements in its body.
Infinite loop- a never ending loop whose stop condition always evaluates to true.
Eg for(;;) or while(true)
Jump statements- allow the control to jump from one part of program to another depending on the
result of a boolean expr.
continue- skips the statements after it and resumes for the next iteration/update
Library classes
Wrapper class-predefined classes stored in java.lang package, used to convert from primitive to String and vice
versa.
Byte, Short,Integer,Long,Float,Double,Character,Boolean
Autoboxing- process of automatically converting primitive type into wrapper class object type.
boolean r=Character.isLetter(c);
boolean r=Character.isDigit(c);
boolean r=Character.isLetterOrDigit(c);
boolean r=Character.isUpperCase(c);
boolean r=Character.isLowerCase(c);
char c=Character.toUpperCase(c);
char c=Character.toLowerCase(c);
(counts from 1)
int l=s.length();
char c=s.charAt(2);
String s1=s.replace(‘a’,’*’);
String toUpperCase() Returns the String after converting all its characters
to uppercase if in lowercase.
String toLowerCase() Returns the String after converting all its characters
to lowercase if in uppercase.
String s1=s.substring(start,end);
String s2=s.substring(start);
Would extract from start till end-1
Boolean equals() Returns true if both the Strings are equal without
ignoring their case.
boolean r=s.equals(s1);
Boolean equalsIgnoreCase() Returns true if both the Strings are equal after
ignoring their case.
boolean r=s.equalsIgnoreCase(s1);
Int compareTo() Returns 0 if both the Strings are equal, >0 if first
String is greater, <0 if Second String is greater
without ignoring their case.
int r=s.compareTo(s1);
Int compareToIgnoreCase() Returns 0 if both the Strings are equal, >0 if first
String is greater, <0 if Second String is greater
without ignoring their case.
int r=s.compareToIgnoreCase(s1);
String s3=s1.concat(s2);
String s1=s.trim();
Advantages:
1. reusability of code
2. modularity
3. easy debugging
4. reduces complexity
Types of methods:
1. pure- method that does not modify the state of received parameter.
Eg: int pure(int a){ return a;}
1. method header/prototype- first line of method definition consisting of [access specifier/modifier], return
type, method name and [parameter list]. Eg- public int find(int x)
1. access specifier/ access modifier- defines the visibility of a method/variable from within or outside
the class premises.
2. private-member can be accessed from only within the class in which it is defined.
4. default/package-member without any access specifier. Can be accessed across the same package.
1. Static
only one copy of this variable is shared Each object has distinct copy of this variable.
Not a part of the instance/ It is a part of the object of object of a class. of a class.
byte/short/int/long/float/double/char/boolean/void
void-no return
actual parameters-parameters present in method call statement. pass the value during method call.
Eg- ob.add(10,20);
1. Static binding / early binding- linking method call with method signature during compilation.
1. pass by value/call by value-during a method call actual parameter gets copied into formal parameter,
an changes in the formal parameter doesn’t reflect on the actual parameter.
2. Pass by reference/call by reference- during a method call actual parameter gets copied into formal
parameter, any changes in formal parameter reflects on the actual parameter.
method overloading-more than one method in a class with the same name but differentiated by number or
type of arguments.
class overlaod{
OR
class overload{
void volume()
System.out.println(l*b*h); }
void volume(int r)
System.out.println(4.0/3*Math.PI*r*r*r);
int a[]={10,20,30,40};
int a[][]={{1,2,3},{3,4,5}};
Types
2. Double dimensional array- array elements are stored in a row × column format
Constructor-special method that has same name of class; used to initialize data members of a class.
Types:
1. Default constructor-contructor without any parameter; used to initialize data members with default or
definite values.
Eg. class y{
int a;
y()
A=10;
}}
2. Parameterised constructor-constructor that has parameter in its header; used to initialize data
members with parameter values.
Eg. class y{
int a;
y(int x)
a=x;
}}