160 likes | 303 Views
Review by groups Polymorphism Abstract vs Interface class GridWorld exercises Homework Quiz on 12/6. Agenda. Declaring subclass object. Student. is a. is a. UnderGrade. GradStudent. Student s = new UnderGrade(); Student s = new GradStudent(); GradStudent g = new Student(); // wrong
E N D
Review by groups • Polymorphism • Abstract vs Interface class • GridWorld exercises • Homework • Quiz on 12/6 Agenda
Declaring subclass object Student is a is a UnderGrade GradStudent Student s = new UnderGrade(); Student s = new GradStudent(); GradStudent g = new Student(); //wrong UnderGrade u = new Student(); //wrong
polymorphism • Polymorphism is the OOP(object oriented programming) property in which objects have the ability to assume different types. • Because a subclass is derived from a superclass, a superclass object can reference an object of the subclass Student s; UnderGrad u = new UnderGrad(); s = u; • Student object s assume the form of u, an object of underGrd, is said to be polymorphic
public class Student{ public Student() { … } public void computeGrade(){..} } public class UnderGrad extends Student{ public UnderGrad() { … } public void computeGrade(){..} } Public class GradStudent extends Student{ public GradStudent() { … } public void computeGrade(){..} }
polymorphism Student s = new Student(); Student g = new GradStudent(); Student u = new UnderGrad(); s.computeGrade(); g.computeGrade(); u.computeGrade(); A method that has been overridden in at least one subclass is said to be polymorphic. Although s, g, u all declared as type Student, each computeGrade() will perform the correct operations for their particular instances.
Abstract class • An abstract class models an abstract concept. For example, a musical instrument is an abstract concept. • An instrument is something that can be played, but there is no such thing an “instrument” instrument. There are however, flutes, drums, and cymbals. • Abstract classes cannot be instantiated because they should not represent objects. They instead describe the more general details and actions of an object.
Signature of abstract class • public abstract class Instrument{…..} • Abstract class is intended to be inherited. • Can contain an abstract method, signature public abstract String makeSound(); Note it doesn’t have a body and followed immediately by semicolon; • No object can be instantiated from abstract classes. • If a class includes abstract methods, the class itself must be declared abstract • Abstract methods must be implemented in subclasses
Interface • An interface is a class with method declarations that have NO implementations. (there is no method body). • An interface describes aspects of a class other than those that it inherits from its parent. • An interface is a list of constants and method declarations. It doesn’t have instance variables. • A class that implements an interface must implement each of the methods listed in the interface.
Signature of interface public interface Parent { void method1(); void method2(); int method3(double d); }
public class Child implements Parent { public void method1() { //some code..} public void method2() { //some code..} public int method3(double x) { //some code..} }
Interface • A method in an interface cannot be made private. All methods in an interface are both public and abstract by default. • The constants in an interface are public static final by default. • A class always extends just one parent but may implement several interfaces public class child extends parent implements Iboy, Igirl
more on the webaboutabstract vs interface • http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html • Barran’s Chapter 3 Inheritance and Polymorphisum p139.
Interface vs Abstract class • Use an abstract class for an object that is application-specific, but incomplete without its subclasses. • An Interface cannot provide implementations for any of its methods, whereas an abstract class can. • An interface cannot contain instance variables, whereas an abstract class can. • An interface and an abstract class can both declare constants. • It is not possible to create an instance of an interface object or an abstract class object.
Constants • There are several values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. • Variables which having final keyword
GridWorld Exercises • Write a CircleBug class • Write a SpiralBug that drops flowers in a spiral patterns. • Write a class Zbug that move in a “Z” pattern. • Design strategy: Bug starting direction how many flowers were left behind Bug turning angle
Homework Prepare for the quiz