1 Inheritance and Subclasses Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

Space and Shape. rectangle Shape ? trapezium.
Let’s review our shapes….
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Hosted by Mrs. Mitchell and Mrs. Johnson TrianglesPolygons QuadrilateralsLines
Inheritance Contents 1.Fundamentals 2.Generalization 3.Constructors and Derived Classes 4.Visibility Modifiers 5.Abstract Classes 6.Interfaces.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Overloading vs. Overriding b Don't confuse the concepts of overloading and overriding b Overloading deals with multiple methods in the same class with.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
CS 100Lecture 241 CS100J Lecture 24 n Previous Lecture –MatLab demonstration n This Lecture –Inheritance –Method overriding –Polymorphism –Reading: n Lewis.
Vocabulary Triangles and Quadrilaterals Congruency,
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
Intro to OOP with Java, C. Thomas Wu
1 Graphs and Search Trees Instructor: Mainak Chaudhuri
CS 2430 Day 9. Announcements Quiz on Friday, 9/28 Prog1: see , see me as soon as possible with questions/concerns Prog2: do not add any public methods.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Types of 2 D Shapes and There Properties 1)A shape with 3 sides is called a triangle 2)A shape with 4 sides is called a quadrilateral 3)The general term.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Inheritance, Abstract & Interface Pepper With help from and
1 Linear and Binary Search Instructor: Mainak Chaudhuri
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Copyright (c) 1998, 1999 D.L. Bailey * Winter 1999 Part 6 Reusing Classes: Inheritance and Composition.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Notes Over Classifying Polygons A polygon with three sides is a _____________. Classifying triangles by angles. Acute Triangle 3 acute angles Right.
Picturing Polygons Review. Definitions:  What is a polygon?  A. A shape with lines  B. A closed figure made up of at least 3 straight sides  C. A.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Quadrilaterals.
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.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
Review Creating Objects Pepper many references from rial/java/objects/object.html
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Parallel lines are always the same distance apart They go in the same direction They never meet.
 SAT Prep Course geometry & Measurement Day 3. Geometry Includes  Notation  Lines & Points  Angles  Triangles  Quadrilaterals  Area & perimeter.
When you get a question right, click a shell to reveal your score.
Lines, Rays & Angles, Oh My! We Are All Created Equal…Or Are We? We Have Many Sides! Measure This! When a Line Bends…a Shape Begins! We Come in 3’s! Just.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
PLOYGONS!! By:edilberto zeferino!!. Right Triangle Equilateral Triangle.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
Inheritance Notes: Using Object Oriented Design
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Section 11-7 Ratios of Areas.
CS100A Lecture 22 Previous Lecture This Lecture Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Polymorphism 11-Nov-18.
Overloading and Constructors
null, true, and false are also reserved.
Add up all the sides Perimeter of Area of a Rectangle: ANY polygon:
Inheritance in Java CS 3331 Fall 2009.
An Example of Inheritance
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

1 Inheritance and Subclasses Instructor: Mainak Chaudhuri

2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different types of quadrilaterals: squares, rectangles, parallelograms, etc. –Different types of animals: dogs, cows, etc. It seems wasteful to write these classes from scratch –These have many features in common Inheritance in Java allows you to extend a base class to another class with similar features

3 Inheritance Usually a class hierarchy is implemented through inheritance –Example: a polygon hierarchy The class that inherits from a base class becomes a subclass of the base class –The constructors are not inherited –Each subclass must offer its own constructor; however it is possible to access the constructors of the base class –All public members and methods are inherited in the subclass –The subclass may define additional members and methods; same methods override those in the base class

4 Polygon hierarchy public class Point { // to be used later private double x; private double y; public Point (double x, double y) { this.x = x; this.y = y; } public Point (Point p) { this.x = p.GetX(); this.y = p.GetY(); } // next slide

5 Polygon hierarchy public double GetX () { return x; } public double GetY () { return y; } public void SetX (double x) { this.x = x; } public void SetY (double y) { this.y = y; } // next slide

6 Polygon hierarchy public double Distance (Point p) { return Math.sqrt ((x-p.GetX())*(x- p.GetX()) + (y-p.GetY())*(y-p.GetY())); } } // end class

