Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arranging the border values of methods. import java.awt.*; import java.applet.Applet; public class Applet2 extends Applet { public void paint(Graphics."— Presentation transcript:

1 Arranging the border values of methods

2 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); }

3

4 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);

5 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); } }

6

7 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);

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

9 A hypothetical TextBook application

10  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.

11 Instances of the Pendulum class

12 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); } ……… }

13 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.

14 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.

15 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.

16 Private, default, protected, and public visibility

17 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.

18 Private, default, protected, and public visibility

19 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.

20 The visibility levels available in Java ModifierVisibility privateNone None (default)Classes in the package protectedClasses in the package and subclasses inside and outside the package publicAll 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.

21 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.

22 Method Overriding

23  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();

24 Method Overriding  Overriden 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.

25 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.


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

Similar presentations


Ads by Google