 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
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.
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.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
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.
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.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Abstract and Nested Classes
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
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.
Lecture 06 Java and OOP Jaeki Song. Outlines Java and OOP –Class and Object – Inheritance – Polymorphism.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
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.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Abstract Classes Course Lecture Slides 7 June 2010 “None of the abstract.
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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
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, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
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.
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 15 Abstract Classes and Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
Abstract Classes and Interfaces in Java Reference: COS240 Syllabus
Chapter 11 Abstract Classes and Interfaces
Static Members and Methods
CMSC 202 Static Methods.
null, true, and false are also reserved.
Classes and Objects 5th Lecture
Chapter 12 Abstract Classes and Interfaces
Java Classes and Objects 3rd Lecture
Chapter 14 Abstract Classes and Interfaces
Classes and Objects Static Methods
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class fills in methods left blank in the parent’s class  Reserved Words  implements, interface: Multiple-inheritance  abstract: Class with some methods left blank

 Inheritance allows  Programs to make use of parent class methods  A degree of polymorphism when the parent class has a default version of a method to be overridden  Limitations  Programs can only extend a single class  There is no accounting for methods for which there is not a version in the parent class Note: Java’s use of inheritance providing limited polymorphism is not universal. C++, for example, always calls parent class methods, even if they are overridden.

 When there are multiple levels of inheritance  The classes at the top of the hierarchy are more and more general  Child classes become more specific to the application  Issues to address  Anticipating methods that all child classes must include (reserved word abstract)  Providing categories of methods that classes of a particular category must include (reserved words interface and implements)

GeoMetric Object -color: Color -filled: boolean +getColor(): Color +setColor(color: Color): void +isFilled(): boolean +findArea(): double +findPerimeter(): double Circle -radius: double +getRadius(): double +setRadius(radius: double): void Rectangle -width: double -height: double +getWidth(): double +setWidth(width: double): void +getHeight(): double +setHeight(double: height): void Cylinder -height: double +getHeight(): double +setHeight(height: double): void +findVolume(): double Object -private, + public, # protected -How does GeometricObject code findArea and findPerimeter

