Basic Java Programming
Sasidhara Marrapu
 Classes
 Properties and Methods
 Constructors
 Objects
 Java Access Modifiers
 Lab Problem
 Home Work
Java Access Modifiers
3
Classes and Objects
• A Java program consists of one or more classes
• A class is an abstract description/Blueprint of objects
• Every class is Subclass (child) of Object
• A class is used as a template or pattern to create new
objects (instances of the class)
4
Classes and Objects
• A class defines the characteristics that are common to a
group of similar objects
• defines the data/variables/properties that hold the object’s state
• defines the methods/behaviors that describe the object’s behaviors
Lamp is an object
• It can be in on or off state.
• You can turn on and turn off lamp (behavior).
Bicycle is an object
• It has current gear, two wheels, number of gear etc. states.
• It has braking, accelerating, changing gears etc. behavior.
5
Class Structure
• A program consists of one or more
classes
• Typically, each class is in a separate
.java file
Program
File File
File
File
Class
Variables
Constructors
Methods
Variables
Variables
Statements
Statements
6
Classes and Objects
public class Person {
// data or variables that hold object state
String name;
int age;
String gender;
// Behaviors
public void canRead() {
}
public void canWrite() {
}
7
Classes and Objects
• A class is used as a template or pattern to create new
objects (instances of the class)
• Every object is an instance of a class
• each object has its own name (identity)
• each object has its own variables (state)
• all instances of the class share the methods defined by the
class (behaviors)
Methods
• A method in a class performs specific behaviors by using
statements
• A statement causes the object to do something
• Class can have many methods
• Two types of methods
• Class level – static methods – can be accessible directly
• Instance/Member/Object level methods – can only be
accessible after creating object/instance.
Methods
Constructor
• A method in a class that initialize an instance of an
object before it's used.
•The same name as the class and have no return type
• Multiple Constructors: the same name but a different
number of arguments or different typed arguments
• Java Provides default constructors
• The special variable, this, can be used inside a method
to refer to the object instance.
11
Constructors
public class Person {
// data or variables that hold
object state
String name;
int age;
String gender;
Person(){
}
Person(String personName){
this.name = personName;
}
12
Creating objects of a class
• Objects are created dynamically using the new keyword.
• student and employee refer to Person objects
employee = new Person(“John”) ;
student = new Person() ;
13
Creating objects of a class
student = new Person();
employee = new Person(“John”) ;
employee = student;
P
student
Q
employee
Before Assignment
P
student
Q
employee
After Assignment
14
Automatic garbage collection
• The object does not have a reference and cannot be
used in future.
• The object becomes a candidate for automatic garbage
collection.
• Java automatically collects garbage periodically and releases
the memory used to be used in the future.
Q
15
Accessing Object Data
Person person = new Person();
person.age = 17
person.name = “Mary”
person.gender = “Female”
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
16
Executing Methods in Object
• Using Object Methods:
sent ‘message’ to aCircle
Person person = new Person();
person.canRead()
Passing Information into a Method
• Argument types
•primitive and reference data type: Yes
•method: No
• Argument Names
•Can have the same name as one of the class's member
variable
• Use this to refer to the member variable
• Primitive arguments are passed by value.
• Reference arguments are passed by reference.
Using
package,
import, and
CLASSPATH
Java
Access
Specifiers
class DoFoo {
public static void main(String[] args) {
Foo f = new Foo();
f.fun();
f.i = f.i + 1;
}
}
public class Foo {
// . . .
int i;
// . . .
fun(){
i = i + 1;
}
}
new
Foo.class
javac
Access
to
What?
Access
to
Everything
Java Access Specifiers
• public
• protected
• "friendly“/default
• private
Packages
• Assume the files are class
files
• Assume all the files in the
same folder are declared to
be in the appropriate
package relative to the
CLASSPATH
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
Can this
reference
access this
member?
Where Can We Access
"i" in Foo From?
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
Can this
reference
access this
member?
Where Can We Access
"i" in Foo From?
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Y
Y
Y
Y
Y
Y
Y
public class Foo {
// . . .
public int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
public int i;
public class Foo {
// . . .
int i;
// . . .
fun(){
i++;
}
}
N
Y
Y
N
N
N
N
public class Foo {
// . . .
int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
int i;
public class Foo {
// . . .
private int i;
// . . .
fun(){
i++;
}
}
N
N
N
N
N
N
N
public class Foo {
// . . .
private int i;
// . . .
fun(){
i++;
}
}
Foo f = new Foo();
f.i++;
private int i;
Inheritance &
Access
obj
Inheritance &
Access
obj
Class Bar extends Foo {
// Stuff
}
Class Foo . . . {
// Stuff
}
Inheritance &
Access
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
Foo f = new Foo();
f.i++;
Inheritance &
Access
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
N
Y
Y
Y
N
N
N
public class Foo ext. . . {
// . . .
protected int i;
// . . .
fun(){
i++;
}
}
obj
Foo f = new Foo();
f.i++;
Inheritance &
Access
The Set View
The Member Accessed
from anywhere
from same package
from same class
public "friendly" private
public "friendly" private
The Member Accessed
from anywhere
from same package
from a child class
from same class
protected
public "friendly" private
You May Wish to Order Declarations
• public
• protected
• "friendly"
• private
Summary
• Access is based on library structure (packages) and inheritance.
• The less you expose the more flexibility is preserved
Home Work

Pj01 x-classes and objects

