Classes And Its Operations In Java Language

CLASS :

Definition : A class is a collection of objects of similar type. Once a class is defined, any number of objects can be produced which belong to that class.

Syntax
Class Declaration
class classname
{
… ClassBody
…}

Objects are instances of the Class. Classes and Objects are very much related to each other. Without objects you can't use a class. A general class declaration:

Syntax
class name1
{
//public variable declaration
void methodname()
{
//body of method…
//Anything
}
}

Now following example shows the use of method.

CODE/PROGRAM/EXAMPLE
class Demo
{
private int x,y,z;
public void input()
{
x=10;
y=15;
}
public void sum()
{
z=x+y;
}
public void print_data()
{
System.out.println(“Answer is =” +z);
}
public static void main(String args[])
{
Demo object=new Demo();
object.input();
object.sum();
object.print_data();
}
}

In program, Demo object=new Demo(); object.input(); object.sum(); object.print_data(); In the first line we created an object.

The three methods are called by using the dot operator. When we call a method the code inside its block is executed. The dot operator is used to call methods or access them.

Creating “main” in a separate class :

We can create the main method in a separate class, but during compilation you need to make sure that you compile the class with the “main” method.

CODE/PROGRAM/EXAMPLE
class Demo
{
private int x,y,z;
public void input() {
x=10;
y=15;
}
public void sum()
{
z=x+y;
}
public void print_data()
{
System.out.println(“Answer is =” +z);
}
}
class SumDemo
{
public static void main(String args[])
{
Demo object=new Demo();
object.input();
object.sum();
object.print_data();
}
}

Use of dot operator :

We can access the variables by using dot operator.

Following program shows the use of dot operator.

CODE/PROGRAM/EXAMPLE
class DotDemo
{
int x,y,z;
public void sum(){
z=x+y;
}
public void show(){
System.out.println("The Answer is "+z);
}
}
class Demo1
{
public static void main(String args[]){
DotDemo object=new DotDemo();
DotDemo object2=new DotDemo();
object.x=10;
object.y=15;
object2.x=5;
object2.y=10;
object.sum();
object.show();
object2.sum();
object2.show();
}
}

// O/P : C:\cc>javac Demo1.java
C:\cc>java Demo1
The Answer is 25
The Answer is 15

Instance Variable :

All variables are also known as instance variable. This is because of the fact that each instance or object has its own copy of values for the variables. Hence other use of the “dot” operator is to initialize the value of variable for that instance.

Methods with parameters :

Following program shows the method with passing parameter.

CODE/PROGRAM/EXAMPLE
class prg
{
int n,n2,sum;
public void take(int x,int y)
{
n=x;
n2=y;
}
public void sum()
{
sum=n+n2;
}
public void print()
{
System.out.println("The Sum is"+sum);
}
}
class prg1
{
public static void main(String args[])
{
prg obj=new prg();
obj.take(10,15);
obj.sum();
obj.print();
}
}

Methods with a Return Type :

When method return some value that is the type of that method.

For Example: some methods are with parameter but that method did not return any value that means type of method is void. And if method return integer value then the type of method is an integer.

Following program shows the method with their return type.

CODE/PROGRAM/EXAMPLE
class Demo1
{
int n,n2;
public void take( int x,int y)
{
n=x;
n=y;
}
public int process()
{
return (n+n2);
}
}
class prg
{
public static void main(String args[])
{
int sum;
Demo1 obj=new Demo1();
obj.take(15,25);
sum=obj.process();
System.out.println("The sum is"+sum);
}
}

// O/P : The sum is25

Method Overloading :

Method overloading means method name will be same but each method should be different parameter list.

CODE/PROGRAM/EXAMPLE
class prg1
{
int x=5,y=5,z=0;
public void sum()
{
z=x+y;
System.out.println("Sum is "+z);
}
public void sum(int a,int b)
{
x=a;
y=b;
z=x+y;
System.out.println("Sum is "+z);
}
public int sum(int a)
{
x=a;
z=x+y;
return z;
}
}
class Demo
{
public static void main(String args[])
{
prg1 obj=new prg1();
obj.sum();
obj.sum(10,12);
System.out.println(+obj.sum(15));
}
}

// O/P : sum is 10
sum is 22
27

Passing Objects as Parameters :

Objects can even be passed as parameters.

CODE/PROGRAM/EXAMPLE
class para123
{
int n,n2,sum,mul;
public void take(int x,int y)
{
n=x;
n2=y;
}
public void sum()
{
sum=n+n2;
System.out.println("The Sum is"+sum);
}
public void take2(para123 obj)
{
n=obj.n;
n2=obj.n2;
}
public void multi()
{
mul=n*n2;
System.out.println("Product is"+mul);
}
}
class DemoPara
{
public static void main(String args[])
{
para123 ob=new para123();
ob.take(3,7);
ob.sum();
ob.take2(ob);
ob.multi();
}
}

// O/P : C:\cc>javac DemoPara.java
C:\cc>java DemoPara
The Sum is10
Product is21

