Advanced Java Programming

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
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)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Lecture 5:Interfaces and Abstract Classes
Inheritance.
Abstract classes and interfaces
Advanced Programming in Java
Advanced Programming in Java
OOP: Encapsulation &Abstraction
Final and Abstract Classes
Inheritance and Polymorphism
Interface.
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Chapter 13 Abstract Classes and Interfaces
CS240: Advanced Programming Concepts
Advanced Java Programming
Interface.
MSIS 670 Object-Oriented Software Engineering
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Java Programming, Second Edition
Java Inheritance.
Advanced Programming in Java
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Advanced Java Programming Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11

Chapter Goals To be able to declare and use interface types To appreciate how interfaces can be used to decouple classes

Why Interface A single motor is a reusable component. How about reusable software components?

Why Interface Inheritance? Session #, Speaker Name Why Interface Inheritance? Java does not allow you to inherit from more than one class (Superclass) Separating reusable part -- Interface methods A class will implements the interface methods. Use more than one reusable component but these are not related to each other. So can not be put in a single superclass. 08/24/11

Why Interface Interfaces are used to express the common operations. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. No implementations for these common behaviors

Interface An abstract type that contains no data, but exposes behaviors defined as methods. A class can only extends to one super abstract class, but can implements more than one interfaces) Composed by public static final attributes and/or method(s) without implementation.

Defining an interface Interface types are used to express the common operations.

Defining an interface Therefore, can implement a reusable average method:

Implementing an interface A class implements an interface type if it declares the interface in an implements clause. The class should then implement the method or methods that the interface requires

Implementing an interface Method(s) in interface are public, abstract by default. Don’t forget to declare the methods’ modifier in the implemented class. Interface cannot be instantiated. Variables in Interface are public static final by default. Method(s) in subclass cannot have narrower modifier than the method(s) in interface.

Implementing an interface

Interface VS. Inheritance a class can implement multiple interfaces a class can only extend a single superclass. interface provides methods without implementation superclass provides some implementation that a subclass inherits developing interfaces when you have code that processes objects of different classes in a common way. using inheritance when have objects with something in common.

Interface VS. Abstract class An interface cannot provide any code at all, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden. A Class may implement several interfaces. In case of abstract class, a class may extend only one abstract class. All methods of an Interface are abstract. An abstract class can have non-abstract methods. An Interface cannot have instance variables. An abstract class can have instance variables. An Interface visibility must be public (or) none. An abstract class can have any visibility: public, private, protected. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. An Interface cannot contain constructors. An abstract class can contain constructors. Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.

Abstract, Interface and Inheritance A superclass can be an abstract class (without implementing some methods), and also can be a regular class with implementations for all methods in it. An Interface type always be a abstract class without any implementation of methods ….. You list more!

Common Error

Common Error

Constants in Interfaces Interface can not have instance variable But can have constants The constant in an interface is public, static and final by default, you could omit these reserved words.

Announcement For some reason, you know what it is, the due date of programming assignment 2 is extended to Feb 4 (next Tuesday)!