Download presentation
Presentation is loading. Please wait.
1
Week 3 Recap CSE 115 – Spring 2007
2
Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are made up of: Constructor Header Constructor Body Starts with { and ends with }
3
Constructor Header Public identifier () public means as it did before. The identifier here is the name of the constructor. Java imposes that the name of a constructor is the same as the name of the class. () are called parameter list
4
Creating an object The syntax to create an object is: new ConstructorName(); new is a keyword that indicates that you will create a new instance of the class whose constructor is named after the keyword.
5
Instantiation Dependency Relationship Informally called “uses a” Represented in UML diagrams as a dotted arrow In code, if ClassA “uses a” ClassB: public class ClassA { public ClassA() { new ClassB(); }
6
Relationships Between Classes You should recognize the formal name, the informal name, the UML relationship arc, and the way the relationship looks in code.
7
Decoupling Separation of concerns Defining what an object can do, not how it does it
8
Interfaces One way to achieve decoupling in Java is through interfaces. An interface definition consists of an interface header and an interface body.
9
Interface Header public interface Name public: keyword – meaning the same as before interface: keyword – indicating the start of an interface definition Name: identifier – name of interface. Interface names by convention start off with a capital I, but then use same conventions as class names
10
Interface Body Enclosed in { } Contains what capabilities elements should have if they implement the interface.
11
Methods Formal specifications of capabilities inside our classes Method definitions consist of a method header and a method body
12
Method Definition (Method Header – Part 1) public type name() public: keyword – as before type: indication of the return type of the method – what information comes back from a method call? If no information comes back return type is void (another keyword)
13
Method Definition (Method Header – Part 2) public type name() name: identifier for method – method names follow conventions of class names, except the names begin with a lower case letter. (): called a formal parameter list – gives us a list of information that the method needs to finish its task.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.