Internet Software Development Classes and Inheritance Paul J Krause.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Lecture 5: Interfaces.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
OOP: Inheritance By: Lamiaa Said.
JAVA METHODS and CONSTRUCTORS. 2 JAVA Classes The class is the fundamental concept in JAVA (and other OOPLs) A class describes some data object(s), and.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CIS 368/568 BU209 Brian O’Haire MCSE MCDBA BU322 Offices Hours 7-8 before class Other times by appointment Web.
Introducing Java Stuart Fitz-Gerald (with thanks to Chris Reade)
Introduction to Object- Oriented Programming with Java Spring Semester 2003 Paul Krause.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Class and Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
The Java Keyword this Marie J. Garlitos. Instance Methods First we introduce instance methods: –any method not declared with a static keyword –operates.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UMBC CMSC 331 Java JAVA BASICS. UMBC CMSC 331 Java 2 Comments are almost like C++ The javadoc program generates HTML API documentation from the “javadoc”
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Inheritance in the Java programming language J. W. Rider.
Programming in Java CSCI-2220 Object Oriented Programming.
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.
Objects and Classes Mostafa Abdallah
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance.
Topic: Classes and Objects
Classes and Objects Introduced
Object-Oriented Programming with Java
Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Interfaces.
Object-Oriented Software Engineering
Chapter 8 Class Inheritance and Interfaces
JAVA BASICS.
Final and Abstract Classes
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Internet Software Development Classes and Inheritance Paul J Krause

Classes and Inheritance Contents  Object-Oriented Programming in Java  Fields and methods  Implementing Inheritance  Method overloading  Abstract classes

The Members of a Class  Class fields public static final double PI = ; public static final double PI = ;  Class methods public static double radiansToDegrees(double rads) {…} public static double radiansToDegrees(double rads) {…}  Instance fields public double radius; public double radius;  Instance methods public double circumference() {…} public double circumference() {…}

Class Fields public static final double PI =  A field of type double  Named PI (capitalise constants)  Assigned a value of  The static modifier tags this as a Class Field Associated with the class in which it is defined Associated with the class in which it is defined  The final modifier means it cannot be changed

Class Fields…  There is only one copy of PI  Any instance of Class can refer to this field as PI  PI is essentially a Global Variable BUT  Methods that are not part of Circle access this as Circle.PI No name collisions No name collisions

Class Methods public static double radiansToDegrees(double rads) { return rads * 180 / PI; return rads * 180 / PI; }  Single parameter of type double and returns a value of type double  Is essentially a “global method” // how many degrees is 2.0 radians? double d = Circle.radiansToDegrees(2.0);

Instance Fields public double radius;  Each Circle object can have a have a radius independent of other Circle objects  Outside a class, a reference to an instance field must be prepended by a reference to the object that contains it Circle c = new Circle(); c.radius = 2.0; Circle d = new Circle(); d.radius = c.radius; Are they the same object?

Instance Methods  Instance methods operate on instances of a Class, and not on the Class itself  E.g. area() area() circumference() circumference()  If an instance method is used from outside the Class itself, it must be prepended by a reference to the instance to be operated on: Circle c = new Circle(); Circle c = new Circle(); c.radius = 2.0; c.radius = 2.0; double a = c.area(); double a = c.area();

Creating an Instance  Every Class has at least one constructor  This is used as a default constructor - a method with the same name as the Class Circle c = new Circle();  The new operator creates a new uninitialised instance of the Class  The constructor method is then called, with the new object passed implicitly

Initialising an Instance  A Constructor can use arguments placed between the parentheses to perform initialisation  Define a new Constructor for Circle public Circle(double r) {this.r = r;}  Now two ways: Circle c = new Circle(); c.r = 0.25;  Or Circle c = new Circle(0.25);

Multiple Constructors public Circle() { r = 1.0; } public Circle(double r) {this.r = r;}  This is a simple example of method overloading

Method Overloading  Definition of multiple methods with the same name.  This is perfectly legal in Java, provided each version of the method has a different parameter list (so there is no ambiguity)  E.g. Circle( ) Circle( ) Circle(double r) Circle(double r)

This and that  Consider the following code fragment: Circle c = new Circle(1.0); double a = c.area();  What are those empty parentheses doing there?  How does a function with no parameters know what data to operate on?  There is an implicit argument named this: Holds a reference to the object c Holds a reference to the object c  We also use “this” in order to make it clear an object is accessing its own fields

Destroying Objects  Java automatically reclaims the memory occupied by an object when it is no longer needed Garbage Collection Garbage Collection  The Java interpreter can determine when an object is no longer referred to by any other object or variable Also works for cycles Also works for cycles

Implementing Inheritance Circle radius circumference area PlaneCircle cx cy isInside

“PlaneCircle” as a subclass public class PlaneCircle extends Circle { } public double cx, cy; public PlaneCircle(double r, double x, double y) { super(r); this.cx = x; this.cy = y; } // automatically inherit fields and methods of Circle PlaneCircle cx cy isInside public boolean isInside(double x, double y) { … }

Subclass Constructors  In this case, the word “super”: Invokes the constructor method of the superclass Invokes the constructor method of the superclass Must only be used in this way within a constructor method Must only be used in this way within a constructor method Must apear within the first statement of the constructor method Must apear within the first statement of the constructor method public PlaneCircle(double r, double x, double y) { super(r); // invoke constructor of superclass this.cx = x; // initialise instance field cx this.cy = y; // initialise instance field cy }

Making more circles  PlaneCircle pc = new PlaneCircle(1.0, 0.0, 0.0); // Create a unit circle at the origin  double a = pc.area( ); // Calculate it’s area by invoking an inherited method  boolean test = pc.isInside(1.5, 1.5); // Test if the point (1.5, 1.5) is inside the PlaneCircle pc or not  What other methods might we want in PlaneCircle?

Overriding methods Account number balance credit debit SavingsAccount credit debit

Method Overriding public class Account { public int number; public double balance; public void credit(double x) { // do some sums } public void debit(double y) { // do checking then sums } public class SavingsAccount extends Account { // instance fields inherited public void credit(double x) { // do some sums // update interest rate } public void debit(double y) { // do checking then sums // update interest rate } }

Overloading vs. Overriding  Overloading: Multiple methods with the same name In the same class, but In the same class, but Different parameter lists Different parameter lists  Overriding: Multiple methods methods with the same name With exactly the same signatures, but With exactly the same signatures, but In different classes in an inheritance hierarchy In different classes in an inheritance hierarchy

Abstract Classes EllipticalShape circumference area Circle radius Ellipse semiMinorAxis semiMajorAxis

Abstract Classes  An abstract class cannot be instantiated  A subclass of an abstract class can only be instantiated if: It overrides each of the abstract methods of its superclass, and It overrides each of the abstract methods of its superclass, and Provides a concrete implementation of them Provides a concrete implementation of them  It’s then known as a concrete class

Java Definition public abstract class Elliptical Shape { public abstract double area( ) ; public abstract double circumference( ) ; // Note semicolons instead of body of methods }