1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
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.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
This keyword.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
1 Inheritance and Subclasses Instructor: Mainak Chaudhuri
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
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.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Object Oriented programming Instructor: Dr. Essam H. Houssein.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
© 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.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 / 41 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 5 Programming Fundamentals using Java 1.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Chapter 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Abstract classes and interfaces
Interface.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 14 Abstract Classes and Interfaces
CSE 142 Lecture Notes Inheritance, Interfaces, and Polymorphism
Abstract classes and interfaces
Chapter 11 Inheritance and Polymorphism
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 13 Abstract Classes and Interfaces Part 01
Object-Oriented Programming: Classes, Inheritance, and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

1 Inheritance and Subclasses

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 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) { // copy constructor 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 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; } // 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 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 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

A protected data or method in a public can be accessed by any class in the same package or subclass in other package 20 modifierSame classSame package subclassDifferent package publicYYyy protectedyyy n (default)yynn privateynnn

Example package p1; public class C1{ protected int y; public int x; private int u; protected void m(){ } 21

public class C2{ } public class C3 extends C1{ } package p2; public class C4 extends C1{ } public class C5{ } 22

Preventing Extending and Overriding Use Keyword final public final class C{ } //no subclasses public class test{ public final void m(){ } 23

Abstract classes A superclass which can not have any specific instances can be declared abstract. Methods which can not be implemented in an abstract class use modifier abstract. 24

public abstract class Geometricobject{ private String color = “white”; private boolean filled; private java.util.Date dateCreated; protected Geometricobject(){ dateCreated=new java.util.Date(); } public String getColor(){ return color; } …. public abstract double getArea(); public abstract double getPerimeter(); } 25

public class TestGO{ public static void main(String[] args){ GeometricObject o1= new Circle(5); GeometricObject o2= new rect(5,3); System.out.println (“Equal area?”+ equalArea (o1,o2); displayGO(o1); displayGO(o2); } public static boolean equalArea (GeometricObject o1, GeometricObject o2){ return o1.getArea()==o2.getArea(); } public static void displayGO(GeometricObject o){ System.out.println (); System.out.println (“Area” + o.getArea()); System.out.println (“Perimeter” + o.getPerimeter()); } 26

27 Multiple inheritance Java does not allow inheritance from more than one base classes –At most one extension –However, one can “implement” multiple interfaces –Interface is a collection of abstract methods i.e. no method body is specified –A class implementing an interface must provide the suitable method bodies

28 Multiple inheritance interface RegularPolygon { public double Circumradius (); //abstract } class Equilateral extends Triangle implements RegularPolygon { public Equilateral (Point vertices[]) { super (vertices); } public Equilateral (double x[], double y[]) { super (x, y); } // next slide

29 Multiple inheritance public double Median () { return (0.5*vertices[0].Distance(vertices[1])*M ath.sqrt (3.0)); } public double Circumradius () { return (2*Median()/3); } } // end class // You can now print eqT.Circumradius() in // PolygonBuilder class.

Comparable interface package java.lang; public interface Comparable{ public int compareTo(Object o); } public class String extends Object implements Comparable{ } public class Date extends Object implements Comparable{ } Each of these have to define method compareTo in its body. 30