Presentation is loading. Please wait.

Presentation is loading. Please wait.

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Similar presentations


Presentation on theme: "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."— Presentation transcript:

1 Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

2 Department of Computer Science2 Last Lecture - Basic Java Introduction (Chapter 1 of Weiss) - Reference types (Chapter 2 of Weiss) - Today - Objects and References (Chapter 3 of Weiss) - Inheritance (Chapter 4 of Weiss)

3 Department of Computer Science3 OO-Programming Objects are atomic units that have a structure and a state  Operations are defined on an object to access and change this state Information hiding means implementation details are hidden Encapsulation means data is grouped with operations

4 Department of Computer Science4 What is an object? A class is a blue-print for objects  Objects are instances of a class A class has fields and methods  These are called members of the class  Values of the fields define an object’s state  Methods are used to access and change state Public members are visible outside the class, private members are not

5 Department of Computer Science5 Basic Methods Constructors specify how an object is created and initialized  The default constructor initializes all fields with their default values Fields are usually private and manipulated with public accessor and mutator methods  Naming convention is get and set Static fields and methods exist once per class, unlike instance methods

6 Department of Computer Science6 Additional Constructs The special reference this refers to the address of the current object You can also use this in a constructor to call another constructor in the same class The instanceof operator is used to check whether a variable refers to an instance of a particular class You can use a static initializer to initialize static fields

7 Department of Computer Science7 Packages Larger programs are split into packages Import is used to import packages or individual classes in a package The package statement declares that a class belongs to a package  Packages must match directories in the file system Relevant packages must be in the classpath of the virtual machine

8 Department of Computer Science8 Inheritance Inheritance models IS-A relationship  Rather than HAS-A relationship A subclass extends a superclass A subclass can override a method  Method must have the same parameters  Return type must be same or subtype References to a superclass type are polymorphic

9 Department of Computer Science9 Continued IS-A relationship is transitive Protected members are visible in subclass Can use super to access superclass constructors (from subclass constructor) A class that is final cannot be extended A method that is final cannot be overriden Type of a reference variable defines what members we can access with the dot operator

10 Department of Computer Science10 Abstract methods and classes Abstract method does not provide any implementation (placeholder) A class with at least one abstract method must be declared abstract An interface is an abstract class that has only public abstract methods A class does not extend an interface, it implements it

11 Department of Computer Science11 Generic Programming Polymorphism enables us to write generic programs  Code where every reference is of type Object – extreme case Primitives are not objects Need to use wrapper classes

12 Department of Computer Science12 Continued Object has few methods Generic classes are parameterized types Class declaration includes one or more type parameters in angular brackets We can also have generic methods in any class


Download ppt "Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2."

Similar presentations


Ads by Google