Professional Documents
Culture Documents
double volume(){
double v=width*height*depth;
return v;
}
void setDim ( double w , double h , double d){
width=w;
height=h;
depth=d;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
mybox1.SetDim(10,20,15);
mybox2.SetDim(3,6,9);
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Constructors
• A constructor initializes an object immediately
upon creation.
• It has the same name as the class
• Constructor is automatically called immediately
after the object is created, before the new operator
completes.
• Constructors have no return type , not even void.
class Box{
double width;
double height;
double depth;
Box()
{
width=10;
height=10;
depth=10;
}
double volume(){
double v=width*height*depth;
return v;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box();
Box mybox2=new Box();
double vol;
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Parameterized Constructors
• In the previous example, all boxes have the same
dimension.
• What is needed is a way to construct a Box
objects of various dimensions.
• Solution: Add parameters to the constructors.
class Box{
double width;
double height;
double depth;
Box( double w , double h , double d)
{
width=w;
height=h;
depth=d;
}
double volume(){
double v=width*height*depth;
return v;
}
}
Class BoxDemo {
public static void main(String args []){
Box mybox1=new Box(10,20,30);
Box mybox2=new Box(20,12,10);
double vol;
vol=mybox1.volume();
System.out.println (“Volume of Box_1:”+vol);
vol=mybox2.volume();
System.out.println (“Volume of Box_2 :”+vol);
}
}
Method overloading
• It is possible to define two or more methods
within the same class that share the same
name, as long as their parameter
declarations are different.
• Such methods are said to be overloaded
and the process is called method
overloading.
• Method overloading is one of the ways that
java implements polymorphism.
class OverloadDemo {
void test()
{
System.out.println(“No Parameters”);
}
void test( int a )
{
System.out.println(“a :”+ a);
}
}
}
Access Control
• Encapsulation provides an important attribute:
access control.
• Through encapsulation, we can control what parts
of a program can access the members of a class.
• How a member can be accessed is determined by
the access specifier.
• Java’s access specifiers are public, private
,protected.
• Protected is applied only when inheritance is
involved.
• Default access specifier is public.
• When a member of a class is specified as public,
then the member can be accessed by any other
code.
• When a member of a class is specified as private,
then that member can be accessed by other
members of its class.
• Example:
Class test{
int a;
public int b;
private int c;
void set_c (int i)
{
c = i;
}
int getc()
{
return c;
}
class Access{
public static void main(String args[])
{
test ob=new test();
ob.a=10;//this is ok,no error
ob.b=20;// this is ok,no error
//ob.c=100, Error! .accessing private
ob.set_c(100);
int k=ob.getc();
System.out.println(k);
}
this keyword
• In java, this is a reference variable that refers to
the current object.
• Eg:
• Example:
class A{
int i,j;
void show( )
{
System.out.println (“ i and j: ”+ i “ ” + j);
}
}
class B extends A{
int k;
void sum()
{
System.out.println (“ i +j+ k: “+( i+ j+ k));
}
}
class simple{
Public static void main(String args [])
{
A ob_a = new A( );
B ob_b = new B( );
ob_a.i=10;
ob_a.j=12;
System.out.println(“Contents base class’s object:”)
ob_a.show( );
//subclass has access to all public members of its //superclass
ob_b.i=7;
ob_b.j=8;
System.out.println(“Contents of child class’s object:”)
ob_b.show();
ob_b.k=5;
ob_b.sum();
}
}
Output:
Contents base class’s object:
i and j: 10 12
Contents of child class’s object:
i and j: 7 8
i+ j+ k = 25
-----------------------------------------------------------------
Types of inheritance
On the basis of class, there can be three types of
inheritance in java: single, multilevel and
hierarchical.
In java programming, multiple and hybrid inheritance
is supported through interface only.
Types
Multiple Inheritance
• Multiple inheritance(a class inherits multiple classes,
ie, more than one parent classes ) is not supported in
java through class.