Sunday, April 1, 2012

Class


Class
-------
A class is the template or blueprint from which objects are made.They define the structure of the objects.

The general form of a Class
---------------------------------
class class-name{
          type instance-variable1;
          type instance-variable2;
          ......
          .....
          type instance-variableN;

          type methodname1(parameter-list)
{
                   body of method
          }
          type methodname2(parameter-list)
          {
                   body of method
          }
          ......
          .........
          type methodnameN(parameter-list)
{
                   body of method
          }
          The data, or variables, defined within a class are called instance variables.  The code contained within methods.  Collectively, the  methods and variables defined within a class are called members of the class.
          ex:
          class Rectangle
          {
                   int length,width;
                   void getData(int x,int y)
                   {
                             length=x;
                             width=y;
                   }
                   int rectArea()//Declaration of another method
                   {
                             int area=length*width;
                             return(area);
                   }
          }

Relationship  between classes
-----------------------------------
·        Dependence(uses-a)
·        Aggregation(Has-a)
·        Inheritance(is-a)

Objects
-----------
Objects is an instance of class.

Three key characteristics of objects
------------------------------------------

·        Object’s behavior—What can you do with this object,or what methods can you apply to it?
·        Object’s state---How does the object react when you apply those methods?
·        Object’s identity---How is the object distinguished from others that may  have the same behavior and state?

Objects in java are created using the new operator.  The new operator creates an object of the specified class and returns a reference to that object.
ex:
          Rectangle rect1;//declare the object
          rect1=new Rectangle();//instantiate the object

