Download File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
 Instance variables, set and get methods
Encapsulation
 Access Modifiers
 Default Access Modifier
 Private Access Modifier
 Public Access Modifier
 Program Example and UML
 Constructor
 Default and Parameter Constructor
 Program and UML
Instance variables:
class variables are called as instance variables, these variables are created
whenever an object of the class is instantiated.
class Rectangle {
public int length;
public int width;
public double height
}
//length of rectangle
//width of rectangle
//height of rectangle
length, width, height are called instance variables. length and width
is initialization with 0 and height is initialization with 0.0
set and get methods:
These methods are used for storing and accessing the instance variables of the
class.
For Example:
class BankAccount {
private int accountNo;
private double balance;
public void setAccountNo(int accNo){
accountNo = accNo;
}
public int getAccountNo(){
return accountNo;
}
public void setBalanace(double bal){
balance = bal;
}
public double getBalance(){
return balance;
}
}
Encapsulation is one of the four fundamental OOP concepts.
Encapsulation can be described as a protective barrier that prevents the code and
data being randomly accessed by other code defined outside the class. Access to
the data and code is tightly controlled.
Benefits of Encapsulation:
 The fields of a class can be made read-only or write-only.
 A class can have total control over what is stored in its fields.
 The users of a class do not know how the class stores its data. A class can
change the data type of a field, and users of the class do not need to change any
of their code.
Object encapsulation can be done in java by using access modifiers
because they control access to the members of a class. The access modifiers are
also known as visibility
methods of a class.
modifiers
that can be applied to the variables and
Java provides a number of access modifiers to help you set the level of access you
want for classes as well as the fields, methods and constructors in your classes.
Access Modifiers
1. default
2. private
3. public
4.protected
Default access modifier means we do not explicitly declare an access modifier for a
class, field, method etc.
A variable or method declared without any access control modifier is available to
any other class in the same package. The default modifier cannot be used for
methods, fields in an interface.
Example: Variables and methods can be declared without any modifiers, as in the
following examples:
String version = “1.5.1”
boolean processOrder(){
return value;
}
Methods, Variables that are declared private can only be accessed within the
declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces
cannot be private.
Variables that are declared private can be accessed outside the class if public
getter methods are present in the class.
Using the private modifier is the main way that an object encapsulates itself and
hide data from the outside world.
Example:
The following class uses private access control:
class Logger{
private String format;
public String getFormat(){
return this.format
}
public void setFormat(String format){
this.format = format;
}
}
Within an instance method or a
constructor, this is a reference to the
current object — the object whose
method or constructor is being called.
You can refer to any member of the
current object from within an instance
method or a constructor by using this.
Here, the format variable of the
Logger class is private, so there's
no way for other classes to
retrieve or set its value directly.
So to make this variable available
to the outside world, we defined
two public methods: getFormat(),
which returns the value of format,
and setFormat(String), which sets
its value.
The most common reason for using
the this keyword is because a field is
shadowed
by
a
method
or
constructor parameter.
A class, method, constructor, interface etc declared public can be accessed from
any other class. Therefore fields, methods, blocks declared inside a public class can
be accessed from any class belonging to the Java Universe.
However if the public class we are trying to access is in a different package, then
the public class still need to be imported.
Example:
The following function uses public access control:
public static void main(String args[ ]){
//......
}
The main() method of an application has to be public. Otherwise, it could not be
called by a Java interpreter (such as java) to run the class.
1
// Fig. 3.7: GradeBook.java
2
// GradeBook class that contains a courseName instance variable
3
// and methods to set and get its value.
4
5
public class GradeBook
6
7
8
{
private String courseName; // course name for this GradeBook
9
10
11
// method to set the course name
public void setCourseName( String name )
{
12
13
14
courseName = name; // store the course name
} // end method setCourseName
15
16
17
18
// method to retrieve the course name
public String getCourseName()
{
return courseName;
19
20
} // end method getCourseName
21
22
// display a welcome message to the GradeBook user
public void displayMessage()
23
{
24
25
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
26
27
System.out.printf( "Welcome to the grade book for\n%s!\n",
getCourseName() );
28
} // end method displayMessage
29
30 } // end class GradeBook
GradeBook
- courseName : String
+ setCorseName(name: String)
+ getCourseName() : String
+ displayMessage()
1
// Fig. 3.8: GradeBookTest.java
2
// Create and manipulate a GradeBook object.
3
import java.util.Scanner; // program uses Scanner
4
5
public class GradeBookTest
6
{
7
// main method begins program execution
8
public static void main( String args[] )
9
{
10
// create Scanner to obtain input from command window
11
Scanner input = new Scanner( System.in );
12
13
// create a GradeBook object and assign it to myGradeBook
14
GradeBook myGradeBook = new GradeBook();
15
16
// display initial value of courseName
17
System.out.printf( "Initial course name is: %s\n\n",
18
19
myGradeBook.getCourseName() );
It would be simpler and more concise to initialize an object when it is first created.
Java supports special type of method, called Constructor, that enables an Object to
initialize itself when it is created.
A java constructor has the same name as the name of the class to which it belongs.
Constructor are of twp type: they are
 Default Constructor
 Parameterized constructor
Default Constructor:
If you don’t define a constructor for a class, a default parameter less
constructor is automatically created by the compiler. The default
constructor is called and initialized with (Zero to numeric types , null for
object references and false for Booleans);
Parameter less constructor:
Constructor with no parameters is called parameter less constructor.
Parameterized constructor:
Constructors that can take arguments are termed as parameterized
constructors. The types must match those that are specified in the constructor
definition.
Example: 1
public class Rectangle {
int length, width, height
Rectangle(){
Length = 10;
width = 30;
height = 10;
}
}
Example: 2
public class Rectangle {
int length, width, height
Rectangle(int len, int wid, int hei){
Length = len;
width = wid;
height = hei;
}
}
GradeBook
- courseName : String
<<constructor>> GradeBook(name : String)
+ setCorseName(name: String)
+ getCourseName() : String
+ displayMessage()