Chapter 13: Abstract Classes and Interfaces

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Cloneable interface

Use when it is desireable to create a copy of an object use clone method in this interface. A class that implements cloneable is marked cloneable and its objects an be cloned using clone() method defined in Object class public interface Cloneable{ } The interface is empty!

Abstract in UML

abstract class and method name is italicized

Marker Interface

an interface with an empty body -used to denote that a class possesses certain desireable properties

Completeness

Classes are designed for use by many different customers. In order to be useful in a wide range of applications, a class should provide a variety of ways for customization through properties and methods.

5 Important notes on abstraction

*Can define constructors of an abstract class (use protected data fields). *To create a CONCRETE class by extending the abstract class, you must override all abstract methods (Always use @Override annotation). *IF a subclass of an abstract superclass does not implement all abstract methods from super it MUST be declared abstract. *Abstract methods are non-static *A subclass CAN be abstract if its superclass is CONCRETE (Object-->GeometricObject)

Encapsulation

A class should use the private modifier to hide its data from direct access by clients. This makes the class easy to maintain. Provide a getter method only if you want the data field to be readable, and provide a setter method only if you want the data field to be updateable.

Interface

A class-like construct that contains only constants and abstract methods. When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract. syntax: public interface Vaccinatable{ public Date getVaccineDate(); public void setVaccineDate(Date vaccineDate); }

Why abstract methods and classes?

Allow easy changes to your code in the future. Leverages inheritance and polymorphism to support future changes.

Interface vs Abstract Classes

An interface is a contract. An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. Abstract classes, unlike interfaces, are classes. They are more expensive to use, because there is a look-up to do when you inherit from them. They have something more than interfaces; you can define a behavior for them.

Relationship to multiple inheritance

Java has single inheritance. A class can only extend one class but it can implement 0, 1, or many interfaces. So it gives your program some of the benefit of multiple inheritance. Using interfaces simulates multiple inheritance in a way because a class now adopts new methods from different interfaces.

How to use interfaces

*Can use as data type for reference variable *CANNOT create an instance of using "new" operator example: public interface Drawable{ public void draw(); } Drawable d = new Building(); d.draw(); //will call actual types draw() method

Consistency

*Choose informative names for classes, data fields, and methods. A popular style is to place the data declaration before the constructor and place constructors before methods. *You should consistently provide a public no-arg constructor for constructing a default instance. If a class does not support a no-arg constructor, document the reason. If no con- structors are defined explicitly, a public default no-arg constructor with an empty body is assumed.

Cohesion

A class should describe a single entity, and all the class operations should logically fit together to support a coherent purpose. You can use a class for students, for example, but you should not combine students and staff in the same class, because students and staff are different entities. A single entity with many responsibilities can be broken into several classes to separate the responsibilities.

Abstract Class

A superclass that is so abstract that it cannot be used to create any specific instances. Are like regular classes but you cannot create instances of abstract classes using the "new" operator.

Instance vs Static

A variable or method that is dependent on a specific instance of the class must be an instance variable or method. A variable that is shared by all the instances of a class should be declared static. *Always reference static variables and methods from a class name (rather than a reference variable) to improve readability and avoid errors. *A constructor is always instance, because it is used to create a specific instance. A static variable or method can be invoked from an instance method, but an instance variable or method cannot be invoked from a static method.

Why protected data fields in an abstract class?

Because it is only used by the subclasses. When you create an instance of a concrete subclass, its superclass' constructor is invoked to initialize data fields defined in the superclass.

"abstract" modifier

Class: public abstract class Pet{ Method: public abstract double getArea(); //does not have a body, just declaration

Class Design Guidelines

Cohesion Consistency Encapsulation Clarity Completeness Instance vs Static Inheritance vs Aggregation Interfaces vs Abstract classes

Clarity

Cohesion, consistency, and encapsulation are good guidelines for achieving design clarity. Additionally, a class should have a clear contract that is easy to explain and easy to understand. Users can incorporate classes in many different combinations, orders, and environments. Therefore, you should design a class that imposes no restrictions on how or when the user can use it, design the properties in a way that lets the user set them in any order and with any com- bination of values, and design methods that function independently of their order of occurrence. *You should not declare a data field that can be derived from other data fields. For exam- ple, the following Person class has two data fields: birthDate and age. Since age can be derived from birthDate, age should not be declared as a data field.

Shallow Copy (clone())

House house1 = new House(1, 1750.50); House house2 = (House)house1.clone(); meaning that if the field is of an object type, the object's reference is copied rather than its contents.

Abstract Methods

Is defined without implementation. It's implementation is provided by the subclasses. A class that contains abstract methods must be defined as abstract. i.e getArea() and getPerimeter() are abstract methods that depend on certain extensions of the GeometricObject class like Circle and Rectangle

Inheritance vs Aggregation

The difference between inheritance and aggregation is the difference between an is-a and a has-a relationship.

Deep Copy(clone())

To have the real content of an object copied over to the other object (and not a reference to that object of the object) perform clone on that specific object inside the object: @Override public Object clone() throws CloneNotSupportedException { // Perform a shallow copy House houseClone = (House)super.clone(); // Deep copy on whenBuilt houseClone.whenBuilt = (java.util.Date)(whenBuilt.clone()); return houseClone; }

Comparable Interface (MOST important interface in Java)

Whenever there is a total ordering of objects you should implement the comparable interface and provide compareTo in your class. <<interface>> Comparable<E> + compareTo(o: E): int Note: compareTo(o: E): int determines the order of this object with specified object o returns: NEGATIVE int -- if this Obj < o ZERO -- if this Obj = o POSITIVE int -- if this Obj > o

Interfaces vs Abstract classes (Relationship)

a strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes A weak is-a relationship, also known as an is-kind-of relationship, indicates that an object possesses a certain property. A weak is-a relationship can be modeled using interfaces.

Notation (Interfaces)

interface name and method names are italicized (because abstract) Dashed lines and hollow triangles used to point to the interface

"implements" keyword

public class Dog extends Pet implements Vaccinatable{ //interface inheritance

Abstract classes CANNOT be instantiated. However...

you CAN use abstract class as data type i.e. create an array whose elements are of abstract class GeometricObject GeometricObject[] obj = new GeometricObject[10]; //You can then create instance of GeometricObject and assign reference to the array obj[0] = new Circle();


Set pelajaran terkait

Art from China's Bronze Age and Cultural Innovations

View Set

Cyber 3100 Principles of Info Security Chapter 1

View Set

Nutrition exam 3 and final quizzes

View Set

Biomedical Technology 1 Final Review

View Set

The Unfinished Nation Chapter 14

View Set

Business Law - Chapter 7 LearnSmart

View Set

Insurance Chapter 4 Dwelling Policy

View Set

Term 3: Chapter 34 - Care of the Patient with a Psychiatric Disorder

View Set