A short tale about public and private - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

A short tale about public and private

Description:

class Chihuahua extends Dog. public Chihuahua() nSize = 12; public String ... Chihuahua TacoBell = new Chihuahua(); Benji.setLicense(1); TacoBell.setLicense(2) ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 42
Provided by: paulc1
Category:

less

Transcript and Presenter's Notes

Title: A short tale about public and private


1
A short tale aboutpublic and private
  • Let's say that you are part of a team of
    programmers working on the "Splatmaster 4"
    paintball computer game
  • Your job is to write a "blob" class (much like
    the particle class in Assignment 5) that
    represent the data and behavior of a paint blob
  • Other programmers will use your class in the game

2
public and private
  • Your first attempt is
  • class Blob
  • int nX, nY
  • double dSpeed, dAngle
  • Color myColor
  • void move()
  • nX (int)(Math.cos(dAngle) dSpeed)
  • nY (int)(Math.sin(dAngle) dSpeed)
  • void draw(Graphics g)
  • g.setColor(myColor)
  • g.fillOval(nX,nY,2,2)
  • //and lots more java

3
public and private
  • A few days later, you notice a problem. Some
    blobs aren't moving
  • nX (int)(Math.cos(dAngle) dSpeed)
  • nY (int)(Math.sin(dAngle) dSpeed)
  • When dSpeed is very small, nX or nY may not
    change (like nX (int)(.3))
  • You figure out that you need to change nX and nY
    to doubles dX and dY
  • No big deal right? Except, don't forget those
    other programmers who are already using your
    code. . .

4
public and private
  • Those other programmers have been "wrapping" the
    blobs when they go off the screen
  • Something like
  • Blob aBlob new Blob()
  • . . . .
  • if(aBlob.nX 640)
  • aBlob.nX 0
  • They are mad at you, because your mistake means
    they have to go back to their code and change all
    the nXs to dXs, as well as other changes to
    accommodate the different data type
  • They complain to your boss, and . . . . .

5
public and private
  • Wouldn't it be nice if you could make changes to
    your class without effecting the client
    programmers who use it?
  • You can, if you encapsulate your code using
    public and private
  • Using private to hide your data members is called
    data hiding
  • Hiding the details about how your class works is
    called encapsulation

6
public and private
  • Your first attempt should have been
  • class Blob
  • private int myX, myY
  • private double mySpeed, myAngle
  • private Color myColor
  • public void setX(int nX)
  • myX nX
  • public int getX()
  • return nX
  • //and lots more java

7
public and private
  • Those other programmers should have been
    "wrapped" the blobs with code like
  • Blob aBlob new Blob()
  • . . . .
  • if(aBlob.getX() 640)
  • aBlob.setX(0)
  • Changing the data types in the blob class doesn't
    require any change to the client program

8
public and private
  • Changing the data types does NOT change the
    public interface
  • class Blob
  • private double myX, myY
  • private double mySpeed, myAngle
  • private Color myColor
  • public void setX(int nX)
  • myX nX
  • public int getX()
  • return (int) myX
  • //and lots more java

9
interfaces
  • By hiding your data with private and forcing the
    other programmers to change the value of x with
    setX(), you can make changes without effecting
    the other programmers!
  • getX() still returns an int, so the client
    programmers dont notice any difference!
  • The public methods are called the interface of
    your class
  • The interface is what other programmers can use
    when they work with your class

10
defining an interface
  • All classes have an interface
  • If you define an interface, you write code like
  • interface Bogus
  • //list of public methods
  • Defining an interface allows you to make arrays
    and handles using the interface as a data type

11
Good style forpublic and private
  • private data members are often given the prefix
    "my"
  • private int myX, myY
  • private double mySpeed, myAngle
  • private Color myColor
  • Data members should be private unless they are
    final static

12
Good style forpublic and private
  • For most data members there should be "set" and
    "get" methods (sometimes called mutators and
    accessors)
  • public void setX(int nX)
  • myX nX
  • public void getX()
  • return myX
  • methods are usually public

13
Encapsulation
  • Encapsulation means hiding the details of a class
    by making the data private

