CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
OOP: Inheritance By: Lamiaa Said.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE S. Tanimoto Java Classes and Inheritance 1 Java Classes and Inheritance Object (again): A computational unit consisting of some data elements.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
OOP Languages: Java vs C++
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance in the Java programming language J. W. Rider.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Chapter 10 Inheritance and Polymorphism
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Introduction to Defining Classes
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
1 More About Derived Classes and Inheritance Chapter 9.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.
Abstract classes and interfaces
Inheritance and Polymorphism
Object Oriented Analysis and Design
Abstract classes and interfaces
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Java Classes and Inheritance
Abstract classes and interfaces
Final and Abstract Classes
Presentation transcript:

CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance and interfaces: class TwoDimFig

CSE 341, S. Tanimoto Java brief review - 2 Java’s Strengths Supports the objected-oriented paradigm Permits client-side execution of programs (Applets) on web pages. Provides portability of applications, including a standard graphics platform Supports networking including communication, parallelism, security. Provides a large standard library. Syntax close to that of C++.

CSE 341, S. Tanimoto Java brief review - 3 Object-Oriented Programming Terms object: encapsulation of data and code. class: a formal category of objects. instance: an object, in the context of a particular class. message: a request to an object or class for data or service. sender: the object or class from which the message comes. receiver: the object or class to which the message is sent. method: a function within a class that responds to a message.

CSE 341, S. Tanimoto Java brief review - 4 Inheritance One class can serve as a base class for defining a new, more restricted class called a subclass. Any instance of a (derived) class inherits the data members and method members of its parent class. A subclass typically provides additional member variables and methods. A subclass may override a member inherited from its parent by redefining it. An abstract class doesn’t provide full implementations of all of its methods and is typically used as a means to provide a uniform protocol to two or more separate subclasses. The visibility of names within and across inheritance hierarchies is controlled by the keywords public, private, protected. The default access control is “package” which itself defaults to accessibility within compilation units.

CSE 341, S. Tanimoto Java brief review - 5 Interfaces Since Java does not permit multiple inheritance with subclassing, (and therefore might have made it difficult for one class to integrate the functionality of several others) it compensates by providing formal interfaces. An interface consists of a name and a protocol (list of functions and their formal parameter types). A class may subclass at most one superclass but may implement any number of interfaces. A class that implements an interface must provide an implementation for each function appearing in the interface. Interfaces do not provide any code and are used to enforce (at compile time) calling conventions within software applications. Interfaces can be extended.

CSE 341, S. Tanimoto Java brief review - 6 An Overview of the TwoDimFig example TwoDimFig is an abstract class Circle extends TwoDimFig Polygon extends TwoDimFig Triangle extends Polygon TwoDimFig implements SizeFunctions, Paintable TestApplet instantiates Circle, Polygon, and Triangle

CSE 341, S. Tanimoto Java brief review - 7 class TwoDimFig import java.awt.*; public abstract class TwoDimFig implements SizeFunctions, Paintable { protected Color c; public abstract String describe(); public abstract double area(); public abstract double volume(); public abstract int nVertices(); public abstract int nFaces(); public void setColor(Color c) { this.c = c; } public abstract void paint(Graphics g); }

CSE 341, S. Tanimoto Java brief review - 8 interface SizeFunctions interface SizeFunctions { public double area(); public double volume(); public int nVertices(); public int nFaces(); }

CSE 341, S. Tanimoto Java brief review - 9 interface Paintable import java.awt.*; interface Paintable { public void setColor(Color c); public void paint(Graphics g); }

CSE 341, S. Tanimoto Java brief review - 10 class Circle import java.awt.*; public class Circle extends TwoDimFig { protected double r; Circle(double r) { this.r = r; } public String describe() { return "a circle with radius " + r; } public int nVertices() { return 0; } public int nFaces() { return 1; } public double area() { return r * r * ; } public double volume() { return 0.0; } public void paint(Graphics g) { g.setColor(c); g.drawOval(0, 0, (int)r, (int)r); }

CSE 341, S. Tanimoto Java brief review - 11 class Polygon import java.awt.*; import java.util.*; public class Polygon extends TwoDimFig { protected Vector vertices; Polygon() {} Polygon(int n) { vertices = new Vector(); int R = 100; double ANGLE = 2.0 * / n; for(int i = 0; i < n; i++) { Point p = new Point((int)(R + R*(Math.cos(i * ANGLE))), (int)(R + R*(Math.sin(i * ANGLE)))); vertices.addElement(p); } public String describe() { return "a polygon with " + vertices.size() + " vertices"; }

CSE 341, S. Tanimoto Java brief review - 12 class Polygon (slide 2 of 3) public double area() { if (vertices.size() < 3) return 0.0; Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; double total = 0.0; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); total += areaUnderSegment(lastVertex, thisVertex); lastVertex = thisVertex; } total += areaUnderSegment(lastVertex, firstVertex); return total; } private double areaUnderSegment(Point p1, Point p2) { return (p1.x - p2.x) * (p1.y + p2.y) / 2; }

CSE 341, S. Tanimoto Java brief review - 13 class Polygon (slide 3 of 3) public double volume() { return 0.0; } public int nVertices() { return vertices.size(); } public int nFaces() { return 1; } public void paint(Graphics g) { g.setColor(c); Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); g.drawLine(lastVertex.x, lastVertex.y, thisVertex.x, thisVertex.y); lastVertex = thisVertex; } g.drawLine(lastVertex.x, lastVertex.y, firstVertex.x, firstVertex.y); }

CSE 341, S. Tanimoto Java brief review - 14 class Triangle (1 of 2) import java.awt.*; import java.util.*; public class Triangle extends Polygon { Triangle(Point p1, Point p2, Point p3) { vertices = new Vector(); vertices.addElement(p1); vertices.addElement(p2); vertices.addElement(p3); } public String describe() { if (isIsosceles()) return "an isosceles triangle"; else return "a triangle"; }

CSE 341, S. Tanimoto Java brief review - 15 class Triangle (2 of 2) public boolean isIsosceles() { Point p1 = (Point) vertices.elementAt(0); Point p2 = (Point) vertices.elementAt(1); Point p3 = (Point) vertices.elementAt(2); int d12 = dist(p1,p2); int d23 = dist(p2,p3); int d31 = dist(p3,p1); return ((d12 == d23) || (d23 == d31) || (d12 == d31)); } private int dist(Point p1, Point p2) { return (int)((double) Math.sqrt((double)(((p1.x - p2.x)*(p1.x - p2.x)) + ((p1.y - p2.y)*(p1.y - p2.y)))) ); }

CSE 341, S. Tanimoto Java brief review - 16 A Pre-Quiz Challenge What shows up on the applet panel? What shows up in the Java console? How should the TestApplet code be changed in order to have the triangle described as a polygon with 3 sides? What would a class ThreeDimFig look like that takes advantage of the same interfaces used by TwoDimFig ? What should it be a subclass of? Should it explicitly implement any interfaces?