cse115 introduction to computer science i
Download
Skip this Video
Download Presentation
CSE115: Introduction to Computer Science I

Loading in 2 Seconds...

play fullscreen
1 / 17

CSE115: Introduction to Computer Science I - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 [email protected] Announcements. Exam 3 on Wednesday 11/11 covers material from last exam up to and including Friday 11/06 Exam review in lecture on Monday 11/09

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CSE115: Introduction to Computer Science I' - benita


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cse115 introduction to computer science i

CSE115: Introduction to Computer Science I

Dr. Carl Alphonce

219 Bell Hall

Office hours: M-F 11:00-11:50

645-4739

[email protected]

announcements
Announcements
  • Exam 3 on Wednesday 11/11
    • covers material from last exam up to and including Friday 11/06
  • Exam review in lecture on Monday 11/09
    • study over the weekend, and bring questions to class on Monday
  • Sample questions on web-site over the weekend
agenda
Agenda
  • Inheritance – our last relationship!
    • we will only introduce inheritance today
    • we’ll continue after the exam
what s wrong with this code
What’s wrong with this code?

package pkg;

public class UTA implements TA {

private String _name;

public UTA(String name) {

_name = name;

}

public String job() {

return "teach recitations";

}

public String name() {

return _name;

}

}

package pkg;

public class GTA implements TA {

private String _name;

public GTA(String name) {

_name = name;

}

public String job() {

return "grade labs";

}

public String name() {

return _name;

}

}

code duplication a code smell
Code duplicationa “code smell”

package pkg;

public class UTA implements TA {

private String _name;

public UTA(String name) {

_name = name;

}

public String job() {

return "teach recitations";

}

public String name() {

return _name;

}

}

package pkg;

public class GTA implements TA {

private String _name;

public GTA(String name) {

_name = name;

}

public String job() {

return "grade labs";

}

public String name() {

return _name;

}

}

slide7
Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.

Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

inheritance
Inheritance
  • Inheritance is the last of the relationships we will study this semester.
  • It is a relationship between:
    • two classes
    • two interfaces
  • Inheritance is (syntactically) simple
  • Inheritance is (conceptually) messy
in code
In code:

public abstract class AbstractTA {…}

public class UTA extends AbstractTA{…}

abstract class
Abstract class
  • A class which mixes method specifications (abstract methods) with fully defined methods (concrete methods) is abstract.
  • An interface contains only abstract methods (they are labelled ‘abstract’ implicitly).
abstract class1
Abstract class
  • ‘abstract’ keyword in class header
  • cannot be instantiated
inheritance extends
Inheritance(“extends”)
  • Source class:
    • subclass
    • child class
    • derived class
  • Target class:
    • superclass
    • parent class
    • base class
implications of extends
Implications of “extends”
  • Same type implications as for interfaces:
    • instance of subclass belongs to subclass type and superclass type
  • inheritance: non-private members of superclass can be accessed via subclass object.
    • e.g. it’s as if methods of superclass were defined in subclass
slide15
[A] common duplication problem is when you have the same expression in two sibling subclasses. You can eliminate this duplication by using Extract Method (110) in both classes then Pull Up Method (322).

Refactoring: Improving the Design of Existing Code, Martin Fowler, page 76

code duplication a code smell1
Code duplicationa “code smell”

package pkg;

public class UTA implements TA {

private String _name;

public UTA(String name) {

_name = name;

}

public String job() {

return "teach recitations";

}

public String name() {

return _name;

}

}

package pkg;

public class GTA implements TA {

private String _name;

public GTA(String name) {

_name = name;

}

public String job() {

return "grade labs";

}

public String name() {

return _name;

}

}

refactored code a breath of fresh air
Refactored code(-: a breath of fresh air :-)

package pkg;

public class UTA extends AbstractTA {

public UTA(String name) {

super(name);

}

public String job() {

return "teach recitations";

}

}

package pkg;

public class GTA extends AbstractTA {

public GTA(String name) {

super(name);

}

public String job() {

return "grade labs";

}

}

package pkg;

public abstract class AbstractTA {

private String _name;

public AbstractTA(String name) {

_name = name;

}

public abstract String job();

public String name() {

return _name;

}

}

ad