Unit - III Object Oriented Programming Concepts
Unit - III Object Oriented Programming Concepts
default constructor
• Class can have any no. of objects of Rectangle.
• Each object has its own instance variables copy so
change to variable of one object have no effect on
variable of another.
Accessing class members
• Syntax:
Objectname . variablename;
Objectname . method name ( parameter list );
Demo( int a)
{ ------ }
}
Example
class A
{
A( ) // definition of default constructor
{
System . out . println( “ Constructor demo “ );
}
Output:
Constructor Demo
Value of x=5
Copy constructor
class A
{
int x;
A(int a)
{
x=a;
}
A(A obj)
{
System.out.println("copy cosnst");
System.out.println("x="+obj.x);
}}
class Copy1
{
public static void main(String args[])
{
A a2=new A(10);
A a3=new A(a2);
//A obj=a2;
}
}
Method overloading
• In java ,it is possible to create methods that have
same name but different definitions . this is called
method overloading.
}
class B extends A //invalid ,it gives Error
{
//body
}
Any ?