Chapter 14 Abstract Classes and Interfaces

Similar documents
26. Interfaces. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

25. Interfaces. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 13 Abstract Classes and Interfaces

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

We are on the GUI fast track path

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Motivations. Chapter 13 Abstract Classes and Interfaces

COMP200 INTERFACES. OOP using Java, from slides by Shayan Javed

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Interface Class. Lecture 22. Based on Slides of Dr. Norazah Yusof

CS171:Introduction to Computer Science II. Li Xiong

Chapter 13 Abstract Classes and Interfaces

Abstract Classes Interfaces

Object Oriented Programming. Java-Lecture 11 Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Chapter 13 Abstract Classes and Interfaces

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Chapter 9 Abstract Classes and Interfaces

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

OVERRIDING. 7/11/2015 Budditha Hettige 82

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS1150 Principles of Computer Science Objects and Classes

Polymorphism and Interfaces. CGS 3416 Spring 2018

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Chapter 10 Classes Continued. Fundamentals of Java

25. Generic Programming

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

Practice for Chapter 11

Inheritance, Polymorphism, and Interfaces

Inheritance and Polymorphism

Inheritance and Polymorphism

CS 251 Intermediate Programming Inheritance

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Inheritance and Polymorphism

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CST242 Object-Oriented Programming Page 1

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

First IS-A Relationship: Inheritance

What is an interface? Why is an interface useful?

Chapter 5 Object-Oriented Programming

CS-202 Introduction to Object Oriented Programming

Programming 2. Inheritance & Polymorphism

Inheritance and Polymorphism

VIRTUAL FUNCTIONS Chapter 10

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

More Relationships Between Classes

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Lecture Notes Chapter #9_b Inheritance & Polymorphism

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

C++ Important Questions with Answers

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

What is Inheritance?

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Lecture 5: Inheritance

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Object-Oriented Programming More Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance (IS A Relationship)

Inheritance. Transitivity

Chapter 6 Introduction to Defining Classes

Programming II (CS300)

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

Chapter 11: Inheritance

Inheritance (continued) Inheritance

Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

More on Inheritance. Interfaces & Abstract Classes

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Inheritance, and Polymorphism.

Polymorphism. Arizona State University 1

Polymorphism and Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Introduction to Programming Using Java (98-388)

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

PROGRAMMING LANGUAGE 2

Inheritance (Deitel chapter 9)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Method Overriding in Java

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Programming in the Large II: Objects and Classes (Part 2)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Transcription:

Chapter 14 Abstract Classes and Interfaces 1

What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden in its subclass must mark with abstract too. Moreover, those methods must not contain any code. However, abstract class can have normal properties, constructors, and other methods. 2

How to make a class to be abstract (1) Here is an example: public abstract class Shape { private String color; public Shape(){ public String getcolor() return color; public void setcolor(string this.color = color; public abstract double getarea(); public abstract double getperimeter(); 3

How to make a class to be abstract (2) And then in subclass, the method that mark with abstract keyword, it will automatically request to be override without any excuse. public class Circle extends Shape{ private double radius public Circle(){ @Override public double getarea(){ return radius*radius*math.pi; @Override public double getperimeter(){ return 2*radius*Math.PI;

How to use abstract class? (1) You can use an abstract class by extends keyword. public class Circle extends Shape { Abstract class can also be a type. inheriting it using Shape sh;//shape is a type of sh variable Because abstract class can also be a type, we can use polymorphism as well. Shape sh = new Circle(); sh.getarea();

How to use abstract class? (2) You CANNOT create instances of abstract classes using the new operator. Shape shape = new Shape();// Compile Error We can make an abstract class by not making any method abstract also. There is no any error. public abstract class Shape { public String getcolor(){ return "";

Importance of abstract class Abstract class is always a superclass. It means when you make an abstract class, you have to think that the class must be a superclass later. Abstract class is the way to guarantee that its closed subclasses MUST override abstract methods. The only reason that we have to make abstract class is because of polymorphism. It makes no sense if we make abstract class, but we don t use any polymorphism.

Interfaces What is an interface? Why is an interface useful? How do you define an interface? How do you use an interface? 8

What is an interface? Why is an interface useful? An interface is a classlike construct that contains only constants and abstract methods. In many ways, an interface is similar to an abstract class, but the intent of an interface is to specify behavior for objects. For example, you can specify that the objects are comparable, edible, cloneable using appropriate interfaces. 9

Define an Interface To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface InterfaceName { constant declarations; method signatures; Example: public interface Edible { /** Describe how to eat */ public abstract String howtoeat(); 10

Interface is a Special Class An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. Like an abstract class, you cannot create an instance from an interface using the new operator, but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on. 11

Example You can now use the Edible interface to specify whether an object is edible. This is accomplished by letting the class for the object implement this interface using the implements keyword. For example, the classes Chicken implement the Edible interface. 12

Omitting Modifiers in Interfaces All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below: public interface T1 { public static final int K = 1; Equivalent public interface T1 { int K = 1; public abstract void p(); void p(); A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K). 13

Interfaces vs. Abstract Classes In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods. Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods 14