2.classes and Objects-1
2.classes and Objects-1
}
class demo static void disp()
{ {
int a,b; System.out.println(“the value of c” +c);
static int c; }
void get()
public static void main(String args[])
{
{
a=10;
demo t= new demo();
b=20;
c++; t.get();
} t.show();
void show() demo.disp();
{ }
System.out.println(“the value of a & b” +a+“”+b); }
}
Using Two Different Classes
class demo static void disp()
{ {
int fact; {
{ {
} t1. getinfo(5);
while (i<=fact) }
{ n=n*i; }
i++;
} return(n);
}
Constructor :
A constructors are the special members of the class which have same
name as that of class name.
They are used to do initialization of the object at the time of creation
so it is called as automatic initialization of the object.
Initializing the object means to assign some initial value to data
members of the object.
Properties of constructor
1. The constructor function of a class have same name as the class
name.
2. The constructor does not have any return type therefor it does not
return any value.
Types of constructor
1 ) Default constructor: The constructor that does not accept any
argument is known as default constructor.
2) Parameterized constructor :It is the constructor with one or more
argument passed in it.
3) Overloading constructor : If a class can have multiple constructor
with different types of argument such a constructor is called
overloading of constructor.
4) Copy Constructor :If parameters to the constructor can be object
reference of the same class such constructor is called copy
constructor.
Demo Program w= t.w;
class box h=t.h;
{ }
int l,w,h ; void show ()
box() //default constructor {
{ System.out.println("The dimension "+l+" x”+w +"x" +h);
l=w=h=-1; }
} }
box (int d) //Parameterized Constructor class test3
{ {
l=w=h=d; public static void main ( String args[])
} {
box ( int len,int wid, int hgt) box b1 = new box();
{ box b2 = new box(5);
l= len; box b3 = new box (10,20,30);
w=wid; box b4 = new box (b3);
h=hgt; b1.show();
} b2.show();
box (box t) // Copy Constructor b3.show();
{ b4.show();
I=t.l; }
}
Write a program which performs addition and subtraction of 2 complex no. initialize
the value through parameterised constructor
}
class complex
void show ()
{
{
double r,i;
system.out.println (r+“+i”+i);
complex()
}}
{ }
class tes14
complex (double real, double img) {
{ public static void main (String args[])
r=real; {
i = img; complex t=new complex (4.5f, 2.2f);
} complex t1=new complex( 2.2f,1.3f);
2) float Float
3) char Character
4) double Double
5) boolean Boolean
6) long Long
final Keyword :
There are 3 purpose of final Keyword
First to declare constant.
I) We can declare a variable in any scope to be final, The value of final variable cannot be
changed throughout the program once it has been initialize.
eg. final int i=10
Here the value of ì remains constant throughout the program.
Note: If we declare final variable & initialized that variable later this is known as blank final.
e.g. final int i;
-------------
------------- blank final
-------------
i=10;
2) Prevent Inheritance(final class):
To prevent the class being further sub class from the base class we use final keyword before
the class declaration i.e we prevent inheritance.
3) To prevent method Overriding (final method) :
Overriding means base class & derived class contains the same method name with Same
parameters then the method of derived class hides the method of base class. To prevent
such a method overriding we use final key word before method declaration.
{ {
String nm; }
{ class test36
rno=r; {
m1=m11; {
} int m1=Integer.parseInt(args[2]);
{ int m3 = Integer.parseInt(args[4]);
} }
} }