Introduction to Class and Constructors and Function Overloading in Core Java

In this tutorial basic introduction to Class and its Elements such as Constructors and  How to perform Function Overloading  in Core Java  is shown.

Code:

Demo.java

class Number
{
int a,b;
static int count=0;
public final static String str=”Hello”;

Number()
{
a=b=0;
counter();
}

Number(int x)
{
a=b=x;
counter();
}

Number(int a,int b)
{
this.a=a;
this.b=b;
counter();
}

public static void counter()
{
count++;
}

public static int getcount()
{
return count;
}

public void insert(int x)
{
a=b=x;
}

public void insert(int a,int b)
{
this.a=a;
this.b=b;
}

public void insert(Number n)
{
this.a=n.a;
this.b=n.b;
}

public void display()
{
System.out.println(“a: “+a);
System.out.println(“b: “+b);
}

protected void finalize()
{
System.out.println(“Finalize method”);
}

}

public class Demo
{
public static void main(String args[])
{
Number n1=new Number();
System.out.println(“No of Objects: “+Number.getcount());

Number n2=new Number(3);
System.out.println(“No of Objects: “+Number.getcount());

Number n3=new Number(4,5);
System.out.println(“No of Objects: “+Number.getcount());

System.out.println(“n1 data”);
n1.insert(5);
n1.display();

System.out.println(“n2 data”);
n2.insert(10,15);
n2.display();

System.out.println(“n3 data”);
n3.insert(12,20);
n3.display();

System.out.println(“n3 data after change”);
n3.insert(n2);
n3.display();

System.out.println(“String: “+ Number.str);

}
}

Add a Comment

Your email address will not be published. Required fields are marked *