Definition: Some methods are left to implement in child classes Note: Parent abstract classes, with empty methods, can’t be instantiated public abstract class GeometricObject { private Color color; protected GeometricObject() {} protected GeometricObject(Color color) { this.color = color; } public String getColor()() { return color; } public void setColor(String color) { this.color = color; } public boolean isFilled() { return color==null; } public abstract double findArea(); public abstract double findPerimeter(); } Note the semicolon at the end of the abstract methods (meaning no body)

 In Circle public double findArea() { return radius * radius * Math.PI; } public double findPerimeter( return 2 * radius * Math.PI; }  In Rectangle public double findArea() { return width * height; } public double findPerimeter( 2 * (width + height); }  In Cylinder public double findArea() { return 2 * (super.findArea() + getRadius() * Math.PI * height; } public double findVolume() { return super.findArea() * height; }

 Any class with abstract methods must also be declared to be abstract  A class with no abstract methods can be declared to be abstract  Any class declared abstract cannot be instantiated  A child class of a concrete (not abstract) parent can be declared abstract  Consider: abstract class Test {}  Legal: Test test;  Illegal: Test test1 = new Test();

 Definition: A class-like construct consisting of constants and method signatures  Syntax: modifier interface interfaceName { *** constant declarations *** *** method signatures *** }  Usage: public class myClass implements interface1, …, interfaceN

 Most interfaces are public  Illegal private interface Foo{ } protected interface Bar{ }  Legal public class Enclosing { private interface Foo{ } protected interface Bar{ } }  Interface member scope  variables are public final static  methods are public Note: public interface T { public static final int x=1; public abstract void p(); } Is equivalent to: public interface T { int x = 1; void p(); }

 abstract class advantages over interfaces  abstract classes can have methods with bodies  abstract classes can have non-constant variables  abstract classes allows protected, private scope  Abstract classes inherit from the Object class  Interface advantages over abstract classes  A class can implement more than one interface  An interface can extend more than one parent interface  Interfaces do not inherit from a root Object  Both abstract classes and interfaces allow  Interface or abstract class names are valid types

public class S extends P { public static void main(String[] args) { S s = new S(); P p; Q q; R r; p = s;q = s; r = s;} interface Q {} interface R {} abstract class P implements Q, R {}

public class Class2 extends Class1 implements face4, face5 {} abstract class Class1 implements face3 {} interface face1 {} interface face2 {} interface face3 extends face1, face2 {} interface face4 extends face3 {} interface face5 {} face5 face4face3 face2 face1 ObjectClass1 Class2

public class TestEdible { public static void main(String[] args) { Object[] objs = {new Tiger(), new Chick(), new Apple() }; for (int i=0; i<objs.length; i++) if (objs[i] instanceof Eat) System.out.println(((Eat)objs[i]).how()); } interface Eat { public String how(); } class Fruit implements Eat {public String how() { return "Eat Fresh";} } class Apple extends Fruit { public String how() { return "Apple Cider"; } } abstract class Animal {} class Tiger extends Animal {} class Chick extends Animal implements Eat { public String how() { return "Nuggets"; } } Output Nuggets Apple Cider

public class Max { public static Object max(Object o1, Object o2) { if (((Comparable)o1).compareTo(o2) > 0)return o1; else return o2; } } public class CompareRect extends Rectangle implements comparable {public CompareRect(double width, double height) { super(width, height); } public int compareTo(Object o) { return findArea() - ((CompareRect)o).findArea()); } } package java.lang; public interface Comparable { public int compareTo(Object o); }

public class House implements Cloneable { public GregorianCalendar built; public House(GregorianCalendar b) { built=b; } public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { return null; } } public static void main(String[] args) { House h1 = new House( new GregorianCalendar(35, 11, 10) ); House h2 = (House)h1.clone(); h2.built.set(50, 2, 20); System.out.println( h1.built.get(Calendar.YEAR) + " " + h2.built.get(Calendar.YEAR)); } } Output: because addresses, not contents, of built copied

Modify the cloneable method from the previous slide public Object clone() { try { House house = (House)super.clone(); house.built = new GregorianCalendar( built.get(Calendar.YEAR), built.get(Calendar.MONTH), built.get(Calendar.DAY_OF_MONTH)); return house; } catch (CloneNotSupportedException ex) { return null; } } Output: because we cloned the instance variable Alternative: house.built = (GregorianCalendar)built.clone(); Many Java API classes implement “deep” clone operations

 Definition: A class that adds functionality to a primitive variable or another class  Examples: Double, Float, Long, Integer, Short, Byte, Character, Boolean  Additional functionality:  Implements Comparable for Java’s generic sort capabilities  Parses strings into primitive types (Integer.parseInt("32");  Java’s default conversions to and from primitive variables. int x = Integer.parseInt("1A", 16); sets x to 26 Integer intObject = 2;

 Definition: Automatic conversion from a primitive variable to its wrapper  Definition: Un-boxing is the automatic conversion from a wrapper object to its primitive variable counterpart  Examples:  Integer x = 3; // Boxing  int x = Integer(3); // Unboxing  double d = Double.valueOf(“93.55”); // Unboxing  String h = (Double.valueOf("23.4")).toString(); (The toString method of Double is called)

Java.lang.Number +byteValue(): byte +shortValue(): short +intValue(): int +longValue(): long +floatValue(): float +doubleValue(): double Java.lang.Integer -value: int +MAX_VALUE: int +MIN_VALUE: int +Integer(vallue: int) +Integer(s: String) +valueOf(s: String): Integer +valueOf(s: String, radix:int): Ingeter +parseInt(s: String): int +parseInt(s: String, radix: int): int Java.lang.Comparable +compareTo(o: Object): int