25
Arranging the border values of methods

Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics

Embed Size (px)

Citation preview

Arranging the border values of methods

import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics g) { g.drawString(“Zeynep", 25, 60);

g.drawString (“Ali", 25, 85); g.drawString (“Fatma", 25, 105); g.setColor(Color.green); g.fillRect(30, 40, 100, 15); g.setColor(Color.blue); g.fillRect(30, 65, 135, 15); g.setColor(Color.yellow); g.fillRect(30, 90, 170, 15); } }

g.drawString("Zeynep", 25, 70);g.drawString ("Ali", 25, 85);g.drawString ("Fatma", 25, 105);g.setColor(Color.green);g.fillRect(70, 60, 100, 10);g.setColor(Color.blue);g.fillRect(70, 80, 135, 10);g.setColor(Color.yellow);g.fillRect(70, 100, 170, 10);

import java.awt.*;

import java.applet.Applet; public class Applet3 extends Applet { public void paint(Graphics g ){ //write name labels g.drawString(“Zeynep",0,50); g.drawString(“Ali",0,75); g.drawString(“Okul",0,100);

g.setColor(Color.blue); //draw and label name Zeynep g.fillRect(15,25,150,20); g.drawString(“first winner",184,50); g.setColor(Color.green); //draw and label Ali

g.fillRect(15,65,175,20); g.drawString(“secod winner",194,75); g.setColor(Color.red); //draw and label name Fatma

g.fillRect(15,105,200,20); g.drawString(“third winner",204,100); } }

g.setColor (Color.blue)g.fillRect (100,25,150,20);g.drawString ("first winner",255,40);g.setColor (Color.green); g.fillRect (85,65,175,20);g.drawString ("second winner",265,80);g.setColor (Color.red); g.fillRect (105,105,200,20);g.drawString("thirdwinner",310,120);

g.drawString(“Zeynep",0,50); g.drawString(“Ali",0,75); g.drawString(“Okul",0,100);

g.drawString("Zeynep",0,40);g.drawString("Ali",0,80);g.drawString("Okul",0,125);

A hypothetical TextBook application

The hypothetical TextBook application uses two instances of Pendulum through reference-type variables bigPendulum and smallPendulum.

Each of these Pendulum objects has its own copy of mass, length, and cycles.

As with variables, methods defined in a class may be instance methods or static methods.

An instance method is associated with an instance of the class, but each instance doesn’t really have its own copy of method.

There’s just one copy of the method, which operates on the values of the instance variables of a particular object.

ClassPendulum

float mass;float length;int cycles;position ();

Class TextBook

Pendulum bigPendulum;

Pendulum smallPendulum; PendulumsmallPendulumfloat mas =10.0;

float length = 10.0;int cycles =0.0;

position( );

PendulumbigPendulumfloat mas =10.0;

float length = 10.0;int cycles =0.0;

position( );

Instances of the Pendulum class

Accessing Fields and Methods

• Inside a class, we can access instance variables and call instance methods of the class directly by name.

class Pendulum {

… ……….

void resetEverything() {

mass=1.0;

length=1.0;

cycles=0;

…….

float startingPosition =position (0.0);

}

………

}

Accessing Fields and Methods Other classes access members of an object through a reference, using

C-style dot notation:class TextBook {…….. void showPendulum( ) { Pendulum.bob =new Pendulum( ); …………………… int i =bob.cycles; bob.resetEverything( ); bob. mass=1.01; ………} ……….} Here we have created a second class TextBook, that uses Pendulum

object. It creates an instance in showPendulum,

invokes methods and access variables of the object through reference bob.

Accessing Fields and Methods con’t In the previous example, we could change the declaration of

the variable cycles to private:

Class Pendulum {……….Private int cycles;……

Now we can’t access cycles from TexrBook:

Class TextBook{…….. void showPendulum ( ) { ……..İnt i= bob.cycles; //Compile time error

• If we need to access cycles, we might add a public getCycles ( ) method to the Pendulum class.

Visibility of Variables and Methods

As explained before, information hiding or encapsulation is one of the most important aspects of OOD.

By treating an object in some respects as a “black box”,

and

ignoring the details of its implementation,

it is possible to write stronger, simpler code with components that can be easily reused.

Several factors affect whether class members can be accessed outside the class.

It is possible to use visibility modifiers public, private, and protected to control the access;

Classes can also be placed into packages which affects their scope.

Public class TextAreaprivate char [ ] text;

int linecount; // default visibilityprotected void formatText ();

public void getText();public void set/ext();

Public class TextEditor

package mytools.text

visible

visible

ClassBook

class MyTextDisplayextend TextArea

visible

visible

visible

Private, default,protected, and public visibility

Basic Access Modifiers By default, the variables and methods of a class are

accessible to members of the class itself and to the other classes in the same package

The classes in the same package are friendly This is the default level of visibility

The private modifier designates a variable or method for only use by other members of the class itself.

The methods and variables declared private are accessible only within their class

Members declared as public are accessible from any class in any class, provided the class itself can be seen.

Public class TextAreaprivate char [ ] text;

int linecount; // default visibilityprotected void formatText ();

public void getText();public void set/ext();

Public class TextEditor

package mytools.text

visible

visible

ClassBook

class MyTextDisplayextend TextArea

visible

visible

visible

Private, default,protected, and public visibility

Visibility Modifiers Public members in TextArea are accessible from

anywhere. Private members are not visible from outside the

class Default visibility allows access by other classes in

the package. The protected modifier allows special access

permission for subclasses. Protected is slightly less restrictive than the default level of

accessibility.In addition to default access afforded classes in the same

package, protected members are visible to subclasses of the class, even if they are defined in a different package.

The visibility levels available in Java

Modifier Visibility

private None

None (default) Classes in the package

protected Classes in the package and subclasses inside and outside the package

public All classes

Visibility levels in Java runs from most restrictive to least. Methods and variables are always visible within a class

so the table doesn’t address those.

Subclasses and Visibility Subclasses add two important (but unrelated)

complications to the topic of visibility Firstly; When we override methods in a subclass, the overriding

method must be at least as visible as the overridden method.

Overriding Methods A subclass can define a method that has exactly the same

method arguments and return type as a method in its superclass.In that case,

The method in the subclass overrides the method in the superclass and replaces its implementation

Overriding methods to change the behavior of objects is called sub-type polymorphism.

Method Overriding

Cat Simoneat ()reproduce()sleep()huntMice()

purr()

Class Animaleat ()

sleep ()reproduce()

Class Mammalreproduce ( )

Class Catsleep ()

huntMice()purr()

ove

rrid

es

ove

rrid

es

Method Overriding Mammal overrides the reproduce () method of

Animal. The Cat object’s sleeping behavior is overriden to

be different from that of a general Animal The Cat class also adds the more unique behaviors

of purring and hunting mice. If we have a Cat instance assign to a variable of

the more general type Animal and we call its sleep() method,

We get sleep() method implemented in the Cat class, not the one in Animal.

Cat Simon = new Cat();Animal creature = Simon;……creature.sleep(); //accesses Cat sleep();

Method OverridingOverriden methods probably look like they shadow methods in superclasses, just as variables do.

An overriden method in Java acts like virtual methods in C++.

When there are multiple implementations of a method in the inheritance hierarchy of an object,

the one in the most derived class (the lowest one in the hierarchy) always overrides the others,

even if we refer to the object by way of a less derived type.

Subclasses and Visibility con’t When it is possible to take a private method and override it

with a public method in a subclass, the reverse is not possible.

We can’t override a public method with a private method.

A Mammal is a subclass of Animal and therefore must be usable as an Animal. i.e.;

if we realize that subtypes have to be usable as instances of their supertype.

If we could override a method with less visible method, we would have a problem.

our Mammal might not be able to do all the things an Animal can.

However, we can reduce the visibility of a variable. That is;The variable acts like any other shadowed variable;

the two variables are distinct and can have separate visibilities in different classes.