1 / 16

Agenda

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

kira
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review by groups • Polymorphism • Abstract vs Interface class • GridWorld exercises • Homework • Quiz on 12/6 Agenda

  2. 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

  3. 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

  4. 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(){..} }

  5. 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.

  6. 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.

  7. 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

  8. 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.

  9. Signature of interface public interface Parent { void method1(); void method2(); int method3(double d); }

  10. public class Child implements Parent { public void method1() { //some code..} public void method2() { //some code..} public int method3(double x) { //some code..} }

  11. 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

  12. more on the webaboutabstract vs interface • http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html • Barran’s Chapter 3 Inheritance and Polymorphisum p139.

  13. 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.

  14. 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

  15. 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

  16. Homework Prepare for the quiz

More Related