Object Oriented Programming
a) Object
Oriented Programming decomposes a program into real world entities called as
objects and then creates data and function around these objects.
b) Object
Oriented Programming is based on 4 major pillars which are as follows:
i.
Abstraction:-
This deals with
identifying the key aspects of a system. This is normally required during
requirement analysis phase.Does not take unnecessary details.
ii.
Encapsulation:-
The key aspects
identified during abstraction are secured using encapsulation. This deals with
hiding the interface from implementation. Hence its purpose is security.e.g.Class
iii.
Inheritance:-
This deals with
incorporating the essential features from a base class in a derived class. It helps in reusability of methods
iv. Polymorphism:-
This helps when a same
message is sent by different objects to obtain different behaviour from a
method.
c)Object Oriented Programming also has 3 minor pillars which are as follows:
i.Strong type casting:-
This helps in avoiding mixing
of abstraction i.e. key aspects identified during requirement analysis.
ii.Persistence:-
This helps in a safe
state of an object.
iii.Concurrency:-
This deals with
simultaneously performing various tasks for better performance.
object and class
a) Object:-
An object is an real-world entity that
has a structure and behaviour
The characteristics of an object are as
follows:-
i.Identity
ii.State
iii.Behaviour
iv.Responsibility
b) Class:-
A class is a template.It can also be described as a collection
of data members and member functions.
Make a variable global in java. Static method access only static variable
and called using class name. Non-static method can access static method or
variable.
Make distinguish between local and instance variable. This holds the current object by default. This point to class not to local method. This cannot point to static there is no object.
Constructor
Constructor has same name as class name. Constructor is use to create object. Default constructor, copy constructor, parametrize constructor are types of constructors. Constructor calls implicitly.
Package
a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions. A Java source file can contain any (or all) of the following parts:
1) a single package statement (optional)
2) any number of import statements (optional)
3) a single public class declaration (required)
4) any number of classes private to the package (optional)
Type casting
If the two types are compatible, then Java will perform
the conversion automatically. For example, it is always possible to assign an int
value to a long variable. However, not all types are compatible, and
thus, not all type conversions are implicitly allowed. For instance, there is
no conversion defined from double to byte. When one type
of data is assigned to another type of variable, an automatic type conversion
will take place if the following two conditions are met:
1)
the two types are compatible.
2)
the destination type is larger than the source type.
Data Types in
java
Data types
|
Byte
|
Range
|
Int
|
32bit
|
-2,147,483,648 to 2,147,483,647
|
short
|
16bit
|
-32,768 to
32767
|
long
|
64bit
|
-9,223,372,036,854,775,808
to
9,223,372,036,854,775,808
|
Byte
|
8bit
|
-128 to127
|
Boolean
|
-
|
True & false
|
Float
|
32bit
|
1.4E-45 to 3.4028235E+38
|
char
|
16bit
|
0 to 65,535
|
Double
|
64bit
|
439E-324 to 1.797693134862315E+308
|
Programs
1)myDate prog
Create package date.com
/**Author Ajay Chakkar
* Email-id ajaychakkar@gmail.com
* Program for Displaying date
*/
package date.com;
public class MyDate {
int dd,mm,yy;//variable for date,month ,year
public MyDate(int d,int m,int y)//constructor
{
dd=d;
mm=m;
yy=y;
}
public String toString() //Returns a string that describes the object.
{
return dd+"-"+mm+"-"+yy; //Returns date,month,year
}
}
2)Student program
Create package stud.com
/**Author Ajay Chakkar
* Email-id ajaychakkar@gmail.com
* Program for Student information
*/
package stud.com;
import date.com.MyDate;
public class Student
{
int id; //id of student
String name; //student name
MyDate dob; //date of birth
public Student(int a,String n,MyDate d) //Constructor
{
id=a;
name=n;
dob=d;
}
public String toString()
{
return(id+" "+name+" "+dob); //returns student id,name,dob
}
}
3) Swap program
Create package main.com in that write program of swapping and test for main program
/**Author Ajay Chakkar
* Email-id ajaychakkar@gmail.com
* Program for Swapping Students array
*/
package main.com;
import stud.com.Student;//import Student class from stud.com
public class Swap {
public void swap(Student a[])
{
Student temp; //temp variable for swappimg student
temp=a[0]; //assigning 1st location student to temp
a[0]=a[1]; //assigning 2nd location student to 1st location
a[1]=temp; //assigning temp to 2nd location
}
}
/**Author Ajay Chakkar
* Email-id ajaychakkar@gmail.com
* Program for main method to access Student and MyDate package
*/
package main.com;
import stud.com.Student; //import Student class
import date.com.MyDate; //import MyDate class
public class Test {
public static void main(String args[])
{
MyDate d=new MyDate(1,1,1); //creating object of MyDate Class
MyDate d1=new MyDate(2,2,2); //creating object of MyDate Class
Student s1=new Student(1,"a",d); //creating object of Student Class
Student s2=new Student(2,"b",d1); //creating object of Student Class
Student arr[]={s1,s2}; //creating array of student
Swap s=new Swap(); //creating object of Swap class
s.swap(arr); //calling swap method using object
System.out.println(arr[0]); //print 1st location student
System.out.println(arr[1]); // print 2nd location student
}
}
No comments:
Post a Comment