14
Encapsulation
  • Encapsulation means hiding the details of a class
    by making the data private
  • Encapsulation makes it easier for teams of
    programmers to work together

15
Encapsulation
  • Encapsulation means hiding the details of a class
    by making the data private
  • Encapsulation makes it easier for teams of
    programmers to work together
  • Encapsulation is good style

16
Encapsulation
  • For example, you have a class with an int
  • class Example
  • int nNum
  • //lots more java

17
Encapsulation
  • The int should be private
  • class Example
  • private int myNum
  • //lots more java

18
Encapsulation
  • and it, if clients of the class need to access
    and modify the int, there should be accessor and
    mutator methods
  • class Example
  • private int myNum
  • public int getNum()
  • return myNum
  • public void setNum(int nNew)
  • myNum nNew
  • //lots more java

19
Encapsulation
  • if you change your mind and decide the int should
    be a double, it won't necessarily affect the
    clients
  • class Example
  • private double myNum
  • public int getNum()
  • return (int) myNum
  • public void setNum(int nNew)
  • myNum nNew
  • //lots more java

20
inheritance
  • One of the key ideas in OOP is inheritance
  • It allows programmers to quickly build new
    classes that are similar to other classes
  • inheritance is similar to implementing an
    interface, but even more powerful
  • To use inheritance, you use the keyword extends

21
extends inheritance
  • class Wolf
  • protected int nLegs
  • protected int nSize
  • public Wolf()
  • nLegs 4
  • nSize 150
  • public int getLegs()
  • return nLegs
  • public int getSize()
  • return nSize
  • public String speak()

22
extends inheritance
  • Once we have a class that is useful, it is easy
    to make variations on that class using extends
  • Let's say we wanted to make a Dog class
  • class Dog extends Wolf
  • All dogs are a special type of wolf
  • The Dog class is a derived class or sub class of
    the Wolf class
  • The Wolf class is the base class or super class
    of the Dog class

23
extends inheritance
  • class Dog extends Wolf
  • //added member variable
  • protected int myLicense
  • public Dog()
  • nSize 50
  • //override speak()
  • public String speak()
  • return "Bark!"
  • //added methods
  • public void setLicense(int nNumber)
  • myLicense nNumber
  • public int getLicense()

24
The "is a" relationship
  • The relationship ship between a super class and a
    sub class is sometimes called a "is a"
    relationship
  • A Dog "is a" Wolf (because all dogs are wolves)
  • A Wolf isn't necessarily a dog (because not all
    wolves are dogs)

25
protected
  • private means it is only available in that class
  • protected means it is only available in that
    class, and the derived (sub) classes of that
    class
  • If you want the classes that extend a super class
    to have access to a member, it should be
    protected
  • class Wolf
  • protected int nLegs
  • protected int nSize
  • . . . . .

26
extends inheritance
  • Wolf BigBad new Wolf()
  • Dog Benji new Dog()
  • System.out.println(BigBad.speak())
  • System.out.println(Benji.speak())
  • System.out.println(Benji.getLegs())
  • System.out.println(Benji.getSize())
  • / Sample Output
  • Howl!
  • Bark!
  • 4
  • 50/

27
inheritance polymorphism
  • polymorphism means same name, different meanings
  • speak() is an example of polymorphism
  • Java matches the different versions of the
    speak() method with the type of object

28
constructors and inheritance
  • Look carefully at this code
  • public Wolf()
  • nLegs 4
  • nSize 150
  • public Dog()
  • nSize 50
  • Dog Benji new Dog()
  • System.out.println(Benji.getLegs())
  • //displayed 4
  • How did Java figure out that Benji had 4 legs?

29
constructors and inheritance
  • The constructor in the sub class automatically
    makes an invisible call to the default
    constructor of the super class!
  • public Wolf()
  • nLegs 4
  • nSize 150
  • public Dog()
  • Wolf() invisible call to constructor
  • nSize 50
  • Dog Benji new Dog()
  • System.out.println(Benji.getLegs())
  • //displayed 4