7 Polygon hierarchy public class Polygon { private int numVertices; public Point vertices[]; public Polygon (Point vertices[]) { int i; numVertices = vertices.length; this.vertices = new Point[numVertices]; for (i=0; i<numVertices; i++) { this.vertices[i] = new Point (vertices[i]); } } // next slide

8 Polygon hierarchy public Polygon (double x[], double y[]) { int i; numVertices = x.length; vertices = new Point[numVertices]; for (i=0; i<numVertices; i++) { vertices[i] = new Point (x[i], y[i]); } // next slide

9 Polygon hierarchy public double Perimeter () { int i; double perimeter = 0; // Assume that the vertices are in order for (i=0; i<numVertices-1; i++) { perimeter += vertices[i].Distance (vertices[i+1]); } perimeter += vertices[i].Distance (vertices[0]); System.out.println (“This is perimeter of Polygon class.”); return perimeter; } // next slide

10 Polygon hierarchy public boolean isRegular () { int i; double lastSide = vertices[0].Distance (vertices[1]); double thisSide; for (i=1; i<numVertices-1; i++) { thisSide = vertices[i].Distance (vertices[i+1]); if (lastSide != thisSide) return false; else lastSide = thisSide; } // next slide

11 Polygon hierarchy thisSide = vertices[i].Distance (vertices[0]); if (lastSide != thisSide) return false; return true; } // next slide

12 Polygon hierarchy public Point Centroid () { int i; double x = 0, y = 0; for (i=0; i<numVertices; i++) { x += vertices[i].GetX(); y += vertices[i].GetY(); } return (new Point (x/numVertices, y/numVertices)); } } // end class

13 Polygon hierarchy public class Triangle extends Polygon { private double a, b, c; // new members public Triangle (Point vertices[]) { super (vertices); // base class constr. // Other member initialization must // come after call to super a = vertices[0].Distance (vertices[1]); b = vertices[1].Distance (vertices[2]); c = vertices[2].Distance (vertices[0]); } // next slide

14 Polygon hierarchy public Triangle (double x[], double y[]) { super (x, y); a = vertices[0].Distance (vertices[1]); b = vertices[1].Distance (vertices[2]); c = vertices[2].Distance (vertices[0]); } // next slide

15 Polygon hierarchy // Add a new method public double Area () { double term1 = 0.5*Perimeter () – a; double term2 = 0.5*Perimeter () – b; double term3 = 0.5*Perimeter () – c; return (Math.sqrt (0.5*Perimeter () * term1 * term2 * term3)); } // next slide

16 Polygon hierarchy // One more new method public double[] Angles () { double angles[] = new double[3]; angles[0] = Math.asin (2*Area()/(b*c)); angles[1] = Math.asin (2*Area()/(c*a)); angles[2] = Math.asin (2*Area()/(a*b)); return angles; } // next slide

17 Polygon hierarchy // Override Perimeter with a simpler one public double Perimeter () { System.out.println (“This is perimeter of Triangle class.”); return (a+b+c); } } // end class // next slide

18 Polygon hierarchy public class Equilateral extends Triangle { public Equilateral (Point vertices[]) { super (vertices); } public Equilateral (double x[], double y[]) { super (x, y); } public double Median () { return (0.5*vertices[0].Distance(vertices[1])*Math.sq rt (3.0)); } } // end class

19 Polygon hierarchy class PolygonBuilder { public static void main (String a[]) { double x[] = {0, 0.5, -0.5}; double y[] = {0.5*Math.sqrt(3.0), 0, 0}; Equilateral eqT = new Equilateral (x, y); System.out.println (“Perimeter: ” + eqT.Perimeter()); System.out.println (“Area: ” + eqT.Area()); System.out.println (“Centroid: (” + eqT.Centroid().GetX() + “, ” + eqT.Centroid().GetY() + “)”); System.out.println (“Median: ” + eqT.Median()); } } // end class