a class in java is a software construct that includes fields (also called data fields or class scope...

12
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods which are functions operating on these data and possibly on the data belonging to other class instances. This combining of the data and related operations is called data encapsulation. An object is an instance of a class, an entity created using a class definition. Object-oriented programming Data Structures and Algorithms in Java, Third Edition Ch01–1

Upload: sharlene-lane

Post on 30-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

A class in Java is a software construct that includes • fields (also called data fields or class scope variables) to

provide data specification, and • methods which are functions operating on these data

and possibly on the data belonging to other class instances.

This combining of the data and related operations is called data encapsulation. An object is an instance of a class, an entity created using a class definition.

Object-oriented programming

Data Structures and Algorithms in Java, Third Edition Ch01–1

Page 2: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Object-oriented programmingClasses correspond to concepts defined in terms of common properties and common behavior, objects corresponds to actualization of these concepts.

• name• population• area• loan• sender• compute population density • get international loan

Country objects:Country class:

Data Structures and Algorithms in Java, Third Edition Ch01–2

Page 3: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Classes and objects in Javaclass Country { String name; long population; double area; double loan; String sender; double density() { return area/population; } void internationalLoan(double ln, String from) { loan = ln; sender = from; } . . . . . . .}

class Countries { Country poland = new Country(); Country brazil = new Country(); . . . . . . .}

data fields

methods

objects

Data Structures and Algorithms in Java, Third Edition Ch01–3

Page 4: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Objects in Java

Country poland = new Country();Country brazil = new Country();

"Poland"40000000

poland

120359

.5

"Germany"

"Brazil"180000000

brazil

3287951

1

"USA"

Data Structures and Algorithms in Java, Third Edition Ch01–4

Page 5: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Inheritance

A new class can be derived form an existing class whereby the new class automatically includes (inherits) fields and methods of the existing class. The former is called a subclass or derived class, the latter is called a superclass of base class.

Data Structures and Algorithms in Java, Third Edition Ch01–5

Page 6: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

class C1 { int n; double d; void f(int m) { . . . } double g(double x) { . . . }}

Inheritance

class C2 extends C1 { int n; double y = g(d); void f(int m) { . . . } double h(double x) { super.n = 10; return n * x; }}

establish inheritance

C2’s variable n,

C1’s variable n is still accessible

method f() is redefined

method h() is newly defined and so is variable y

variable d and method g() are inherited

Data Structures and Algorithms in Java, Third Edition Ch01–6

Page 7: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Accessibility control

• The private modifier indicates methods and fields that can be used only by this class

• The protected modifier means that a method or a data field is accessible to derived classes and in the package that includes the class that declares the method or the data field.

• A default modifier is no modifier at all, which indicates access to methods and fields in the package that includes the class that declares the methods or the data fields.

• Methods and fields declared public can be used by any other object.

Data Structures and Algorithms in Java, Third Edition Ch01–7

Page 8: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

place of access private protected

no modifi

er (≠ C++)

public

same classsame package subclasssame package non-

subclassdifferent package subclassdifferent package non-

subclass

yesnononono

yesyesyes (≠ C+

+)yesno

yesyesyesnono

yesyesyesyesyes

Accessibility control

Data Structures and Algorithms in Java, Third Edition Ch01–8

Page 9: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

class C1 { private int k = 11; protected int m = 12; int n = 13; // package public int p = 14;}

class C2 extends C1 { C2() { k = 21; m = 22; n = 23; p = 24; }}

class C3 extends C1 { C3() { k = 31; m = 32; n = 33; p = 34; }}

class C4 { void f() { C1 c1 = new C1(); c1.k = 41; c1.m = 42; c1.n = 43; c1.p = 44; }}

class C5 { void f() { C1 c1 = new C1(); c1.k = 51; c1.m = 52; c1.n = 53; c1.p = 54; }}

// //

////

two packages

m is protected

k is private

k is private

k is private

k is private //

n is in different package

n is in different package

//

//

Page 10: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

Static and dynamic binding

• Java checks the type of object to which a reference is made and chooses the method appropriate for this type at compilation time producing static binding during run time producing dynamic binding.

• Polymorphism is an ability to associate with the same method name different meanings through the mechanism of dynamic binding.

Data Structures and Algorithms in Java, Third Edition Ch01–10

Page 11: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

class A { public void process() { System.out.println("Inside A"); } void f(A a) { a.process(); }}class ExtA1 extends A { public void process() { System.out.println("Inside ExtA1"); }}class ExtA2 extends A { public void process() { System.out.println("Inside ExtA2"); }}

Dynamic binding

A ob = new A();ob.process();ob = new ExtA1();ob.process();ob = new ExtA2();ob.process();

Inside AInside ExtA1Inside ExtA2

output

Data Structures and Algorithms in Java, Third Edition Ch01–11

Page 12: A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods

class A { public void process() { System.out.println("Inside A"); } void f(A a) { a.process(); }}

Dynamic binding

A ob = new A(); A a1 = new A();ExtA1 a2 = new ExtA1();ExtA2 a3 = new ExtA2();ob.f(a1);ob.f(a2);ob.f(a3);

Inside AInside ExtA1Inside ExtA2

output

Data Structures and Algorithms in Java, Third Edition Ch01–12