  • 1.
  • 2.
     Classes  Propertiesand Methods  Constructors  Objects  Java Access Modifiers  Lab Problem  Home Work Java Access Modifiers
  • 3.
    3 Classes and Objects •A Java program consists of one or more classes • A class is an abstract description/Blueprint of objects • Every class is Subclass (child) of Object • A class is used as a template or pattern to create new objects (instances of the class)
  • 4.
    4 Classes and Objects •A class defines the characteristics that are common to a group of similar objects • defines the data/variables/properties that hold the object’s state • defines the methods/behaviors that describe the object’s behaviors Lamp is an object • It can be in on or off state. • You can turn on and turn off lamp (behavior). Bicycle is an object • It has current gear, two wheels, number of gear etc. states. • It has braking, accelerating, changing gears etc. behavior.
  • 5.
    5 Class Structure • Aprogram consists of one or more classes • Typically, each class is in a separate .java file Program File File File File Class Variables Constructors Methods Variables Variables Statements Statements
  • 6.
    6 Classes and Objects publicclass Person { // data or variables that hold object state String name; int age; String gender; // Behaviors public void canRead() { } public void canWrite() { }
  • 7.
    7 Classes and Objects •A class is used as a template or pattern to create new objects (instances of the class) • Every object is an instance of a class • each object has its own name (identity) • each object has its own variables (state) • all instances of the class share the methods defined by the class (behaviors)
  • 8.
    Methods • A methodin a class performs specific behaviors by using statements • A statement causes the object to do something • Class can have many methods • Two types of methods • Class level – static methods – can be accessible directly • Instance/Member/Object level methods – can only be accessible after creating object/instance.
  • 9.
  • 10.
    Constructor • A methodin a class that initialize an instance of an object before it's used. •The same name as the class and have no return type • Multiple Constructors: the same name but a different number of arguments or different typed arguments • Java Provides default constructors • The special variable, this, can be used inside a method to refer to the object instance.
  • 11.
    11 Constructors public class Person{ // data or variables that hold object state String name; int age; String gender; Person(){ } Person(String personName){ this.name = personName; }
  • 12.
    12 Creating objects ofa class • Objects are created dynamically using the new keyword. • student and employee refer to Person objects employee = new Person(“John”) ; student = new Person() ;
  • 13.
    13 Creating objects ofa class student = new Person(); employee = new Person(“John”) ; employee = student; P student Q employee Before Assignment P student Q employee After Assignment
  • 14.
    14 Automatic garbage collection •The object does not have a reference and cannot be used in future. • The object becomes a candidate for automatic garbage collection. • Java automatically collects garbage periodically and releases the memory used to be used in the future. Q
  • 15.
    15 Accessing Object Data Personperson = new Person(); person.age = 17 person.name = “Mary” person.gender = “Female” ObjectName.VariableName ObjectName.MethodName(parameter-list)
  • 16.
    16 Executing Methods inObject • Using Object Methods: sent ‘message’ to aCircle Person person = new Person(); person.canRead()
  • 17.
    Passing Information intoa Method • Argument types •primitive and reference data type: Yes •method: No • Argument Names •Can have the same name as one of the class's member variable • Use this to refer to the member variable • Primitive arguments are passed by value. • Reference arguments are passed by reference.
  • 18.
  • 19.
    class DoFoo { publicstatic void main(String[] args) { Foo f = new Foo(); f.fun(); f.i = f.i + 1; } } public class Foo { // . . . int i; // . . . fun(){ i = i + 1; } } new Foo.class javac Access to What? Access to Everything
  • 20.
    Java Access Specifiers •public • protected • "friendly“/default • private
  • 21.
    Packages • Assume thefiles are class files • Assume all the files in the same folder are declared to be in the appropriate package relative to the CLASSPATH
  • 22.
    public class Foo{ // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; Can this reference access this member? Where Can We Access "i" in Foo From?
  • 23.
    public class Foo{ // . . . public int i; // . . . fun(){ i++; } } public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; Can this reference access this member? Where Can We Access "i" in Foo From?
  • 24.
    public class Foo{ // . . . public int i; // . . . fun(){ i++; } } Y Y Y Y Y Y Y public class Foo { // . . . public int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; public int i;
  • 25.
    public class Foo{ // . . . int i; // . . . fun(){ i++; } } N Y Y N N N N public class Foo { // . . . int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; int i;
  • 26.
    public class Foo{ // . . . private int i; // . . . fun(){ i++; } } N N N N N N N public class Foo { // . . . private int i; // . . . fun(){ i++; } } Foo f = new Foo(); f.i++; private int i;
  • 27.
  • 28.
  • 29.
    obj Class Bar extendsFoo { // Stuff } Class Foo . . . { // Stuff } Inheritance & Access
  • 30.
    public class Fooext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj Foo f = new Foo(); f.i++; Inheritance & Access
  • 31.
    public class Fooext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj N Y Y Y N N N public class Foo ext. . . { // . . . protected int i; // . . . fun(){ i++; } } obj Foo f = new Foo(); f.i++; Inheritance & Access
  • 32.
  • 33.
    The Member Accessed fromanywhere from same package from same class public "friendly" private
  • 34.
    public "friendly" private TheMember Accessed from anywhere from same package from a child class from same class protected public "friendly" private
  • 35.
    You May Wishto Order Declarations • public • protected • "friendly" • private
  • 36.
    Summary • Access isbased on library structure (packages) and inheritance. • The less you expose the more flexibility is preserved
  • 37.