Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
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.
Java Programming Abstract classes and Interfaces.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Public class ABC { private int information = 0; private char moreInformation = ‘ ‘; public ABC ( int newInfo, char moreNewInfo) { } public ABC () {} public.
Exercise 1 Suppose C is a class that implements interfaces I and J. Which of the following Requires a type cast? C c = ……? I i = …..? J j = …..? c =
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
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,
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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.
UML Basics & Access Modifier
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 Inheritance and Subclasses Instructor: Mainak Chaudhuri
Intro to OOP with Java, C. Thomas Wu
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Topic 4 Inheritance.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
L EC. 07: I NHERITANCE S PRING C ONTENT  Inheritance basics  Member access and inheritance  Constructors and inheritance  Superclass references.
C++ Programming: chapter 7 – inheritence 2014, Spring Pusan National University Ki-Joune Li 1.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
A: A: double “4” A: “34” 4.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
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.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
1 More About Derived Classes and Inheritance Chapter 9.
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Inheritance class TwoDShape { private double width;
Java Inheritance.
Refactoring Methods: Kevin Murphy.
C# - Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Web Design & Development Lecture 5
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Overloading and Constructors
Inheritance.
Inheritance CT1513.
Constructors under inheritance Variable Shadowing
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism
An Example of Inheritance
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Polymorphism Part 1
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width and height are " + width + " and " + height); }

Inheritance // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { String style; double area() { return width * height / 2; } void showStyle() { System.out.println("Triangle is " + style); }

Inheritance class Shapes { public static void main(String args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.width = 4.0; t1.height = 4.0; t1.style = "isosceles"; t2.width = 8.0; t2.height = 12.0; t2.style = "right"; System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println(); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); }

Inheritance // A subclass of TwoDShape for rectangles. class Rectangle extends TwoDShape { boolean isSquare() { if(width == height) return true; return false; } double area() { return width * height; }

Inheritance // Private members are not inherited. // This example will not compile. // A class for two-dimensional objects. class TwoDShape { private double width; // these are private double height; // now private void showDim() { System.out.println("Width and height are " + width + " and " + height); } // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { String style; double area() { return width * height / 2; // Error! can't access } void showStyle() { System.out.println("Triangle is " + style); }

Inheritance // Use accessor methods to set and get private members. // A class for two-dimensional objects. class TwoDShape { private double width; // these are private double height; // now private // Accessor methods for width and height. double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } void showDim() { System.out.println("Width and height are " + width + " and " + height); }

Inheritance // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { String style; double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); }

Inheritance class Shapes2 { public static void main(String args[]) { Triangle t1 = new Triangle(); Triangle t2 = new Triangle(); t1.setWidth(4.0); t1.setHeight(4.0); t1.style = "isosceles"; t2.setWidth(8.0); t2.setHeight(12.0); t2.style = "right"; System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println(); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); }

Inheritance // Add a constructor to Triangle. // A class for two-dimensional objects. class TwoDShape { private double width; // these are private double height; // now private // Accessor methods for width and height. double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } void showDim() { System.out.println("Width and height are " + width + " and " + height); }

Inheritance // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { private String style; // Constructor Triangle(String s, double w, double h) { setWidth(w); setHeight(h); style = s; } double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); }

Inheritance class Shapes3 { public static void main(String args[]) { Triangle t1 = new Triangle("isosceles", 4.0, 4.0); Triangle t2 = new Triangle("right", 8.0, 12.0); System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println(); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); }

Inheritance // Add constructors to TwoDShape. class TwoDShape { private double width; private double height; // Parameterized constructor. TwoDShape(double w, double h) { width = w; height = h; }

Inheritance // Accessor methods for width and height. double getWidth() { return width; } double getHeight() { return height; } void setWidth(double w) { width = w; } void setHeight(double h) { height = h; } void showDim() { System.out.println("Width and height are " + width + " and " + height); }

Inheritance // A subclass of TwoDShape for triangles. class Triangle extends TwoDShape { private String style; Triangle(String s, double w, double h) { super(w, h); // call superclass constructor style = s; } double area() { return getWidth() * getHeight() / 2; } void showStyle() { System.out.println("Triangle is " + style); }

Inheritance class Shapes4 { public static void main(String args[]) { Triangle t1 = new Triangle("isosceles", 4.0, 4.0); Triangle t2 = new Triangle("right", 8.0, 12.0); System.out.println("Info for t1: "); t1.showStyle(); t1.showDim(); System.out.println("Area is " + t1.area()); System.out.println(); System.out.println("Info for t2: "); t2.showStyle(); t2.showDim(); System.out.println("Area is " + t2.area()); }

exception // Use a custom exception. // Create an exception. class NonIntResultException extends Exception { int n; int d; NonIntResultException(int i, int j) { n = i; d = j; } public String toString() { return "Result of " + n + " / " + d + " is non-integer."; }

exception class CustomExceptDemo { public static void main(String args[]) { // Here, numer contains some odd values. int numer[] = { 4, 8, 15, 32, 64, 127, 256, 512 }; int denom[] = { 2, 0, 4, 4, 0, 8 }; for(int i=0; i<numer.length; i++) { try { if((numer[i]%2) != 0) throw new NonIntResultException(numer[i], denom[i]); System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); }

exception catch (ArithmeticException exc) { // catch the exception System.out.println("Can't divide by Zero!"); } catch (ArrayIndexOutOfBoundsException exc) { // catch the exception System.out.println("No matching element found."); } catch (NonIntResultException exc) { System.out.println(exc); } 4 / 2 is 2 Can't divide by Zero! Result of 15 / 4 is non-integer. 32 / 4 is 8 Can't divide by Zero! Result of 127 / 8 is non-integer. No matching element found.