Top Banner
Agenda About Homework for BPJ lesson 36 About practice of last class Review –superclass vs subclass • Encapsulation • Homework
26

Agenda

Jan 12, 2016

Download

Documents

Azizuddin Khan

About Homework for BPJ lesson 36 About practice of last class Review –superclass vs subclass Encapsulation Homework Quiz on 12/6. Agenda. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Agenda

Agenda

• About Homework for BPJ lesson 36• About practice of last class• Review –superclass vs subclass• Encapsulation• Homework• Quiz on 12/6

Page 2: Agenda

public class Red extends Green{

public int blue(double x){...}public String s;private int i;

}

public class Green{

public double peabody(double y){return mm;}private boolean crunch( ){...}private double mm;public long xx;

}

Page 3: Agenda

Long data type

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

super.peabody(11);

Page 4: Agenda

Is this legal? Red myObj = new Red();boolean bb = myObj.Crunch();

Is this legal? the two above)Red myObj = new Red( );int bb = myObj.blue(105.2);

Page 5: Agenda

Write code for the blue method that will printout the mm state variable.

public int blue(double x){ double mmValue =

super.peabody(x); System.out.println(mmValue);}

Page 6: Agenda

public int blue(double x){ double xxValue = super.xx; System.out.println(xxValue);

return 0;}

Write code for the blue method that will printout the xx state variable.

Page 7: Agenda

this keyword• Within an instance method or a constructor,

this is a reference to the current object.public class Point { public int x = 0;

public int y = 0; public Point(int a, int b) { x = a; y = b; } }

public class Point {public int x = 0; public int y = 0; public Point(int x, int y) { this.x = x; this.y = y; } }

Page 8: Agenda

Using this with a Constructorpublic class Rectangle {

private int x, y; private int width, height;public Rectangle() { this(0, 0, 0, 0); } public Rectangle(int width, int height) {

this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x;

this.y = y; this.width = width; this.height = height; } ... }

Page 9: Agenda

Encapsulation• Protecting an object’s data from code

outside the class.• Those instance variables marked as private.• They can only be seen or modified through

the use of public accessor and mutator methods.

• Encapsulation = information hiding

Page 10: Agenda

public class Actor{ private Grid<Actor> grid; private Location location; private int direction; private Color color;public Actor() { color = Color.BLUE; direction = Location.NORTH; grid = null; location = null; }public Color getColor() { return color; }

Page 11: Agenda

public class Flower extends Actor{ private static final Color DEFAULT_COLOR = Color.PINK; private static final double DARKENING_FACTOR = 0.05;public Flower() { setColor(DEFAULT_COLOR); }public Flower(Color initialColor) { setColor(initialColor); }public void act(){….}

Page 12: Agenda

public class Cube{private int length;public int breadth;private int height;

public Cube(int l, int b, int h) {length = l;breadth = b;height = h; }

public int getVolume() {return (length * breadth * height); }

public int getBreadth(){return breadth; }

public int getHeight(){return height; }

}

Page 13: Agenda

Golden nuggets of wisdom

• Private state variables and methods are not accessible from outside the class. • But private things can only be

accessed from within the class itself.

Page 14: Agenda

How to compare objects?• Circle cir1 = new Circle(3.0);• Circle cir2 = new Circle(3.0);• cir1 == cir2 ??????

• Circle cir3;• cir3 = cir1;• cir1 == cir3 ??????

Page 15: Agenda

• String str1 = “Hello”;• String str2 = “Hello”;• str 1 == str2 ????

• String str3 = new String(“Hello”);• str3 == str1?????

Page 16: Agenda

Inheritance

• Inheritance enables you to define a new class based upon an existing class. The new class is similar to the existing class, but has additional member variables and methods.

• This makes programming easier because you can build upon an existing class instead of starting out from scratch.

Page 17: Agenda

Is-a relationship

In diagrams that show inheritance, an arrow points from the new class to the class it is based upon. The arrow is sometimes labeled "is a".

Page 18: Agenda

The class that is used to define a new class is called a parent class (or superclass or base class.) The class based on the parent class is called a child class (or subclass or derived class.)

subclass

superclass

Page 19: Agenda

Inheritance example

Subclasses need all the methods and state variables of the superclass - BankAccount

BankAccount

SavingsAccount checkingAccount

Page 20: Agenda

BankAccount classpublic class BankAccount{

private double balance;public BankAccount(double amt){

balance = amt; }public void deposit(double amt){

balance += amt; }private void withdraw(double amt){

balance -= amt;}

Page 21: Agenda

public class SavingsAccount extends BankAccount{private double interestRate;public SavingsAccount(double amt, double rate){

super(amt);interestRate = rate; }

Public void addInterest(){ double interest = getBalance()* interestRate/100; deposit(interest); }

}

Page 22: Agenda

1. Private members of the superclass are not inherited by the subclass and can only be indirectly accessed.2. Members that have default accessibility in the superclass are also not inherited by subclasses in other packages, as these members are only accessible by their simple names in subclasses within the same package as the superclass.3. Since constructors and initializer blocks are not members of a class, they are not inherited by a subclass.4. A subclass can extend only one superclass

Page 23: Agenda

GridWorld

Actor

BugRockFlower

Page 24: Agenda

Bug class

• If you don’t have the import statement for ex. Bug class, you have to write the whole path in order to use the class like

• Bug oBug = new info.gridworld.actor.Bug();instead of

Bug oBug = new Bug();

Page 25: Agenda

Abstract class• An abstract class models an abstract

concept. For example, a musical instrument is an abstract concept.

• An instrument is something that can be played, but there is no such thing an “instrument” instrument. There are however, flutes, drums, and cymbals.

• Abstract classes cannot be instantiated because they should not represent objects. They instead describe the more general details and actions of an object.

Page 26: Agenda

Homework

Based on the BankAccount and SavingsAccount classes on this slide and answer all the questions in the word document homework-nov26 posted on the web