Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.

Презентация:



Advertisements
Похожие презентации
1 © Luxoft Training 2012 Inner and anonymous classes.
Advertisements

Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Unit II Constructor Cont… Destructor Default constructor.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
1/27 Chapter 9: Template Functions And Template Classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
What to expect? How to prepare? What to do? How to win and find a good job? BUSINESS ENGLISH COURSE NOVA KAKHOVKA GUMNASUIM 2012.
How can we measure distances in open space. Distances in open space.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
1. Do you have a lot of friends? Yes, I can say that I have quite a lot of friends. But I have only two special ones. We spend plenty of time together,
CONSTRAINTS 52. You do your CONSTRAINING in Sketcher mode to create your part to exacting dimensions. This is the opposite of free-form creating we have.
DRAFTING TECHNIQUES I 136. Here is a basic shape. From here, we will do some advanced drafting once we put this shape on a sheet as a drawing. Select.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
Транксрипт:

Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig

Todays Topics Implementation hiding with packages and access specifiers. Composition Inheritance More on constructors Finals

Access Specifiers public, protected, private and friendly main() needs to be public, so the runtime system can call it. toString() needs to be public since it is public in Object, and we are overriding it.

The Need To Know Principle Portions of your classes are best kept private. The public interface of a class should provide everything users need, but nothing they dont. Access specifiers allow you to enforce the need to know principle.

Access Specifiers public members (variables and methods) are freely available for anyones use. private members can be accessed (used) only by the methods of the containing class. protected members are public to subclasses, private to others. Friendly members have package access: no access specifier public within the containing package

Packages A group of related classes. A package can have a name, but there is the unnamed package, which holds all the classes in your program that you havent put into a named package.

How Does It All Work? So far, if we havent used packages and access specifiers. Why has this worked? We kept all our.class files in the unnamed package. All of our members were therefore friendly. Only methods that are called from another package need access specifiers.

The Basic Rules Class members should be private unless there is a need to know or use. Think carefully about the public interface. Use accessors/mutators (aka get and set methods) to control access to private member variables. Often we create methods that are only used internally; make these private. Well worry about protected later.

Example public class Fraction { private int numerator; private int denominator; public Fraction() public Fraction(int n, int d) public Fraction add(Fraction f) private int gcd(int a, int b) }

How To Change A Fraction? This is a design decision. If we want users to change a Fraction objects values, provide a set function: public void set(int n, int d) { // test carefully for suitability, then: numerator = n; denominator = d; }

More on Packages Bringing in a package of classes: import java.util.*; Bringing in a single class: import java.util.ArrayList; The compiler can find these things, through the classpath. You might have to set your CLASSPATH so that the compiler and the JVM can find the.class files for your types.

Creating a Package The very first line in all the files intended for the package named myPackage: package myPackage; Put all of the.class files in a directory named myPackage. Put the myPackage directory, as a subdirectory, in a directory given in the classpath.

Class Access Classes can be public or not. Non-public classes are available only within the package they are defined in. There can be only one public class in a compilation unit (a.java file). Non-public classes are helper classes, not for public use.

Class Reuse A noble goal, and in Java its finally happening! Basically two ways: composition and inheritance. Composition is called has-a. Inheritance is called is-a.

Composition Weve done plenty of this already: The student Class has an Advisor The course has a teacher All you do is place a reference to a different kind of object in your class: Ta Da! Youre using composition.

Inheritance An object of a new class that inherits from an existing class has all the powers and abilities of the parent class: all data members all methods you can add additional data and methods if you wish a derived class object is-an object of the parent class type, so can be used in function calls where a parent-class object is specified

Inheritance Syntax class Cleanser { private String activeIngredient; public void dilute(int percent) {// water-down} public void apply(DirtyThing d) {// pour it on} public void scrub(Brush b) {// watch it work} } public class Detergent extends Cleanser { private String specialIngredient; public void scrub(Brush b) { // scrub gently, then super.scrub(b); // the usual way } public void foam() { // make bubbles} }

Access Control, Again Detergent does indeed have an activeIngredient, but its not accessible. If Detergent needs to access it, it must be either made protected (or friendly) in Cleanser, or be accessible through get and set methods in Cleanser. You cant inherit just to get access!

What Is A Detergent Object? An object of type Cleanser, having all the members of Cleanser. An object of type Detergent, having all the additional members of Detergent. An object that responds to messages (ummm…method calls) as though its a Cleanser, unless new methods have been added to Detergent, or Cleanser methods have been over-ridden.

Subclasses and Constructors Think of a Detergent object as containing a Cleanser sub-object. So, that sub-object has to be constructed when you create a Detergent object. The Cleanser object has to be created first, since constructing the remaining Detergent part might rely on it. Always call the base class constructor first.

Subclasses and Constructors class Cleanser { private String activeIngredient; Cleanser() { System.out.println(Cleanser constructor); } public class Detergent extends Cleanser { private String specialIngredient; Detergent() { System.out.println(Detergent constructor); } public static void main(String[] args) { Detergent d = new Detergent(); }

Subclasses and Constructors class Cleanser { private String activeIngredient; Cleanser(String active) { activeIngredient = active; } public class Detergent extends Cleanser { private String specialIngredient; Detergent(String active, String special) { super(active); // what if this isnt here? specialIngredient = special; }

protected Class Members public to subclasses and the same package. private to the outside world,