Lecture 1:OOP and UML Class DiagramsOOP with Java
OOP and UML Class Diagrams OOP and Java
Object-Oriented Programming Object-Oriented Programming
Class Hierarchies
Superclass and subclass
Superclasses and subclasses
Pillars of Object-Oriented Programming Pillars of Object-Oriented Programming
- Abstraction
– Modelling attributes and behaviors of real objects, in specific contexts
Abstraction - modeling the properties and behavior of real objects, specifically
-
Encapsulation
– Hiding parts of an object’s states and behaviors from others, and exposing a limited set of interfaces
– public, private, and protected
– Interfaces and abstract classes
seal inside
- Hide parts of the object and state and behavior from others, and expose a limited set of interfaces
- Public, private and protected
- Interfaces and Abstract Classes -
Inheritance
– Main benefit: code reuse
predecessor
- Key benefit: code reuse -
Polymorphism
– Performing an action in many forms
– A mechanism for detecting the real class of an object and call its implementation
polymorphism
Multiple forms to perform an action
A mechanism for detecting the real class of an object and calling its implementation methods
OOP with Java: Declaring Classes and Creating Objects OOP with Java: Declaring Classes and Creating Objects
-
Class declaration
class declaration -
Creating objects
create an object- Declaration, instantiation, initialization
Declaration, instantiation, initialization
- The reference returned by the new operator does not have to be assigned to a variable
The reference returned by the new operator does not have to be assigned to a variable.
- Declaration, instantiation, initialization
OOP with Java: Access Control OOP with Java: Access Control
- At the top level
overhead
– public, or package-private (no explicit modifier)
Public or package private (no explicit modifier) - At the member level
At the membership level
– public, private, protected, or package-private (no explicit modifier)
public, private, protected or package-private (no explicit modifiers)
OOP with Java: Inheritance OOP in Java: Inheritance
-
Classes can be derived from other classes, inheriting fields and methods
Classes can be derived from other classes, inheriting fields and methods -
Definitions
define
– Subclass (derived class/extended class/child class)
– Superclass (base class/parent class)
- Subclasses (derived classes/extended classes/subclasses)
- Superclass (base class/parent class) -
Every class has one and only one direct superclass (single inheritance)
Each class has one and only one direct superclass (single inheritance)
– Excepting Object, which has no superclass
It has no superclasses except Object -
A subclass inherits all the members (fields, methods, and nested classes) from its superclass
Subclasses inherit all members of their superclasses (fields, methods, and nested classes)
OOP with Java: What You Can Do in a Subclass Java's OOP: What You Can Do in a Subclass
Use the inherited members as is, replace them, hide them, or supplement them
Use inherited members as is, replace them, hide them, or supplement them
- Declare a field in the subclass with the same name as the one in the superclass, thus hiding it (NOT recommended)
- Hide a field in a subclass by declaring it with the same name as the field in the superclass (not recommended) - Write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it
- Override it by writing a new instance method in the subclass with the same signature as the field in the superclass - Write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it
-Write a new static method in a subclass that has the same signature as the static method in the superclass, thus hiding it - Write a subclass constructor that invokes the constructor of the superclass
- Write a subclass constructor that calls the superclass constructor
How about private members in a superclass?
OOP with Java: Abstract and Final Methods/Classes OOP with Java: Abstract and Final Methods/Classes
- An abstract class is a class declared abstract: it may or may not include abstract methods
An abstract class is a class declared to be abstract: it may or may not contain abstract methods - An abstract method is a method declared without an implementation
Abstract methods are methods that are declared but not implemented - Final methods and classes
Final Methods and Classes
Methods called from constructors should generally be declared final
- Methods called from a constructor should normally be declared final.
OOP with Java: Interfaces OOP with Java: Interfaces
- Interfaces are contracts
The interface is the contract. - A reference type, containing only constants, method signatures,default methods, static methods, and nested types
Reference types, containing only constants, method signatures, default methods, static methods, and nested types - Cannot be instantiated
Cannot be instantiated
– They can only be implemented by classes or extended by other interfaces
- They can only be implemented by classes or extended by other interfaces - Consisting of modifiers, keyword, interface name, a comma-separated list of parent interfaces (if any), and the interface body
Consists of modifiers, keywords, the interface name, a comma-separated list of parent interfaces (if any), and the interface body - Interface body can contain abstract methods, default methods,and static methods
Interface bodies can contain abstract methods, default methods, and static methods
OOP with Java: Implementing and Using Interfaces OOP with Java: Implementing and Using Interfaces
- Include an implements clause in the class declaration
Include the implements clause in the class declaration
– Your class can implement more than one interface
- Your class can implement multiple interfaces - If you define a reference variable whose type is an interface,any object you assign to it must be an instance of a class that implements the interface
If a reference variable of type interface is defined, any object assigned to it must be an instance of a class that implements the interface
OOP with Java: Abstract Classes vs. Interfaces OOP with Java: Abstract Classes vs.
-
Consider using abstract classes when
Consider using abstract classes
– You want to share code among several closely related classes
- You want to share code between several closely related classes
– You expect that classes extending the abstract class have many common methods or fields, or require access modifiers other than public
- The class you want to extend the abstract class has many generic methods or fields, or requires access modifiers other than public
– You want to declare non-static or non-final fields
- You want to declare non-static or non-final fields -
Consider using interfaces when
Consider using interfaces
– You expect that unrelated classes would implement your interface
- You want unrelated classes to implement your interface.
– You want to specify the behavior of a particular data type, but not concerned about who implements its behavior
- You want to specify the behavior of a particular data type, but don't care who implements its behavior
– You want to take advantage of multiple inheritance
- You want to utilize multiple inheritance