We have defined a method “take2” that declares an object named obj as parameter. We have passed ob to our method. The method “take2” automatically gets 3,7 as values for n and n2.

Passing Values to methods and Constructor :

These are two different ways of supplying values to methods.

Classified under these two titles -

  • Pass by Value
  • Pass by Address or Reference

Pass by Value-When we pass a data type like int, float or any other datatype to a method or some constant values like(15,10). They are all passed by value. A copy of variable’s value is passed to the receiving method and hence any changes made to the values do not affect the actual variables.

CODE/PROGRAM/EXAMPLE
class Demopbv
{
int n,n2;
public void get(int x,int y)
{
x=x*x; //Changing the values of passed arguments
y=y*y; //Changing the values of passed arguments
}
}
class Demo345
{
public static void main(String args[])
{
int a,b;
a=1;
b=2;
System.out.println("Initial Values of a & b "+a+" "+b);
Demopbv obj=new Demopbv();
obj.get(a,b);
System.out.println("Final Values "+a+" "+b);
}
}

// O/P : C:\cc>javac Demo345.java
C:\cc>java Demo345
Initial Values of a & b 1 2
Final Values 1 2

Pass by Reference :

Objects are always passed by reference. When we pass a value by reference, the reference or the memory address of the variables is passed. Thus any changes made to the argument causes a change in the values which we pass.

Demonstrating Pass by Reference---

CODE/PROGRAM/EXAMPLE
class pass_by_ref
{
int n,n2;
public void get(int a,int b)
{
n=a;
n2=b;
}
public void doubleit(pass_by_ref temp)
{
temp.n=temp.n*2;
temp.n2=temp.n2*2;
}
}
class apply7
{
public static void main(String args[])
{
int x=5,y=10;
pass_by_ref obj=new pass_by_ref();
obj.get(x,y); //Pass by Value
System.out.println("Initial Values are-- ");
System.out.println(+obj.n);
System.out.println(+obj.n2);
obj.doubleit(obj); //Pass by Reference
System.out.println("Final Values are");
System.out.println(+obj.n);
System.out.println(+obj.n2);
}
}

Abstract Classes :

Definition : An abstract class is a class that is declared as abstract. It may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclass.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this :

Syntax
abstract void studtest(int rollno, double testfees);

If a class includes abstract methods, the class itself must be declared abstract, as in :

Syntax
public abstract class GraphicObject
{
// declare fields
// declare non-abstract methods
abstract void draw();
}

When an abstract class is subclass, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.

Abstract classes are those which can be used for creation of objects. However their methods and constructors can be used by the child or extended class. The need for abstract classes is that you can generalize the super class from which child classes can share its methods. The subclass of an abstract class which can create an object is called as "concrete class".

For example :

CODE/PROGRAM/EXAMPLE
Abstract class A
{
abstract void method1();
void method2()
{
System.out.println("this is real method");
}}
class B extends A
{
void method1()
{
System.out.println("B is execution of method1");
}}
class demo
{
public static void main(String arg[])
{
B b=new B();
b.method1();
b.method2();
}
}

Extending the class :

Inheritance allows to subclass or child class to access all methods and variables of parent class.

Syntax
//Syntax:
class subclassname extends superclassname
{
Varables;
Methods;
…..
}

For example : calculate area and volume by using Inhertance.

CODE/PROGRAM/EXAMPLE
class data
{
int l;
int b;
data(int c, int d)
{
l=c;
b=d;
}
int area( )
{
return(l*b);
}
}
class data2 extends data
{
int h;
data2(int c,int d, int a)
{
super(c,d);
h=a;
}
int volume()
{
return(l*b*h);
}
}
class dataDemo
{
public static void main(String args[])
{
data2 d1=new data2(10,20,30);
int area1=d1.area(); //superclass method
int volume1=d1.volume( );// subclass method
System.out.println("Area="+area1);
System.out.println("Volume="+volume1);
}
}
O/P : C:\cc>javac dataDemo.java
C:\cc>java dataDemo
Area=200
Volume=6000

"Is A" - is a subclass of a superclass (ex: extends) "Has A" - has a reference to (ex: variable, ref to object).

o Access Control –

Away to limit the access others have to your code.

  • Same package - can access each others’ variables and methods, except for private members.
  • Outside package - can access public classes. Next, can

access members that are public. Also, can access protected members if the class is a subclass of that class.

Same package - use package keyword in first line of source file, or no package keyword and in same directory.

Keywords -

  • public - outside of package access.
  • [no keyword] - same package access only.
  • protected - same package access. Access if class is a subclass of, even if in another package.
  • private - same class access only.
#classes_in_java #Use_of_dot_operator_in_java #Instance_Variable_in_java #Methods_with_parameters_in_java #Methods_with_a_Return_Type_in_java #_Method_Overloading_in_java #Passing_Objects_as_Parameters_in_java #Passing_Values_to_methods_and_Constructor_in_java #Pass_by_Reference_in_java #Abstract_Classes__in_java

(New page will open, for Comment)

Not yet commented...