Unit 1 - Intro To OOP and Java Fundamentals
Unit 1 - Intro To OOP and Java Fundamentals
1.Elements of OOPs
1. Object – Instance of class | - Run time entities which occupies memory
2. Classes – Collection of objects, attributes, methods.
3. Instance – Obj. created at run time
4. Inheritance – Provides reusability |
5. Data abstraction – Information hiding | refers to particular feature and hiding its
background details | used in s/w design phase.
6. Encapsulation – Binding data and method together | used in s/w implementation |
Inherited
7. Polymorphism –Ability to take more than one form | Types: compile time & run time
8. Message passing – An object sends data to another obj.
2.Characteristics of Java
1. Simple – No pointer concept so easy to debug – auto memory allocation & De-allocation
2. Object Oriented – Almost everything in java is object – it is an entity has attributes,
functions to manipulate.
3. Platform independent – write once, Run anywhere - JVM
4. Dynamic and distributed – java classes can be distributed in networks – java.net
package.
5. Multithreaded – concurrency (run multiple pgm at same time) – Parallel execution –
built in thread class.
6. Robust and secure – good exception handling, explicit methods – array bound checking
7. Interpreted language –source code stored in .java – compiled file in .class(bytecode) –
JVM interprets and executes the program.
Documentation section -
Package stmt -
Import stmt -
Interface stmt -
Class defn. -
-
Main() method class
{
Definitions;
}
Comments:
Variable:
Data type:
(Eight types – Byte “8-Bit, Short “16-B, int “32-B, long “64-B, float “32-B,
double “64-B, char “16-B, Boolean “True or False)
Keywords
Operators:
1. Assignment Operators
2. Arithmetic Operators - + , -
3. Relational Operators - = = , <=
4. Boolean logical Operators - &&, ||, !
2
5. Conditional Operators - ?:
6. Type conversion Operators – Obj. reference
7. Bitwise and bit shift Operators - >>>, ^, ~
8. Increment and decrement Operators - ++, - -
8.Arrays
- Collection of similar type of elements. – Group of elements stored under common name.
Example:
class twodimarray
class onedimarray {
{ public static void main(String args[])
{
public static void main(String args[])
int arr[ ][ ] = {{1,2,3},{4,5,6},{7,8,9}};
{ for(int i=0; i<3; i++)
//dec. and instantiation {
int arr[ ] = {10,20,30,40,50}; for(int j=0; j<3; j++)
//printing array {
for(int i=0; i<arr.length; i++) System.out.print(arr[i][j]+" ");
System.out.println(arr[i]); }
System.out.println();
}
}
} }
}
3
9. Constructors
- It is special method called automatically when an object is created. Class name and
constructor name is same.
Types: i. Default constructor
ii.Parameterized constructors
Properties of constructors
i. No return type. No void.
ii. Must be declared in public
iii. Cannot be virtual
iv. Cannot be inherited
Call by value: calling a method with parameter as value. The changes done to the parameter
don’t reflect in caller’s scope.
Call by reference: calling a method with a parameter as a reference. The changes done to the
parameter reflect in caller’s scope. Java uses only call by value.
4
10. Creating class and objects
- Class is a template/blueprint that describes behaviors of object.
- Object is an instance of class. “New” keyword used
Ex:
Class Demo
{
Psvm..
{
Demo d1; // Creating reference obj
d1= new Demo(); //Object for Demo
}
}
11.Methods
- In java method is equivalent to function.
- Every method must be declared with in class.
- Ex: isEmpty | compareTo | run
Using parameter / Specifying method arguments
A method can have zero or more arguments is called parameters.
Two ways:
1. Specifying multiple arguments
public void empdetails(String name, int age)
{
Ename = name;
Eage = age;
}
2. Specifying no arguments – if no arguments leave it empty
5
12.Access specifier’s:
Rules:
1. Public
Access Modifier
2. Protected Access Location
Public Protected Default Private
3. Default Same class Yes Yes Yes Yes
4. Private Sub class in same package Yes Yes Yes No
Other classes in same package Yes Yes Yes No
Subclass in other packages Yes Yes No No
Non-subclass in other package Yes No No No
Example:
package p1;
public class class1
{ import p1.*;
public int a; public class class3
int b;
{
private int c;
psvm…{
public void fun1() class1 obj = new class1();
{ s.o.p(a+b+c); obj.a; //allowed
} obj.b; //cant access
obj.c; //cant access
void fun1() obj.fun1(); //allowed
{ s.o.p(a+b+c);
obj.fun2(); //cant access
}
obj.fun3(); //cant access
private void fun1() }
{ s.o.p(a+b+c); }
}
}
public class class2
{
p.s.vm..{
class1 obj = new class1();
obj.a; //allowed
obj.b; //allowed
obj.c; //cant access
obj.fun1(); //allowed
obj.fun2(); //allowed
obj.fun3(); //cant access
}
}
6
7
13.Static members
- The memory of static fields will be stored in constant pole instead heap organization.
- Only one copy of memory created and shared
- The default value of static field is zero.
package package_name;
package abc;
import abc.*;
Example:
STEP 1: Create a folder p1 and follow the below code,
package p1;
public class testpackage
{
public void display()
{
System.out.println("Hi");
}
}
Save the file in p1 folder as “testpackage” and compile it.
STEP 2: Come out from the folder p1 and save the below code as “test”.
import p2.*;
public class test
{
public static void main(String args[])
{
testpackage tp = new testpackage();
tp.display();
}
}
Now compile and run the “test” file
9
15.JavaDoc comments
- A convenient and standard way to document java code. Creates HTML format
documentation.
Two types:
/**
*@author ABC
*The Student class contains marks
*/
class Student
{
//Student class code
}
Member level comments:
- It describes about data members, methods, and constructors.
Tags Description
@author Author name
@since To show from when used
@version Current version
@deprecated Deprecated should not be used
@param Describes name of the method
@return Return type
@throws Type of action
@exception Error
10
Assignment 01:
Part A:
11