Beruflich Dokumente
Kultur Dokumente
class Rectangle {
int length;
int breadth;
}
class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
Rectangle r2 = r1;
r1.length = 10;
r2.length = 20;
System.out.println("Value of R1's Length : " + r1.length);
System.out.println("Value of R2's Length : " + r2.length);
}
}
class Rectangle {
int length;
int breadth;
r1.length = 10;
System.out.println("Before Function Length : " + r1.length);
r1.setLength(20);
1
System.out.println("After Function Length : " + r1.length);
}
}
class Rectangle {
int length;
int breadth;
void setLength(int len)
{
length = len;
}
int getLength()
{
return length;
}
}
class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
r1.setLength(20);
int len = r1.getLength();
System.out.println("Length of Rectangle : " + len);
}
}
2
class RectangleDemo {
public static void main(String args[]) {
r1.setDiamentions(20,10);
}
}
Constructors
class Rectangle {
int length;
int breadth;
Rectangle()
{
length = 20;
breadth = 10;
}
class RectangleDemo {
public static void main(String args[]) {
}
}
class Rectangle {
int length;
int breadth;
Rectangle()
{
length = 20;
breadth = 10;
3
}
void setDiamentions()
{
length = 40;
breadth = 20;
}
}
class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
System.out.println("Length of Rectangle : " + r1.length);
System.out.println("Breadth of Rectangle : " + r1.breadth);
r1.setDiamentions();
System.out.println("Length of Rectangle : " + r1.length);
System.out.println("Breadth of Rectangle : " + r1.breadth);
}
}
Parameterized Constructors :
1. Constructor Can Take Value , Value is Called as – “Argument“.
2. Argument can be of any type i.e Integer,Character,Array or any Object.
3. Constructor can take any number of Argument.
class Rectangle {
int length;
int breadth;
Rectangle(int len,int bre)
{
length = len;
breadth = bre;
}
}
class RectangleDemo {
public static void main(String args[]) {
}
}
4
overloading
class poly{
void add(int a, int b)
{
System.out.println("sum of two" +(a+b));
}
void add(int a, int b, int c)
{
System.out.println("sum of three" +(a+b+c));
}
}
public class Overload {
public static void main(String args[]) {
poly p=new poly();
p.add(10,55);
p.add(10,6,9);
}
}
Over riding
class one{
void calculate(int x)
{
System.out.println("square of two" +(x*x));
}
}
}
}
5
STACK IMPLEMENTATION
In general stack class is used to support object oriented principle encapsulation. Satck is having
operations like push() and pop() to insert and delete elements respectively.
// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
6
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++)
mystack1.push(i);
for(int i=10; i<20; i++)
mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
finalyze()
finalyze() method is called before Garbage collector reclaim the object
its last chance for any object to perform cleanup activity that is releasing any system resources
held,closing connection if open etc
finalize() is defined in java.lang.Object class
its the responsibility of developer to call superclass finalize method when we override finalyze()
there is no guarantee that finalize() is called because finalyze()
is called just before the garbage collection process happens and we are not sure when garbage
collection process happens
When we pass a primitive type to a method, it is passed by value. But when we pass an object
to a method, the situation changes dramatically, because objects are passed by what is
effectively call-by-reference. Java does this interesting thing that’s sort of a hybrid between
pass-by-value and pass-by-reference
class Rectangle {
int length;
int width;
7
Rectangle(int l, int b) {
length = l;
width = b;
}
static:
When a member is declared static, it can be accessed before any objects of its class are created,
and without reference to any object.
Methods declared as static have several restrictions:
8
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way
class UseStatic {
static int a = 3;
static int b;
static void meth(int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
Output:
Static block initialized.
x = 42 a=3 b = 12
As soon as the UseStatic class is loaded, all of the static statements are run.
· First, a is set to 3, then the static block executes, which prints a message and then
initializes b to a*4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x.
· The three println( ) statements refer to the two static variables a and b, as well as to the
local variable x
to define a class within another class; such classes are known as nested classes. The scope of a
nested class is bounded by the scope of its enclosing class.
10