0% found this document useful (0 votes)
44 views

02 - Classes (Lec2)

Java uses classes and objects to implement object-oriented programming. A class defines the data fields and methods that characterize objects of that class. Objects are instances of classes that have a state defined by their data fields and a behavior defined by their methods. Classes support encapsulation through the use of access modifiers like public and private for data fields and methods. They also support inheritance, polymorphism, and abstraction through subclassing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

02 - Classes (Lec2)

Java uses classes and objects to implement object-oriented programming. A class defines the data fields and methods that characterize objects of that class. Objects are instances of classes that have a state defined by their data fields and a behavior defined by their methods. Classes support encapsulation through the use of access modifiers like public and private for data fields and methods. They also support inheritance, polymorphism, and abstraction through subclassing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Classes and Objects

Dr. Mona Nashaat


Development lifecycle
- Java is compiled and interpreted
• source code is compiled into bytecode (low-level,
platform independent code)
• bytecode is interpreted (real machine code produced
at run time) often fast and portable (“write once run
anywhere”)
- Dynamic linking (no link phase at compile time)
• Program consists of class definitions
• each class is compiled into a separate class file
• classes may refer to each other, references are
resolved at run-time

2
Development lifecycle

3
Class file?

- The binary form for Java programs

- Represents a complete description of one Java class or


interface

- Platform independent bytecodes are the machine


language of the JVM

- Supports linking code from other languages

4
How a JVM works?

- Start with a method named “main” in a given class file.

- The method must have certain properties

- Continue execution in other methods as necessary.

5
Classes and Objects
- An object has both a state and behavior. The state
defines the object, and the behavior defines what the
object does.
Class Name:
A class template
Circle
Data fields:
radius
Methods:
getArea

Circle object1 Circle object2 Circle object3


Data fields: radius=10 Data fields: radius=25 Data fields: radius=30

Three objects of the Circle class


6
Classes

- Classes are constructs that define objects of the same


type.

- A Java class uses variables to define data fields and


methods to define behaviors.

- Additionally, a class provides a special type of


method(s), known as constructors, which are invoked to
construct objects from the class.

7
Classes

class Circle {
/** The radius of this circle */ Data field
double radius = 1.0;
/** Construct a circle object */ Constructors
Circle() { }
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */ Method
double getArea() {
return radius * radius * 3.14159;
}
}
8
Know Your Data item
- Fields define the state; local variables don’t

class Car {
String licensePlate = ""; // field
double speed = 0.0; // field
static double maxSpeed; = 123.45;// fields
boolean isSpeeding(int i) {// parameters
double excess; // local variable
excess = maxSpeed - this.speed;
if (excess < 0) return true;
else return false;
}
}

9
Constructors

- Constructors are a special kind of methods that are


invoked to construct objects.

class Circle {
…….
Circle() { }

/** Construct a circle object */


Circle(double newRadius) {
radius = newRadius;
}
}

10
Constructors

- Constructors must have the same name as the class


itself.

- Constructors do not have a return type—not even void.

- Constructors are invoked using the new operator when


an object is created.

- Constructors play the role of initializing objects.

11
Declaring Object Reference Variables

ClassName objectName;

- Example:

Circle myCircle;

12
Accessing Objects

- Referencing the object’s data:


objectName.data

- e.g., myCircle.radius

- Invoking the object’s method:


objectName.methodName(arguments)

- e.g., myCircle.getArea()

13
Using “this”

- this refers to the current object.

class Circle {
…....
Circle(double newRadius) {
this.radius = newRadius;
}
…..
}

14
The null Value

- If a data field of a reference type does not reference any


object, the data field holds a special literal value, null.

- Differences between Variables of Primitive Data Types


and Object Types

15
Copying Variables of Primitive Data Types

Assigning primitive data: i=j

16
Copying Variables of Object Types

Assigning objects: c1=c2

17
Garbage Collection

- As shown in the previous figure, after the assignment


statement c1 = c2, c1 points to the same object
referenced by c2.

- The object previously referenced by c1 is no longer


referenced.

- This object is known as garbage.

- Garbage is automatically collected by JVM.

18
Array of Objects

Circle[] circleArray = new Circle[10];

19
Visibility Modifiers
- By default, the class, variable, or method can be
accessed by any class in the same package.
- public: The class, data, or method is visible to any
class in any package.
- private: The data or methods can be accessed only by
the declaring class.
- The get and set methods are used to read and modify
private properties.

- Rule #1:
Data Fields should always be private.
Always, Hide Information
20
Accessibility of Methods
1. public: visible everywhere
2. protected: visible in the package and in subclasses
3. Default (no modifier): visible in the package
4. private: visible only inside the class (more on this later)
- A method can access fields and methods that are
visible to it.
• Public method and fields of any class
• Protected fields and methods of superclasses and
classes in the same package
• Fields and methods without modifiers of classes in the
same packages
• Private fields and methods of the same class.
21
Accessibility of Methods

22
Accessibility of Methods

- The private modifier restricts access to within a class.

- The default modifier restricts access to within a


package.

- The public modifier enables unrestricted access.

23
Types of Methods

- Accessor methods:
public String getName()
{ return name; }
- Mutator methods:
Public void setSalary(double newSalary)
{ salary = newSalary;}

24
Static Methods

- Declared with modifier static.

- It belongs to class rather than any individual object.

- Sometimes called class method.

- Usage:

className.staticMethod()

NOT objectName.staticMethod()

25
Static Methods

class Employee{
public static int getNumOfEmployees()
{
return numOfEmpolyees;
}
private static int numOfEmployees = 0;

}
//to invoke it:
Employee.getNumOfEmployee(); // ok
Harry.getNumOfEmployee(); // not this one

26
Static Methods

- The main method is always static because when it is


called, there are not objects yet.

class EmployeeTest
{
public static void main(String[] args)
{… }
}

27
Conclusions

- Java is an object-oriented programming languages.


- The main concepts of OOP are Abstraction,
Encapsulation, Polymorphism, and Inheritance.
- Array is a collection of elements of same type.
- Arrays have fixed length and cannot be changed once
created.
- Arrays are passed to methods by reference not by
value.

28

You might also like