Chapter-7

Chapter-7 - Exercise 1 public class Tester cfw_ public...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 1 Exercise 1 public class Tester { public static void main( String[] args ) { int count; int j = 0; while( j <= 100) { System.out.println( j ); count++; } } } compile ?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 2 Exercise 2 import java.util.ArrayList; public class Foo { public static void main( String[] args ) { ArrayList<Integer> values = new ArrayList<Integer>(); values.add( new Integer(102) ); for( int v : values ) System.out.println( v ); } } values.add( new String("Ottawa") ); compile ??
Background image of page 2
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 3 Exercise 3 public class Tester { private int value; public Tester() { value = 0; } public static void test() { value = 6; } } compile ??
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 4 Inheritance What is inheritance with respect to  classes  ?  Visibility modifiers (  protected  modifier )  Inheritance and  Object  class  Shadowing Variables  Overriding methods Polymorphism
Background image of page 4
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 5 Class Relationships Three most common relationships: Dependency: A depends on (uses) B Aggregation: A has-a B Inheritance: A is-a B
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 6 Motivation for Inheritance Suppose you need to write class declarations for the  following entities:   Employee  Student
Background image of page 6
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 7 Class Declaration for Employee public class Employee { private String name; private int salary; public int getName() {…} public void setName(String n) {…} public int getSalary() {…} public void setSalary(int s) {…} } Employee +getName(): String +setName( n:String ): void +getSalary() : int +setSalary( s: int ) : void -name: String -salary: int
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 8 Class Declaration for Student public class Student { private String name; private int year; public int getName() {…} public void setName(String n) {…} public int getYear() {…} public void setYear(int y) {…} } Student +getName(): String +setName( n:String ): void +getYear() : int +setYear( y: int ) : void -name: String -year: int
Background image of page 8
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 9 Person -name: String +getName() : String +setName( s : String ) : void Employee +getSalary() : int +setSalary( s: int ) : void +getName(): String -salary: int Student +getYear() : int +setYear( y: int ) : void -year: int Creating Employee and Student classes from Person Abstracting out the common features from Employee and Student class and put them in a new class called Person. superclass subclasses overriding
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 10 What is inheritance with respect to classes ? Creating new classes from existing classes by   inheriting all the instance variables and instance  methods of the existing class   adding additional state and behavior in the new class Supports the idea of software reuse Creates an “is-a” relationship between classes
Background image of page 10
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 72

Chapter-7 - Exercise 1 public class Tester cfw_ public...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online