Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.

Slides:



Advertisements
Similar presentations
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, 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,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Java and C++, The Difference An introduction Unit - 00.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Interface 1. Using interface, we specify what a class must do, but not how it does this. An interface is syntactically similar to a class, but it lacks.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Learners Support Publications Classes and Objects.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
 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.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
Packages and Interfaces 1. Packages The mechanism provided by Java for partitioning the class namespace into more manageable chunks. It is both a naming.
Object Oriented Programming
CSI 3125, Preliminaries, page 1 Compiling the Program.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Packages and Interfaces. Packages are containers for classes that are used to keep the class name space compartmentalized It provides mechanism for partitioning.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
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.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
2 Chapter Classes & Objects.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Final and Abstract Classes
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Packages When we name a program , we name it by class name…
Packages, Interfaces & Exception Handling
Packages and Interfaces 1. Packages The mechanism provided by Java for partitioning the class namespace into more manageable chunks. It is both a naming.
Interfaces.
Object Based Programming
UNIT-3.
Abstract classes and interfaces
Interface.
Java Programming Language
Chapter 9: Polymorphism and Inheritance
Packages and Interfaces
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Classes and Objects.
Java – Inheritance.
Abstract classes and interfaces
Presentation transcript:

Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru interface refrnc Partial Implementations Variables in Interfaces Interfaces Can Be Extended

Programming in java Packages Till now all java classes were stored in same name space. As the applications grows it becomes difficult to keep all the files in same directory, also we need to ensure that the name we choose should be unique. To address this problem java introduces a mechanism to manage all the classes this is called packages packages are containers for classes that are used to keep the class name space compartmentalized. It is both a naming and a visibility control mechanism

Programming in java To create a package simply include a package command as the first statement in a Java source file. Any classes declared within that file will belong to the specified package. The package statement defines a name space in which classes are stored. If we omit the package statement, the class names are put into the default package, which has no name General form of the package statement: package pkgname; Eg: package mypackage;

How to run packages d:\Teaching\MCA\Java\EclispeWorkBench\Training >cd src d:\Teaching\MCA\Java\EclispeWorkBench\Training \src>java org.mes.basic.Sample Hi d:\Teaching\MCA\Java\EclispeWorkBench\Training \src> Programming in java

How to run.. Programming in java

Java uses file system directories to store packages. The.class files for any classes you declare to be part of mypack must be stored in a directory called mypack/ is significant, and the directory name must match the package name exactly. More than one file can include the same package statement. The package statement simply specifies to which package the classes defined in a file belong. It does not exclude other classes in other files from being part of that same package.

Programming in java To create package hierarchy separate each package name from the one above it by use of a period. package pkg1[.pkg2[.pkg3]]; package java.awt.image; A package hierarchy must be reflected in the file system of your Java development system. For example, a package declared as package training.mes.mca.inheritance needs to be stored in following directory training\mes\mca\inheritance

Programming in java Access Protection

Access… While Java's access control mechanism may seem complicated, we can simplify it as follows. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member does not have an explicit access specification, it is visible to subclasses as well as to other classes in the same package. This is the default access. If you want to allow an element to be seen outside your current package, but only to classes that subclass your class directly, then declare that element protected. Programming in java

Importing Packages In java all of the standard classes are stored in some named package. If we need use some classes from different package we should import that package using import statement Once imported, a class can be referred to directly, using only its name. To specify either an explicit classname or a star (*), which indicates that the Java compiler should import the entire package. import pkg1[.pkg2].(classname|*); import java.util.Date; import java.io.*; Refer :TestClassB.java /Training2/com/mes/training/packageB/AccessF romA.java

Programming in java Java program structure A single package statement (optional) Any number of import statements (optional) A single public class declaration (required) Any number of classes private to the package (optional)

Programming in java Interfaces Using interface, we can fully abstract a class' interface from its implementation. Using interface, we can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are declared without any body. Once it is defined, any number of classes can implement an interface. One class can implement any number of interfaces. To implement an interface, a class must create the complete set of methods defined by the interface. Each class is free to determine the details of its own implementation

Programming in java Why interface? Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. This requirement by itself makes for a static and non extensible classing environment. Eg:-College Office appln,employee management,Student management both needs method to delete insert the employee /student details.So make an interface which has all the CURD ops and implement that is all classes

Programming in java Defining interface access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; //... return-type method-nameN(parameter-list); type final-varnameN = value; } Access can be public or default The methods which are declared have no bodies(abstract). They end with a semicolon after the parameter list. Each class that includes an interface must implement all of the methods.

Programming in java Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. Variables must also be initialized with a constant value. All methods and variables are implicitly public if the interface, itself, is declared as public. interface Callback { void callback(int param); } Once an interface has been defined, one or more classes can implement that interface

Programming in java To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface access class classname [extends superclass][implements interface [,interface...]] { // class-body } access is either public or not used. If a class implements more than one interface, the interfaces are separated with a comma. If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface.

Programming in java The methods that implement an interface must be declared public. Also, the type signature of the implementing method must match exactly the type signature specified in the interface definition interface Callback { void callback(int param); } class Client implements Callback { public void callback(int p) { System.out.println("callback called with " + p); } }

Programming in java interface Callback { void callback(int param); } class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } } It is both permissible and common for classes that implement interfaces to define additional members of their own.

Programming in java class Client implements Callback { // Implement Callback's interface public void callback(int p) { System.out.println("callback called with " + p); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces " + "may also define other members, too."); } }

Programming in java Accessing implementation through interface references It is possible to declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be stored in such a variable. When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. This is one of the key features of interfaces.

Programming in java The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. The calling code can dispatch through an interface without having to know anything about the "callee." This process is similar to using a superclass reference to access a subclass object, as described in DynamicMethodDespatch

Programming in java interface Callback { void callback(int param); } class Client implements Callback { public void callback(int p) { System.out.println("callback called with " + p); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces " + "may also define other members, too."); } } class TestIface { public static void main(String args[]) { Callback c = new Client(); c.callback(42); } } callback called with 42

Programming in java Notice that variable c is declared to be of the interface type Callback, yet it was assigned an instance of Client. Although c can be used to access the callback( ) method, it cannot access any other members of the Client class. An interface reference variable only has knowledge of the methods declared by its interface declaration. Thus, c could not be used to access nonIfaceMeth( ) since it is defined by Client but not Callback

Programming in java Partial Implementations If a class includes an interface but does not fully implement the methods defined by that interface, then that class must be declared as abstract. abstract class Incomplete implements Callback { int a, b; void show() { System.out.println(a + " " + b); } //... } Here, the class Incomplete does not implement callback() and must be declared as abstract. Any class that inherits Incomplete must implement callback() or be declared abstract itself.

Programming in java Variables in Interfaces You can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values. If an interface contains no methods, then any class that includes such an interface doesn't actually implement anything. It is as if that class were importing the constant variables into the class name space as final variables. Ref: SharedConstAppln.java

Programming in java Interfaces Can Be Extended One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain

Reference /Training2/com/mes/training/interfaces/SharedConst Appln.java /Training/src/org/mes/inheritance/SalutationApplicat ion.java /Training2/com/mes/training/interfaces/InterfaceApp ln.java Programming in java