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

Slides:



Advertisements
Similar presentations
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance using Java
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance The Basics in Java. Definition  A class that is derived from another class is called a subclass (also a derived class, extended class, or.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Inherited Classes in Java CSCI 392 Ch 6 in O’Reilly Adapted from Dannelly.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
External Scope CECS 277 Mimi Opkins.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Overriding Method.
Inheritance and Polymorphism
12 Data abstraction Packages and encapsulation
Chapter 9 Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Advanced Java Topics Chapter 9
Java – Inheritance.
Fundaments of Game Design
Chapter 11 Inheritance and Encapsulation and Polymorphism
C++ Object Oriented 1.
CSG2H3 Object Oriented Programming
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Chapter 5 Classes.
Presentation transcript:

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.

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.

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.

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

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

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

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.