Encapsulation and Interface Types --- The Beautiful of OOP By Penghuan Ni

advertisement
Encapsulation and Interface Types
--- The Beautiful of OOP
By Penghuan Ni
Object-Oriented Programming
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
Encapsulation (aka. Data Hiding)
Encapsulation is one of the four fundamental OOP concepts. The other three are
inheritance, polymorphism, and abstraction.
How To...
1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify and view the variables values.
Accessors and Mutators
Constructor Methods: A new instance of an object is created by calling a constructor
method. Values can be passed to a constructor method to set the initial state of an object.
Mutator Methods: For every private field we can create a public method that will return
its value.
Accessor Methods: For every private field we can create a public method that will set its
value. If you want a private field to be read only do not create a mutator method for it.
java.util.Date Class & Day Class
Examples:
1. Date(int year, int month, int date)
2. getMonth()
3. setHours(int hours)
Benefits of Encapsulation
1. The fields of a class can be made read-only or write-only.
2. A class can have total control over what is stored in its fields.
3. The users of a class do not know how the class stores its data. A class can change the
data-type of a field and users of the class do not need to change any of their code.
Let’s Take a Look at Matrix Class (Lab3)
Interface
An interface declaration can contain method signatures, default methods, static methods
and constant definitions. The only methods that have implementations are default and
static methods.
A class that implements an interface must implement all the methods declared in the
interface.
An interface name can be used anywhere a type can be used.
Same Between Interface and Class
An interface can contain any number of methods.
An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
The byte code of an interface appears in a .class file.
Interfaces appear in packages, and their corresponding bytecode file must be in a directory
structure that matches the package name.
Difference Between Interface and Class
You cannot instantiate an interface.
An interface does not contain any constructors.
All of the methods in an interface are abstract.
An interface cannot contain instance fields. The only fields that can appear in an interface
must be declared both static and final.
An interface is not extended by a class; it is implemented by a class.
An interface can extend multiple interfaces.
Keywords for Signature
Keywords for declaring interface: interface
Keyword for implementing interface: implements
Keyword for extending interface: extends
A Java class can only extend one parent class. Multiple inheritance is not allowed.
Interfaces are not classes, however, and an interface can extend more than one parent
interface.
Inheritance
Definitions: A class that is derived from another class is called a subclass (also a derived
class, extended class, or child class). The class from which the subclass is derived is called
a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct
superclass (single inheritance). In the absence of any other explicit superclass, every class
is implicitly a subclass of Object.
Five Types of Inheritance
Five Types of Inheritance
Con’t
How to model interface implementation in a UML class diagram
The Comparable interface in java.lang
The Comparator interface in java.util
The Icon interface in javax.swing
Download