Are You Feeling any difficulity while writing Core-Java Programs. Send those to me Here.

Classes

Classes

Unit Structure

5.1 Objective
5.2 class
5.2.1 Creating ―main‖ in a separate class
5.2.2 Methods with parameters
5.2.3 Methods with a Return Type
5.2.4 Method Overloading
5.2.5 Passing Objects as Parameters
5.2.6 Passing Values to methods and Constructor:
5.2.7 Passing Values to methods and Constructor:
5.2.8 ABSTRACT CLASSES
5.2.9 Extending the class:
5.3 Summary:
5.4 List of references
5.5 Bibliography
5.6 Model answers

5.1 Objective: In this lesson of Java Tutorial, you will learn... 



  • How to create class 
  • How to create method 
  • How to create constructor


5.2 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.
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:
class name1
{
//public variable declaration
void methodname()
{
//body of method…
//Anything
}
}
Now following example shows the use of method.
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.

5.2.1 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.
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();
}
}

5.2.3 use of dot operator

We can access the variables by using dot operator. Following program shows the use of dot operator.
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();
}}
output:
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.

5.2.4 Methods with parameters

Following program shows the method with passing parameter.
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();
}
}

5.2.5 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.
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);
}
}
Output:
The sum is25

5.2.6 Method Overloading

Method overloading means method name will be same but each method should be different parameter list.
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));
}
}
Output:
sum is 10
sum is 22
27

5.2.7 Passing Objects as Parameters

Objects can even be passed as parameters.
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();
}
}
Output:
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.

5.2.10 Passing Values to methods and Constructor:

These are two different ways of supplying values to methods.
Classified under these two titles -
1.Pass by Value
2.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.
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);
}
}
Output:
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---
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);
}
}

5.2.9 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:
abstract void studtest(int rollno, double testfees);
If a class includes abstract methods, the class itself must be declared abstract, as in:
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.
For example: In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. Some of these states and behaviors are the same for all graphic objects—for example: position, fill color, and moveTo. Others require different implementations—for example, resize or draw. All GraphicObjects must know how to draw or resize themselves; they just differ in how they do it. This is a perfect situation for an abstract superclass. You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object—for example, GraphicObject, as shown in the following figure.

How to implement above diagram concept with source code:

abstract class GraphicObject
{
int x, y;
...
void moveTo(int newX, int newY)
{
...
}
abstract void draw();
abstract void resize();
}
Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
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: 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(); } }

5.2.10 Extending the class:

Inheritance allows to subclass or child class to access all methods and variables of parent class.
Syntax:
class subclassname extends superclassname
{
Varables;
Methods;
…..
}
For example: calculate area and volume by using Inhertance.
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);
}
}
Output:
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).
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 -
1. public - outside of package access.
2. [no keyword] - same package access only.
3. protected - same package access. Access if class is a subclass of, even if in another package.
4. private - same class access only.

5.3 Summary:

In this unit, we learn the concept of class and how to create method and how to pass parameters by value and by reference and method overloading with example. In this unit, we also learn the concept of inheritance.

5.4 List of references

1. Java 2: The Complete Reference, Fifth Edition, Herbert Schildt, Tata McGraw Hill.
2. An Introduction to Object oriented Programming with JAVA, C THOMAS WU

5.5 Bibliography

http://www.michael-thomas.com/tech/java/javacert/JCP_Access.htm
http://en.wikipedia.org/wiki/Class_%28computer_science%29#Sealed_classes
http://www.javabeginner.com/learn-java/java-abstract-class-and-interface



No comments:

Post a Comment