{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter-7

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

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

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 ?

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

View Full Document
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 ??
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 ??

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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
Ask a homework question - tutors are online