          Both statement can be combined into one :
          Rectangle rect1=new Rectangle();

Accessing class Members :
--------------------------------
objectname.variablename=value;
objectname.methodname(parameter-list);
ex:
rect1.length=10;
rect1.width=10
rect1.getData(15,10);



ex:1
class Rectangle
{
          int length,width;
          void getData(int x,int y)
          {
                   length=x;
                   width=y;
          }
          int rectArea()
          {
                   int area=length*width;
                   return(area);
          }
}
class RectArea
{
          public static void main(String args[])
          {
                   int area1,area2;
                   Rectangle rect1=new Rectangle();
                   Rectangle rect2=new Rectangle();
                   rect1.length=15;
                   rect1.width=20;
                   area1=rect1.length*rect1.width;
                   rect2.getData(10,12);
                   area2=rect2.rectArea();
                   System.out.println("Area1="+area1);
                   System.out.println("Area2="+area2);
          }
}

//Returning a Value
-------------------------
class Box
{
          double width,height,depth;
          double volume(){
                   return width*height*depth;
          }
}
class BoxDemo1
{
          public static void main(String args[]){
                   Box b1=new Box();
                   Box b2=new Box();
                   double vol;

                   b1.width=10;
                   b1.height=20;
                   b1.depth=2;

                   b2.width=3;
                   b2.height=4;
                   b2.depth=5;

                   vol=b1.volume();
                   System.out.println("Volume is :"+vol);

                   vol=b2.volume();
                   System.out.println("Volume is :"+vol);
          }
}

//Parameterized method
---------------------------
class Box
{
          double width,height,depth;
          double volume()
{
                   return width*height*depth;
          }
          void setDim(double w,double h, double d){
                   width=w;
                   height=h;
                   depth=d;
          }
}
class BoxDemo2
{
          public static void main(String args[]){
                   Box b1=new Box();
                   Box b2=new Box();
                   double vol;

                   b1.setDim(10,20,15);
                   b2.setDim(3,6,9);
                   vol=b1.volume();
                    System.out.println("Volume is :"+vol);

                   vol=b2.volume();
                   System.out.println("Volume is :"+vol);
          }
}
------------------
import java.io.*;
class Emp
{
          String name;
          int s;
          void set(String n,int s1)
          {
                   name=n;
                   s=s1;
          }
          void show(){
                   System.out.println(name+"               "+s);
          }
}
class Display{
          public static void main(String args[])
          {
                   Emp e1=new Emp();
                   e1.set("Sita",5600);
                   e1.show();
          }
}

//return type function
------------------------
import java.io.*;
class Emp
{
          String name;
          double s,hra,da;
          void set(String n,double s1)
{
                   name=n;
                   s=s1;
          }
          double show()
{
                   hra=s*0.25;
                   da=s*0.71;
                   return (s+hra+da);
          }
}
class Display1{
          public static void main(String args[])throws IOException
          {
                   String name1;
                   int n;
                   Emp e=new Emp();
                   DataInputStream din=new DataInputStream(System.in);
                   System.out.println("Enter name and salary :");
                   name1=din.readLine();
                   n=Integer.parseInt(din.readLine());
                   e.set(name1,n);
                   System.out.println(e.name+"  "+e.show());
          }
}
Constructor
-------------
A constructor initializes an object immediately upon creation.  It has the same name as the class.  Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes.  Constructor have no return type, not even void.
ex:
class product
{
          String name;
          int price;
          product()
{
                   name="Mobile";
                   price=5400;
          }
          void show()
{
                   System.out.println(name+"     "+price);
          }
}
class cons
{
          public static void main(String arg[])
          {
                   product p1=new product();
                   p1.show();
          }
}
---------------------

ex:
class Box
{
          double width,height,depth;
          double volume()
{
                   return width*height*depth;
          }
          Box()
{
                   System.out.println("Constructing Box");
                   width=10;
                   height=20;
                   depth=5;
          }

}
class BoxDemo3
{
          public static void main(String args[])
{
                   Box b1=new Box();
                   Box b2=new Box();
                   double vol;


                   vol=b1.volume();
                   System.out.println("Volume is :"+vol);

                   vol=b2.volume();
                   System.out.println("Volume is :"+vol);
          }
}

Parameterized Constructor
--------------------------------
class Box
{
          double width,height,depth;
          double volume(){
                   return width*height*depth;
          }
          Box(double w,double h,double d){
                   width=w;
                   height=h;
                   depth=d;
          }

}
class BoxDemo4
{
          public static void main(String args[]){
                   Box b1=new Box(10,20,5);
                   Box b2=new Box(3,6,9);
                   double vol;


                   vol=b1.volume();
                   System.out.println("Volume is :"+vol);

                   vol=b2.volume();
                   System.out.println("Volume is :"+vol);
          }
}
------------------------------------------------
class RectangleArea
{
          int length,width;
          RectangleArea(int x,int y)
          {
                   length=x;
                   width=y;
          }
          int rectArea()
          {
                   return (length*width);
          }
}
class Rectangle
{
          public static void main(String args[])
          {
                   RectangleArea r1=new RectangleArea(15,10);//Calling constructor
                   int area1=r1.rectArea();
                   System.out.println("Area="+area1);
          }
}
-------------
Constructor Overloading
------------------------------
class nos
{
          int a,b,c;
          nos()
{
                   a=10;
                   b=20;
                   c=30;
          }
          nos(int x,int y,int z)
          {
                   a=x;
                   b=y;
                   c=z;
          }
          void show()
{
                   System.out.println("sum="+(a+b+c));
          }
}
class consover
{
          public static void main(String arg[])
          {
                   nos n1=new nos();
                   nos n2=new nos(1,2,3);
                   n1.show();
                   n2.show();

          }
}



The this keyword
--------------------
this can be used inside any method to refer to the current object.
class Box
{
          double width,height,depth;
          double volume()
{
                   return width*height*depth;
          }
          Box(double width,double height,double depth)
{
                   this.width=width;
                   this.height=height;
                   this.depth=depth;
          }

}
class BoxDemo5
{
          public static void main(String args[])
{
                   Box b1=new Box(10,20,5);
                   Box b2=new Box(3,6,9);
                   double vol;


                   vol=b1.volume();
                   System.out.println("Volume is :"+vol);

                   vol=b2.volume();
                   System.out.println("Volume is :"+vol);
          }
}

The finalize() Method
--------------------------

Used to deallocate the memory allotted by constructor. It is automatically executed from the garbage collector.
syntax:
          protected void finalize()
          {
}
         

Methods Overloading
--------------------------
It is possible to create methods that have the same name, but different parameter lists and different definitions.  This is called method overloading.  Method overloading is used when objects are required to perform similar tasks but using different input parameters.  When we call a method in an object, java matches up the method name first and then the number and type of parameters to decide which one of the definitions to execute.  This process is known as polymorphism.

//Demonstrate method overloading.
------------------------------------------
class Overload
{
          void test()
{
                   System.out.println("No parameters");
          }
          void test(int a)
{
                   System.out.println("a="+a);
          }
          void test(int a,int b)
          {
                   System.out.println("a and b :"+a+"  "+b);
          }
          double test(double a)
{
                   System.out.println("double a="+a);
                   return a*a;
          }
}
class OverloadDemo
{
          public static void main(String args[])
          {
          Overload ob=new Overload();
          double result;
          ob.test();
          ob.test(10);
          ob.test(10,20);
          result=ob.test(123.2);
          System.out.println("Result of ob.test(123.2):"+result);
}
}
---------
class shape
{
          int length,width;
          float radius;
          int area(int l,int b)
          {
                   length=l;
                   width=b;
                   return (length*width);
          }
          double area(float r)
          {
                   radius=r;
                   return (3.14*radius*radius);
          }
}
class Overload
{
          public static void main(String args[])
          {
                   shape s=new shape();
                   System.out.println("Area of rectangle="+s.area(10,20));
                   System.out.println("Area of circle="+s.area(2.f));
          }
}







Static methods
------------------
Static variables are used when we want to have a variable common to all instances of a class. Java creates only one copy for a static variable which can be used even if the class is never actually instantiated.
static variable and static methods can be called without using the objects.
ex:
          float x=Math.sqrt(25);

Static have several restrictions:
-------------------------------------
1) They can only call other static methods.
2) They must only access static data.
3) They cannot refer to this or super in any way.
class Mathoperation
{
          static float mul(float x,float y)
          {
                   return x*y;
          }
          static float div(float x,float y)
          {
                   return(x/y);
          }
}
class MathApplication
{
          public static void main(String args[])
          {
                   float a=Mathoperation.mul(4,5);
                   float b=Mathoperation.div(a,4);
                   System.out.println(b);
          }
}

---------------
class StaticDemo
{
          static int a=42;
          static int b=99;
          static void callme()
{
                   System.out.println("a="+a);
          }
}
class UseStatic
{
          public static void main(String args[])
{
                   StaticDemo.callme();
                   System.out.println("b="+StaticDemo.b);
          }
}


Nesting of Methods
-----------------------
A method can be called  by using only its name by another method of the same class.  This is known as nesting of methods.
class Nesting
{
          int m,n;
          Nesting(int x,int y)
          {
                   m=x;
                   n=y;
          }
          int largest()
          {
                   if(m>=n)
                             return m;
                   else
                             return n;
          }
          void display()
{
                   int large=largest();
                   System.out.println("Largest value="+large);
          }
}
class NestingTest
{
          public static void main(String args[])
          {
                   Nesting nest=new Nesting(10,45);
                   nest.display();
          }
}


No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin