JAVA
Class
Prepared by
Miss. Arati A. Gadgil
2
3
Class
class is the template for an object and that a class is a way to
encapsulate both data and the functions that operate on that
data.
We define a class by using the class keyword along with the
class name,
class MyClass
{
}
4
Object
A class consists of a collection of fields, or variables, very
much like the named fields of a struct ,all the operations
(called methods) that can be performed on those fields.
A class describes objects and operations defined on those
objects.
To create an object from a class, we type the class's name
followed by the name of the object. For example, the line
below creates an object from the MyClass class,
MyClass myObject = new MyClass();
5
Declaring Fields for a Class
We declare fields for class in much the same way as any
variable in a program, by typing the data type of the field
followed by the name of the field, like this:
int name;
Class fields that is data field is by default accessible only by
methods in the same package.
we can change the rules of this access by using the public,
protected, and private keywords.
6
Public
A public data field can be accessed by any part of a program,
inside or outside of the class in which it's defined.
Protected
A protected data field can only be accessed from within the
class or from within a derivedclass (a subclass).
Private
A private data field cannot even be accessed by a derived
class.
7
Defining Methods
Method describe behavior of an object. A method is a
collection of statements that are group together to perform an
operation.
Syntax :
return-type methodName(parameter-list)
{
//body of method
}
8
void show()
{
System.out.println(“Welcome…”);
}
Here return type is void ,show is name.
int getNo()
{
no=10;
return no;
}
Here return type is int ,getNo is name.
9
Parameter is variable defined by a method that receives
value when the method is called. Parameter are always
local to the method they dont have scope outside the
method. While argument is a value that is passed to a
method when it is called.
10
There are two ways to pass an argument to a method
call-by-value : In this approach copy of an argument value is
pass to a method. Changes made to the argument value inside
the method will have no effect on the arguments.
call-by-reference : In this reference of an argument is pass to
a method. Any changes made inside the method will affect the
agrument value.
11
call-by-value
public class A
{
int a;
public void callByValue(int x)
{
a=x;
System.out.println(a);
}
public static void main(String[] args)
{
A obj = new A(10);
obj.callByValue(x); //function call
}
}
12
call-by-reference
public class A
{ int x=10; int y=20;
public void callByReference(A t)
{
t.x=100; t.y=50;
}
public static void main(String[] args)
{
A s = new A();
System.out.println("Before "+s.x+" "+s.y);
s.callByReference(s);
System.out.println("After "+s.x+" "+s.y);
}
}
13
Controlling Access to Members of a Class
Access level modifiers determine whether other classes can
use a particular field or invoke a particular method. There are
two levels of access control:
1.At the top level—public, or package-private (no explicit
modifier).
2. At the member level—public, private, protected,
or package-private (no explicit modifier).
14
A class may be declared with the modifier public, in which
case that class is visible to all classes everywhere. If a class
has no modifier, it is visible only within its own package.
The following table shows the access to members permitted by
each modifier.
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N
15
Constructor
Constructor is special type of method, enables an object to
initialize itself when it's created. A constructor is a public
method with the same name as the class.
class A
{
int a;
public A(int value)
{
a = value;
}
}
16
Class's constructor starts with the public keyword. This is
important because we want to be able to create an object from
the class anywhere in program, and when we create an
object, you're actually calling its constructor.
After the public keyword comes the name of the
constructor followed by the constructor's arguments in
parentheses. When you create an object of the
class, you must also provide the required arguments.
17
There are two types of constructor
Default Constructor
Parameterized constructor
Each time a new object is created at least one constructor will
be invoked.
A obj1=new A()//Default constructor invoked
A obj2=new A() //Parameterized constructor invoked
18
Constructor Overloading
Overloaded constructors are differentiated on the basis of their
type of parameters or number of parameters.
class A
{
int a;
public A()
{
a=0;
}
public A(int value)
{
a = value;
}
}
19
Using the this Keyword
Within an instance method or a constructor, this is a reference
to the current object — the object whose method or constructor
is being called.
we can refer to any member of the current object from within
an instance method or a constructor by using this.
20
public class Point
{ public int x = 0;
public int y = 0;
public Point(int x, int y)
{ this.x = x;
this.y = y; }
}
Each argument to the constructor shadows one of the object's
fields — inside the constructor x is a local copy of the
constructor's first argument. To refer to the Point field x, the
constructor must use this.x.
21
Static
Same data is used for all the instances (objects) of some Class.
Class A
{
public int y = 0;
public static int x= 1;
};
A a = new A();
A b = new A();
System.out.println(b.x);
a.x = 5;
System.out.println(b.x);
A.x= 10;
System.out.println(b.x);
Assignment performed
on the first access to the
Class.
Only one instance of ‘x’
exists in memory
Output:
1
5
10
a b
y y
A.x_
0 0
1
22
Static member function
Static member function can access only static members
Static member function can be called without an instance.
Class TeaPot
{ private static int numOfTP = 0;
private Color myColor_;
public TeaPot(Color c)
{ myColor_ = c;
numOfTP++; }
public static int howManyTeaPots()
{ return numOfTP; }
// error : public static Color getColor()
{ return myColor_; }
}
23
TeaPot tp1 = new TeaPot(Color.RED);
TeaPot tp2 = new TeaPot(Color.GREEN);
System.out.println(“We have “ +
TeaPot.howManyTeaPots()+ “Tea Pots”);
Static Block
Code that is executed in the first reference to the class.
Several static blocks can exist in the same class
( Execution order is by the appearance order in the class
definition ).
Only static members can be accessed.
24
Class design hints
1.Always keep data private.
2.Always initialize data.
3.Don’t use too many basic type in class.
4.Not all fields indivisual field accessor and mutators.
5.Brak up classes that have too many responsibilities.
6.Make the name of your classes and method that reflect their
responsibilty.
Thank You
25

Java class

  • 1.
  • 2.
  • 3.
    3 Class class is thetemplate for an object and that a class is a way to encapsulate both data and the functions that operate on that data. We define a class by using the class keyword along with the class name, class MyClass { }
  • 4.
    4 Object A class consistsof a collection of fields, or variables, very much like the named fields of a struct ,all the operations (called methods) that can be performed on those fields. A class describes objects and operations defined on those objects. To create an object from a class, we type the class's name followed by the name of the object. For example, the line below creates an object from the MyClass class, MyClass myObject = new MyClass();
  • 5.
    5 Declaring Fields fora Class We declare fields for class in much the same way as any variable in a program, by typing the data type of the field followed by the name of the field, like this: int name; Class fields that is data field is by default accessible only by methods in the same package. we can change the rules of this access by using the public, protected, and private keywords.
  • 6.
    6 Public A public datafield can be accessed by any part of a program, inside or outside of the class in which it's defined. Protected A protected data field can only be accessed from within the class or from within a derivedclass (a subclass). Private A private data field cannot even be accessed by a derived class.
  • 7.
    7 Defining Methods Method describebehavior of an object. A method is a collection of statements that are group together to perform an operation. Syntax : return-type methodName(parameter-list) { //body of method }
  • 8.
    8 void show() { System.out.println(“Welcome…”); } Here returntype is void ,show is name. int getNo() { no=10; return no; } Here return type is int ,getNo is name.
  • 9.
    9 Parameter is variabledefined by a method that receives value when the method is called. Parameter are always local to the method they dont have scope outside the method. While argument is a value that is passed to a method when it is called.
  • 10.
    10 There are twoways to pass an argument to a method call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments. call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the agrument value.
  • 11.
    11 call-by-value public class A { inta; public void callByValue(int x) { a=x; System.out.println(a); } public static void main(String[] args) { A obj = new A(10); obj.callByValue(x); //function call } }
  • 12.
    12 call-by-reference public class A {int x=10; int y=20; public void callByReference(A t) { t.x=100; t.y=50; } public static void main(String[] args) { A s = new A(); System.out.println("Before "+s.x+" "+s.y); s.callByReference(s); System.out.println("After "+s.x+" "+s.y); } }
  • 13.
    13 Controlling Access toMembers of a Class Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control: 1.At the top level—public, or package-private (no explicit modifier). 2. At the member level—public, private, protected, or package-private (no explicit modifier).
  • 14.
    14 A class maybe declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier, it is visible only within its own package. The following table shows the access to members permitted by each modifier. Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N
  • 15.
    15 Constructor Constructor is specialtype of method, enables an object to initialize itself when it's created. A constructor is a public method with the same name as the class. class A { int a; public A(int value) { a = value; } }
  • 16.
    16 Class's constructor startswith the public keyword. This is important because we want to be able to create an object from the class anywhere in program, and when we create an object, you're actually calling its constructor. After the public keyword comes the name of the constructor followed by the constructor's arguments in parentheses. When you create an object of the class, you must also provide the required arguments.
  • 17.
    17 There are twotypes of constructor Default Constructor Parameterized constructor Each time a new object is created at least one constructor will be invoked. A obj1=new A()//Default constructor invoked A obj2=new A() //Parameterized constructor invoked
  • 18.
    18 Constructor Overloading Overloaded constructorsare differentiated on the basis of their type of parameters or number of parameters. class A { int a; public A() { a=0; } public A(int value) { a = value; } }
  • 19.
    19 Using the thisKeyword Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. we can refer to any member of the current object from within an instance method or a constructor by using this.
  • 20.
    20 public class Point {public int x = 0; public int y = 0; public Point(int x, int y) { this.x = x; this.y = y; } } Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.
  • 21.
    21 Static Same data isused for all the instances (objects) of some Class. Class A { public int y = 0; public static int x= 1; }; A a = new A(); A b = new A(); System.out.println(b.x); a.x = 5; System.out.println(b.x); A.x= 10; System.out.println(b.x); Assignment performed on the first access to the Class. Only one instance of ‘x’ exists in memory Output: 1 5 10 a b y y A.x_ 0 0 1
  • 22.
    22 Static member function Staticmember function can access only static members Static member function can be called without an instance. Class TeaPot { private static int numOfTP = 0; private Color myColor_; public TeaPot(Color c) { myColor_ = c; numOfTP++; } public static int howManyTeaPots() { return numOfTP; } // error : public static Color getColor() { return myColor_; } }
  • 23.
    23 TeaPot tp1 =new TeaPot(Color.RED); TeaPot tp2 = new TeaPot(Color.GREEN); System.out.println(“We have “ + TeaPot.howManyTeaPots()+ “Tea Pots”); Static Block Code that is executed in the first reference to the class. Several static blocks can exist in the same class ( Execution order is by the appearance order in the class definition ). Only static members can be accessed.
  • 24.
    24 Class design hints 1.Alwayskeep data private. 2.Always initialize data. 3.Don’t use too many basic type in class. 4.Not all fields indivisual field accessor and mutators. 5.Brak up classes that have too many responsibilities. 6.Make the name of your classes and method that reflect their responsibilty.
  • 25.