Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
Class Inheritance Liang, Chpt 8. X is a Y: the is-a hierarchy life-form animal person clown geometric object circle cylinder instrument trumpet piano.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Inheritance and.
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Chapter 5 Abstract Classes and Interfaces. 2 Objectives u To design and use abstract classes. u To process a calendar using the Calendar and GregorianCalendar.
Lecture 4: Object Composition, Inheritance and Polymorphism Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
1 1 Chapter 14 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code.
Chapter 11 Abstract Classes and Interfaces
Lecture 4: Object Composition, Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Chapter 10 Thinking in Objects
Overloading and Constructors
The Language Package.
Chapter 8 Class Inheritance and Interfaces
Chapter 10 Thinking in Objects Part 2
Chapter 10 Thinking in Objects
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass – child, derived, extended class A subclass inherits functionality from its supperclass, and also creates new data and new methods. Subclasses usually have more functionality than their superclasses

Circle and cylinder public class Cylinder extends Circle{ private double length; public Cylinder(){ super(); // calls circle default constructor length = 1.0;} public Cylinder( double r, double m) { super(r); // calls another circle constructor length = m; } public double getLength(){ return length;} public double findVolume{ return findArea()*length; }

The Object class Every class in Java is descended from Java.lang.Object class. If no inheritance is specified when a class is defined. The supper class of the class is Object Classes like Circle, String, StringBuffer are implicitly the child classes of Object Three instance methods in object class are:  public boolean equals(Object obj)  public String toString()  public Object clone()

The clone method Sometimes you need to make a copy of an object. Mistakenly, you might use the assignment operator newObject = someobject; This statement does not create a duplicate object. It simply assigns the reference of someobject to newobject. To create a new object with separate memory space, you need to use clone() method: newObject = someObject.clone(); This statement copies someobject to a new memory location and assigns the reference of the new object to newObject.

Protected, private and public The protected data or methods in a public class can be accessed by any class in the same package or its subclasses, even if the subclasses are in different packages.

final Used on variable – constant  final double PI = ; Used on methods – the methods cannot be modified by subclass Used on class – the class can not be parent class, cannot be extended

Abstract classes The class cannot have specific instances Can have data and methods like regular classes Can have abstract method An abstract method is a method signature without implementation. The abstract methods are implemented in subclasses

GeoObject public abstract class GeoObject{ private String color=“white”; public String getColor(){return color;} … public abstract double findArea(); public abstract double findPerimeter(); }

Redefine circle inherit from GeoObject public class circle extends GeoObject{ private double radius; … public double findAera(){ return radius*radius*Math.PI; } public String toString(){ return “[Circle] radius = “ + radius; }

Casting objects and the instanceof operator Casting can also be used to convert an object of one class type to another within an inheritance hierarchy. GeoObject ob = new Circle(); is known as implicit casting But Circle cir = ob; will have compiler error Use explicit casting Circle cir = (Circle)ob; It is always possible to cast an instance of a subclass to a variable of a supper class

When casting an instance of a superclass to a variable of its subclass, explicit casting must be used You must make sure that the object to be cast is an instance of the subclass. If not, a run time exception occurs. // suppose myObj is declared as Object type if( myObj instanceof Circle) { Circle cir = (Circle)myObj; System.out.println(“The circle area is” +cir.findArea()); }

Processing primitive type values as objects Primitive data types are not objects Many methods require the use of object Java offers corresponding class, called wrapper class Integer, Double, Float, Long, Short, Byte, Character and Boolean

Numeric wrapper class Constructors  Public Integer(int value)  Public Integer(String s)  Public Double(double value)  Public Double(String s) Example:  Double Obj = new Double(5.0);  Double obj = new Double(“5.0”);

Numeric wrapper class constant MAX_VALUE: represent the maximum value MIN_VALUE: represent the minimum value System.out.println(“maximum integer is” + Integer.MAX_VALUE);

Conversion methods Each numeric wrapper class implements the abstract methods doubleValue, floatValue, intValue, longValue and shortValue, which are defined in the number class Each wrapper class also overrides the toString and equals methods Integer obj = new Integer(“23”); int I = obj.intValue();

Sorting an array of objects Write a static method for sorting an array of comparable objects They are compared using compareTo method

Static valueOf methods Wrapper class have a useful method  valueOf(string s) This method creates a new object initialized to the value represented by the specified string Double obj = Double.valueOf(“12.4”); Integer obj = Integer.valueOf(“12”);