Multiple inheritance Composition Interfaces Polymorphism Inheritance in Java (part 2)

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
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.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Written by: Dr. JJ Shepherd
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
Chapter 10 Classes Continued
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.
Multiple Choice Solutions True/False a c b e d   T F.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Porting Implementation of Packet Utilization Standard from ADA to JAVA Annelie Hultman (TEC-EME) Donata Pedrazzani (TEC-EMS) ESA/ESTEC 2004 JPUS de-briefing.
Inheritance in the Java programming language J. W. Rider.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
 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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved Java does not support multiple inheritance. Interface Characteristics...
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Coming up: Inheritance
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces and Inner Classes
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Modern Programming Tools And Techniques-I
Interface.
Chapter 11: Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
UML Class Diagram: class Rectangle
Interfaces.
Class Inheritance (Cont.)
null, true, and false are also reserved.
Interface.
Java Programming Language
Interfaces.
AP Computer Science DYRT Quiz
Final and Abstract Classes
C++ Polymorphism Reference and pointer implicit type casting
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Multiple inheritance Composition Interfaces Polymorphism Inheritance in Java (part 2)

Multiple inheritance abstract class Electronics { … } class clock extends Electronics { …} class radio extends Electronics { …} class clock-radio extends clock, radio

Multiple Inheritance Allowed in C++ (1985) Not Allowed in Java (1995), C# (2000) Issues Data in common base class is duplicated (i.e. Serial Number) Methods in base classes are overloaded (i.e. powerUp() ) Two parent classes may have different implementations for the same feature (i.e. makeLouder() )

Composition in Java class Clock-Radio extends Electronics { …} { Clock c = new Clock(); Radio r = new Radio(); }

Rapper methods for composition class clock extends Electronics { void setTime(int h, int m) { hours=h; mins=m;} void setAlarm(int h, int m) { almH = h; almM = m;} } class clock-radio extends Electronics { Clock c = new Clock(); Radio r = new Radio(); void setTime(int h, int m) { c.setTime(h,m); } }

Interfaces An interface is a description of a capability. It lists methods that a class must implement to carry out that capability. A class may implement multiply interfaces. An interface may be implemented by many classes.

Example of an interface interface autoShutOff { boolean deviceTooHot(int temperture); void autoShutOff(); } class Radio extends Electronics implements autoShutOff() { …. boolean deviceTooHot(int t) { if (temp > 98) {return true} else { return false; } void autoShutOff { if (deviceTooHot()) { … } } }

Interfaces are like Classes Is complied into bytecode file, named xyz.class public, protected, private or package Cannot be public unless same name as filename Serves as a type for declaring variables

Interfaces are not like Classes… Declares only method headers (no method body allowed) Cannot declare data storage, only public constants. Has no constructors Cannot be instantiated Can be implemented by a class Cannot implement an interface (but can extend an interface) Cannot extend a class Can extend several other interfaces

Java types Interface Class Array Primitive

Interface extends interface interface abc { // method signatures of abc } interface xyz extends abc { // method signatures of xyz } class C implements xyz { // bodies of abc and xyz methods }

Multiple Interface example interface abc { // method signatures of abc } interface xyz { // method signatures of xyz } class C implements abc, xyz { // bodies of abc and xyz methods }

Ex) Java’s Comparable interface The standard libraries in Java define over 600 interfaces. java.lang.Comparable interface in one. It has one method int compareTo(Object o) { … } Usage: int result = x.compareTo(y) Returns a negative integer when x < y Returns 0 when x == y Returns a positive number when x > y

Class object may need ordering People, clock-radio, etc. do not have a natural ordering ( ) class Student extends Person implements Comparable { … public int compareTo(Object o) { Student that = (Student)o; if (this.age < that.age) return -1; if (this.age > that.age) return 1; return 0; }

Polymorphism in Java The word ‘polymorphism’ literally means ‘a state of having many shapes’ or ‘the capacity to take on different forms’. An object may be referred to by a reference: - of it’s type - of any type it inherited from - of any interface it’s class implements - of any interface implemented by a class it inherits from

Polymorphism of Classes abstract class Electronics implements powerOn{ … } class clock extends Electronics { …} class radio extends Electronics { …} class clock-radio extends Electronics { …} Electronics ec = new Clock(); Electronics er = new Radio(); Electronics ecr = new Clock-Radio(); PowerOn po = ec; ec.powerUp(); er.printSerialNumber(); ecr.setTime(3,20); // not allowed

Polymorphism of Interfaces abstract class Shapes implements Drawables { … } class Circle extends Shapes { …} class Square extends Shapes { …} Drawables dc = new Circle(); Drawables ds = new Square(); dc.draw(); // calls the drawable method in Circle ds.draw(); // calls the drawable method in Circle int a = ds.area() (3,20); // not allowed