Abstract Classes. Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible.

Slides:



Advertisements
Similar presentations
Java Programming Abstract classes and Interfaces.
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Circle – Formulas Radius of the circle is generally denoted by letter R. Diameter of the circle D = 2 × R Circumference of the circle C = 2 ×  × R (
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Unit 021 Abstract Classes What is an Abstract Class? Properties of an Abstract Class Discovering Abstract Classes.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
1 Further OO Concepts (Part I) Further OO Concepts I - Polymorphism Overview l Polymorphism is Wonderful. l Usefulness of Up casting? l Method call binding?
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
GEOMETRY Today we are going to learn about some geometric shapes...
Objective: find the area of geometric figures and shaded regions. What area formulas must be memorized?
The distance around the outside of a shape.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
$100 Area of Parallelograms Area of Triangles Perimeter And Area Area of Trapezoids Area of Compound Figures & Area and Circumference of Circles $200.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CPS Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5.
Perimeter & Area Lessons 19 & 20.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
5 CM 4 CM Calculation Area = Length times Width (lw or l x W) Note Length of a rectangle is always the longest side.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritence Put classes into a hierarchy derive a new class based on an existing class with modifications or extensions. Avoiding duplication and redundancy.
Areas and Perimeter of Rectangles, Square, Triangles and Circles
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Perimeter of different shapes. *Engage* We take some different shapes How to measure their boundary.
Area & Perimeter. SHAPE OVERVIEW Rectangle Triangle Hexagon Trapezoid Square Parallelogram Pentagon Circle.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Distribute and Combine Like Terms Applications. What is the area of the following shape? 5 2x-3 1.
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
To find the perimeter of a rectangle, just add up all the lengths of the sides: Perimeter = L + w + L + w         = 2L + 2w To find the area of a rectangle,
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
Perimeter is the distance around an object. If you started at one point and went all the way around until you got to where you started, the perimeter.
Area & Perimeter An Introduction. AREA The amount of space inside a 2-dimensional object. Measured in square units cm 2, m 2, mm 2 Example: 1 cm 2 cm.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Toddrick’s Shape Scrapbook By:Toddrick Newton. The perimeter, P, of a rectangle is given by the formula P = 2(l + w) where l is the length width of the.
Object-Oriented Programming: Polymorphism Chapter 10.
Perimeter, Circumference and Area. Perimeter and Circumference Perimeter : The distance around a geometric figure. Circumference: The distance around.
Perimeter and Area Formulas.  Perimeter is the distance around an object. It is easily the simplest formula. Simply add up all the sides of the shape,
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Abstract Classes 1. Objectives You will be able to: Say what an abstract class is. Define and use abstract classes. 2.
Perimeter & Area. Today’s Objectives:  Learn what it means to find perimeter and area.  Practice finding or estimating the perimeter and area.
9.1 PERIMETER AND AREA OF PARALLELOGRAMS Objective: Students find the perimeter and area of parallelograms.
1 More About Derived Classes and Inheritance Chapter 9.
Q. What do we mean by finding the perimeter of a shape? Q. What strategies might you use for calculating the perimeter of a rectangle?
Geometry/Trig 2 Name __________________________
8th Grade Math Unit 8 Review
LENGTH and PERIMETER.
Advanced Programming in Java
Area of Shapes.
CS250 Introduction to Computer Science II
UNIT 8: 2-D MEASUREMENTS PERIMETER AREA SQUARE RECTANGLE PARALLELOGRAM
Class vs Abstract vs Interface
Interfaces EE 422C.
More inheritance, Abstract Classes and Interfaces
What shape am I? I am a plane shape. I have 4 sides.
Polymorphism Phil Tayco San Jose City College Slide version 1.1
Week 6 Object-Oriented Programming (2): Polymorphism
Shapes.
Chapter 14 Abstract Classes and Interfaces
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Area & Perimeter.
By- Sabrina,Julianna, and Killian
Introduction to Methods and Interfaces
Presentation transcript:

Abstract Classes

Recall We have the ability to create hierarchies of classes. Good design practice suggests that we move common methods as high as possible in the “tree” Why? Minimize code duplication Minimize maintenance efforts Good abstraction Example?

But What if we know that every subclass will have a certain method but the methods will all be different??? What then? Example: We are designing a calculator that will have as a feature the ability to make calculations involving geometric shapes We want it to handle rectangle, squares, triangles, equilateral triangles, ellipses and circles Each shape should know how to calculate its area and perimeter

Shapes Shape EllipseRectangleTriangle getArea getPerim major minor Circle radius altitude base Equilateral Triangle side length width Square side

Need What we would like is the ability to say in the Shape class that all Shapes will have getArea and getPerimeter methods. We obviously don’t know what they will be when all we know is that something is a shape but... The basic idea is for the compiler to provide as a service the ability to “force” all subclasses of shape to have a getArea and getPerimeter method The underlying philosophy is simple: Catch errors when compiling the program not when running the program

Abstract Methods We have the ability to define a method with no method body. We are required to label this method as abstract Once a class contains an abstract method it too must be labelled abstract Every subclass will too be abstract until a subclass is written in which every abstract method has been defined Abstract classes cannot be used to make Objects!!! But, they can be used to make references!?!?!?

Shape // Comments omitted... abstract class Shape { private String name; public Shape(String name) { setname(name); } public void setName(String name) { this.name = name; } public String getName() { return name; } public abstract double getArea(); public abstract double getPerim(); }

Rectangle class Rectangle extends Shape { private double ln; private double wd; public Rectangle(String name, double ln, double wd){ super(name); this.ln = ln; this.wd = wd; } public double getArea() { return ln * wd; } public double getPerim() { return 2.0 * (ln + wd); }

In case you’re wondering... Shape ThingsWithStraightSides TriangleRectangle If Shape is abstract and ThingsWithStraightSides doesn’t define getArea then it must also be abstract.