UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.

Slides:



Advertisements
Similar presentations
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Advertisements

Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
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.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
 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
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Packages. Access Specifications Public Available anywhere (public keyword) Only one public class per file allowed Protected Available in subclasses, and.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Static Methods Slides 2 to 15 only Static Methods A static method is one that can be used without a calling object A static method still belongs.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
EKT 472: Object Oriented Programming
Java Basics Packages.
Interface.
CMSC 202 Interfaces.
Java Programming Language
Advanced Java Topics Chapter 9
Packages and Interfaces
Interfaces.
Advanced Java Programming
CSE 1030: Implementing GUI Mark Shtern.
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
Abstract Classes and Interfaces
Classes, Objects and Methods
Chapter 13 Abstract Classes and Interfaces Part 01
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Abstract Classes and Interfaces
CMSC 202 Interfaces.
Presentation transcript:

UNIT-3 Interfaces & Packages

What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in an interface are abstract. Interfaces can contain no implementation Interfaces cannot contain instance variables. However, they can contain public static final variables (ie. constant class variables) Interfaces are declared using the "interface" keyword If an interface is public, it must be contained in a file which has the same name. Interfaces are more abstract than abstract classes Interfaces are implemented by classes using the "implements" keyword.

Declaring an Interface public interface Steerable { public void turnLeft(int degrees); public void turnRight(int degrees); } In Steerable.java: public class Car extends Vehicle implements Steerable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } In Car.java: When a class "implements" an interface, the compiler ensures that it provides an implementation for all methods defined within the interface.

Implementing Interfaces A Class can only inherit from one superclass. However, a class may implement several Interfaces The interfaces that a class implements are separated by commas Any class which implements an interface must provide an implementation for all methods defined within the interface. NOTE: if an abstract class implements an interface, it NEED NOT implement all methods defined in the interface. HOWEVER, each concrete subclass MUST implement the methods defined in the interface. Interfaces can inherit method signatures from other interfaces.

Declaring an Interface public class Car extends Vehicle implements Steerable, Driveable { public int turnLeft(int degrees) { [...] } public int turnRight(int degrees) { [...] } // implement methods defined within the Driveable interface In Car.java:

Inheriting Interfaces If a superclass implements an interface, it's subclasses also implement the interface public abstract class Vehicle implements Steerable { private String make; [...] public class Car extends Vehicle { private int trunkCapacity; [...] Vehicle - make: String - model: String - tireCount: int Car - trunkCapacity: int Truck - bedCapacity: int public class Truck extends Vehicle { private int bedCapacity; [...]

Multiple Inheritance? Some people (and textbooks) have said that allowing classes to implement multiple interfaces is the same thing as multiple inheritance This is NOT true. When you implement an interface: The implementing class does not inherit instance variables The implementing class does not inherit methods (none are defined) The Implementing class does not inherit associations Implementation of interfaces is not inheritance. An interface defines a list of methods which must be implemented.

Interfaces as Types When a class is defined, the compiler views the class as a new type. The same thing is true of interfaces. The compiler regards an interface as a type. It can be used to declare variables or method parameters int i; Car myFleet[]; Steerable anotherFleet[]; [...] myFleet[i].start(); anotherFleet[i].turnLeft(100); anotherFleet[i+1].turnRight(45);

Abstract Classes Versus Interfaces When should one use an Abstract class instead of an interface? If the subclass-superclass relationship is genuinely an "is a" relationship. If the abstract class can provide an implementation at the appropriate level of abstraction When should one use an interface in place of an Abstract Class? When the methods defined represent a small portion of a class When the subclass needs to inherit from another class When you cannot reasonably implement any of the methods

© 2006 Pearson Addison- Wesley. All rights reserved 5-10 Packages and Import Statements Java uses packages to form libraries of classes A package is a group of classes that have been placed in a directory or folder, and that can be used in any program that includes an import statement that names the package – The import statement must be located at the beginning of the program file: Only blank lines, comments, and package statements may precede it – The program can be in a different directory from the package

© 2006 Pearson Addison- Wesley. All rights reserved 5-11 Import Statements We have already used import statements to include some predefined packages in Java, such as Scanner from the java.util package import java.util.Scanner; It is possible to make all the classes in a package available instead of just one class: import java.util.*; – Note that there is no additional overhead for importing the entire package

© 2006 Pearson Addison- Wesley. All rights reserved 5-12 The package Statement To make a package, group all the classes together into a single directory (folder), and add the following package statement to the beginning of each class file: package package_name; – Only the.class files must be in the directory or folder, the.java files are optional – Only blank lines and comments may precede the package statement – If there are both import and package statements, the package statement must precede any import statements

© 2006 Pearson Addison- Wesley. All rights reserved 5-13 The Package java.lang The package java.lang contains the classes that are fundamental to Java programming – It is imported automatically, so no import statement is needed – Classes made available by java.lang include Math, String, and the wrapper classes

© 2006 Pearson Addison- Wesley. All rights reserved 5-14 Package Names and Directories A package name is the path name for the directory or subdirectories that contain the package classes Java needs two things to find the directory for a package: the name of the package and the value of the CLASSPATH variable – The CLASSPATH environment variable is similar to the PATH variable, and is set in the same way for a given operating system – The CLASSPATH variable is set equal to the list of directories (including the current directory, ". ") in which Java will look for packages on a particular computer – Java searches this list of directories in order, and uses the first directory on the list in which the package is found

© 2006 Pearson Addison- Wesley. All rights reserved 5-15 A Package Name

© 2006 Pearson Addison- Wesley. All rights reserved 5-16 Pitfall: Subdirectories Are Not Automatically Imported When a package is stored in a subdirectory of the directory containing another package, importing the enclosing package does not import the subdirectory package The import statement: import utilities.numericstuff.*; imports the utilities.numericstuff package only The import statements: import utilities.numericstuff.*; import utilities.numericstuff.statistical.*; import both the utilities.numericstuff and utilities.numericstuff.statistical packages

© 2006 Pearson Addison- Wesley. All rights reserved 5-17 The Default Package All the classes in the current directory belong to an unnamed package called the default package As long as the current directory (. ) is part of the CLASSPATH variable, all the classes in the default package are automatically available to a program