1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Lecture 5: Interfaces.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Chapter 10: Introduction to Inheritance
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
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.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Inheritance-Basics.
Inheritance and Polymorphism
Modern Programming Tools And Techniques-I Inheritance
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Java Programming Language
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Chapter 5 Classes.
Presentation transcript:

1 Chapter 6 Inheritance, Interfaces, and Abstract Classes

2 Inheritance basics Every class except Object extends (that is, is a subclass of) exactly one other class. Java supports only single inheritance for classes; that is, a class can have only one superclass. Java supports multiple inheritance for interfaces; that is, an interface can have more than one superinterface.

3 Inheritance basics If a class is declared without an explicit superclass, Object is the superclass. The code segment class C {/*...*/} is thus shorthand for class C extends Object {/*...*/}

4 Inheritance basics The general syntax for inheritance is class Mang extends Emp {/*...*/} interface I3 extends I1, I2 {/*...*/} A subclass inherits all superclass members except for constructors. A subinterface inherits all superinterface members.

5 Inheritance basics Because Object is at the root of the class inheritance hierarchy in Java, Object has no superclass. Every other class, standard or programmer defined, has Object as an ancestor. Object members such as the toString() method are inherited by all Object descendants.

6 Inheritance and scope Scope rules apply in inheritance. For instance, given class Child extends Parent {/*...*/} Parent must have either public scope or be in the same package as Child.

7 Inheritance and scope Inherited members normally retain their scope. For instance, a superclass member with private scope is inherited with private scope by the subclass. An inherited member’s scope can be increased but not decreased. For example, an inherited protected member can be redeclared as public but not vice-versa.

8 Inheritance and scope The clone method in Object has protected scope. Object subclasses are allowed to redeclare clone as public in accordance with the rule that an inherited member’s scope can be increased but not decreased.

9 Constructors under inheritance Although constructors are not inherited, they play a key role in inheritance. Every subclass constructor must invoke a superclass constructor, either implicitly or explicitly. –The invocation must occur as the first statement in the subclass constructor.

10 Constructors under inheritance class C extends Object { C(){ n = -999; } int n; } is shorthand for class C extends Object { C(){ super(); n = -999;} int n; }

11 Constructors under inheritance If the programmer does not explicitly invoke a superclass constructor in a subclass constructor, the compiler in effect inserts super() as the first statement. super() invokes the superclass no- argument constructor. super(-1) invokes a superclass parameterized constructor.

12 Constructors under inheritance Because a subclass derives from a superclass, it is appropriate that every subclass constructor invoke a superclass constructor to handle construction of the inherited or “superclass part” of a subclass object.

13 Name hiding Within a class, every field must have a distinct name. In an inheritance hierarchy, a subclass field may have the same name (e.g., num ) as a superclass field. In this case, the subclass field hides the superclass field of the same name. The term super can be used to access the superclass field: super.num.

14 Name hiding A subclass method may have the same signature as a superclass method. In this case, the subclass method overrides the superclass method of the same name. –It is common for Object descendants to override the toString() method inherited ultimately from Object. Overriding is deliberate name hiding.

15 Disabling inheritance A class can be declared as final prevent subclassing. The standard String class is declared as final; hence, no standard or programmer- defined class can extend String.

16 Inheritance and casts A reference of a superclass type can refer to an object of a subclass type: Object obj = new Date(); A reference of a subclass type can refer with casting to an object of a superclass type, but this is very dangerous: Date d = (Date) new Object();

17 Inheritance and casts A reference of one peer type cannot refer, even with explicit casting, to an object of another type peer. Peer types are classes between which there is no ancestor/descendant relationship. Date d = (Date) new String(); // error

18 Inheritance and casts Casting a reference to subclass type (e.g., Date ) so that the reference can refer to an object of a superclass type (e.g., Object ) is known as a down cast. Down casts are not type safe; that is, such casts can lead to subtle run-time errors.

19 Polymorphism Polymorphism has two requirements: –A subclass method must override a superclass method. Only superclass methods with public and protected scope are eligible for overriding. –A superclass reference, which may refer to either a superclass or a subclass object, is used to invoke the method.

20 Polymorphism The code segment Object obj; if ( new Random().nextInt() > 0 ) obj = new Date(); else obj = new Vector(); System.out.println( obj.toString() ); illustrates a polymorphic invocation.

21 Polymorphism All Component s such as windows, buttons, and labels inherit a setVisible method that can be overridden to display the component in a class-appropriate fashion. If com refers to any Component, then comp.setVisible( true ) polymorphically invokes the setVisible method.

22 Review of overriding Only protected and public methods can be overriden. Only non static methods can be overridden. Methods declared as final cannot be overriden.

23 Interface basics An interface is defined with the keyword interface : interface MyInterface { public void m1(); public String m2(); } An interface cannot be instantiated as an object.

24 Interface basics An interface can contain –method declarations rather than definitions –static and final fields, which represent constants public static final double pi = ; All interface members must be declared public. Empty or marker interfaces are allowed.

25 Interface basics A class implements an interface: class C implements Runnable { A class can implement multiple interfaces. The interface identifiers are separated by commas. One interface can extend (subclass) one or more interfaces.

26 Interfaces and concrete classes If a class implements an interface, the class must define all of the methods declared in the interface in order to be concrete. –A concrete class is a class that can be instantiated as an object. –An abstract class is a class that cannot be instantiated as an object.

27 Interface basics An interface, like a class, has either package or public scope. Package scope is the default. An interface may be declared abstract abstract interface MyIface {/*...*} to emphasize that the interface cannot be instantiated as an object.

28 Interface basics All interfaces are abstract even if not declared so. The term abstract signals that an interface cannot be instantiated as an object. Interface methods may be declared abstract to emphasize that interface methods are only declared rather than defined.

29 Interfaces and concrete classes The Runnable interface declares one method public void run(); A class that implements Runnable must define run in order to be concrete.

30 Interface types and references Object references can be of interface types. The code segment Set s1 = new HashSet(); illustrates. Set is an interface rather than a class. Because s1 is an interface type, it can be used to invoke only methods declared in the Set interface.

31 Interface types and references “Programming to the interface” is a key principle in object-oriented programming. Wherever possible, object references should be of interface types. In JDK documentation, interfaces are listed before classes. This underscores that classes should be viewed, wherever possible, as interface implementations.

32 Nested interfaces An interface can be nested inside a class. In this case, the interface is static regardless of whether it is declared so.

33 Abstract classes An abstract class cannot be instantiated as an object: abstract class ABS {/*...*/} ABS a1 = new ABS(); //*** ERROR Abstract classes can contain an arbitrary mix of method declarations and method definitions.

34 Abstract classes There are three ways for a class to be abstract: –The class is explicitly declared abstract. –An encapsulated method is explicitly declared abstract. –The class fails to define all of the methods in the interfaces that the class implements.

35 Abstract classes Abstract classes are bona fide classes. For instance, such classes descend from Object, have exactly one superclass, and so on. Abstract classes tend to have highly specialized uses. Beginning programmers typically use either concrete classes or interfaces.