30
constructors and inheritance
  • Good Style Always write a default (no argument)
    constructor for a base class
  • Note a base class is a (super) class that will
    be extended to make derived (sub) classes

31
extends inheritance
  • You can extend a class that already extends
    another class (think grandchild)
  • class Chihuahua extends Dog
  • public Chihuahua()
  • nSize 12
  • public String speak()
  • return "Yap!"

32
The difference between extends and implements
  • Both inheritance (extends) and interfaces
    (implements) allow you to build new classes
    quickly, using other classes as a starting point
  • Inheritance means the new class gets all of the
    methods and data members of the super class
    (except constructors)
  • With an interface, the classes share only those
    methods that are listedand no data

33
The difference between extends and implements
  • Inheritance means the new class is fundamentally
    the same as super class
  • With an interface, the classes only share a few
    methods

34
The difference between extends and implements
  • An analogy
  • I (Mr. Simon) extend the Simon class I inherited
    all the data (hair, eye color, etc) and methods
    (breathe, sleep, eat, etc) of my parents
  • I implement the Teacher interface I have teach
    and grade methods
  • I also implement the Motorcyclist interface I
    have a ride method

35
The difference between extends and implements
  • Inheritance is more powerful, but you can only
    use it once a class cannot extend two different
    classes
  • With an interface, you can implement as many
    interfaces as you want however, you can not
    make an instance of an inteface
  • new Simon() //OK
  • new Motorcyclist() //NO!
  • new Teacher()//NO!

36
What is the output of this code?
  • Wolf BigBad new Wolf()
  • Dog Benji new Dog()
  • Chihuahua TacoBell new Chihuahua()
  • Benji.setLicense(1)
  • TacoBell.setLicense(2)
  • System.out.println(BigBad.speak())
  • System.out.println(Benji.speak())
  • System.out.println(Benji.getSize())
  • System.out.println(Benji.getLicense())
  • System.out.println(TacoBell.speak())
  • System.out.println(TacoBell.getLegs())
  • System.out.println(TacoBell.getSize())
  • System.out.println(TacoBell.getLicense())

37
Overriding inherited methods
  • The derived (sub) class inherits all of the
    methods (except constructors) from the base
    (super) class
  • So, what do you do if you don't like the
    inherited method?
  • Or in other words, what if you don't want your
    Chihuahuas to howl like a wolf?
  • Overriding means writing a new version of the
    method in the derived class.

38
polymorphism dynamic binding
  • "Same names, different meanings"
  • working with multiple classes that inherit from
    the same base (parent) class.
  • All derived (child) classes have the same
    interface.
  • Allows us to treat the derived class as if it
    were an instance of the base class, but we dont
    lose touch with its true class.
  • One piece of code can be written which deals with
    the base class, and still works with all the
    derived classes.

39
What is the output of this code?
  • Wolf Pack new Wolf3
  • Pack0 new Wolf()
  • Pack1 new Dog()
  • Pack2 new Chihuahua()
  • for(int nI 0 nI
  • System.out.println(PacknI.speak())

40
What is the output of this code?
  • Wolf Pack new Wolf3
  • Pack0 new Wolf()
  • Pack1 new Dog()
  • Pack2 new Chihuahua()
  • for(int nI 0 nI
  • System.out.println(PacknI.speak())
  • /Sample Output
  • Howl!
  • Bark!
  • Yap!/
  • This is an example of dynamic (run-time) binding
  • Java "binds" the correct method with each of the
    Wolf objects when the loops runs
  • This is also an example of polymorphism

41
Practice Quiz Question what is the output
  • public class SubSuperPractice extends Applet
  • public void paint(Graphics g)
  • SuperClass joe new SuperClass()
  • SuperClass bob new SuperClass(9)
  • SubClass alex new SubClass()
  • class SuperClass
  • protected int myNum
  • public SuperClass()
  • myNum 0
  • System.out.println("Constructed with "
    myNum)
  • public SuperClass(int nNum)
  • myNum nNum
  • System.out.println("Constructed with "
    myNum)
  • class SubClass extends SuperClass
Write a Comment
User Comments (0)
About PowerShow.com