Sampath Kumar S Assistant Professor, SECE

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
BASIC JAVA PROGRAMMING TUTORIAL. History  James Gosling and Sun Microsystems  Oak  Java, May 20, 1995, Sun World  Hot Java –The first Java-enabled.
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.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
Topics Inheritance introduction
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
Introduction To Java Programming 1.0 Basic Concepts of Java Programming 2.0 Classes, Polymorphism and Inheritance 3.0 Exception handling 4.0.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
Inheritance and Polymorphism
Interface.
Inheritance in Java.
INHERITANCE IN JAVA.
UML Class Diagram: class Rectangle
Programming in Java Text Books :
Modern Programming Tools And Techniques-I Inheritance
Interface.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Chapter 6 Methods: A Deeper Look
Java Programming Language
Polymorphism and access control
Sampath Kumar S Assistant Professor, SECE
Interfaces.
Lecture 16 - Interfaces Professor Adams.
Advanced Java Programming
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
METHOD OVERRIDING in JAVA
Java Intro.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Sampath Kumar.S Assistant Professor/IT, SECE
Advanced Programming in Java
S.VIGNESH Assistant Professor, SECE
Method Overloading in JAVA
Sampath Kumar S Assistant Professor, SECE
Inheritance Inheritance is a fundamental Object Oriented concept
Method Overriding in Java
Sampath Kumar S Assistant Professor, SECE
Graphics Programming - Frames
Sampath Kumar S Assistant Professor, SECE
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Sampath Kumar S Assistant Professor, SECE
AP Computer Science DYRT Quiz
Chapter 13 Abstract Classes and Interfaces Part 01
Sampath Kumar S Assistant Professor, SECE
Interfaces,Packages and Threads
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Sampath Kumar S Assistant Professor, SECE Interfaces Sampath Kumar S Assistant Professor, SECE

12/28/2018 Interfaces An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. 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. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. Sampath Kumar S, AP

12/28/2018 Why use Interface? There are mainly three reasons to use interface. They are given below. It is used to achieve fully abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling. Sampath Kumar S, AP

Similarities Between Class and Interface: 12/28/2018 Similarities Between Class and Interface: An interface is similar to a class in the following ways: An interface can contain any number of methods. An interface is written in a file with a “.java” extension, with the name of the interface matching the name of the file. The bytecode of an interface appears in a “.class” file. Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name. Sampath Kumar S, AP

Difference between Interface and Class 12/28/2018 Difference between Interface and Class An interface is different from a class in several ways, including: You cannot create instance(Create Object) to an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces. Sampath Kumar S, AP

12/28/2018 Cont.., Sampath Kumar S, AP

Defining an Interfaces: 12/28/2018 The interface keyword is used to declare an interface. Access_Specifier interface <name> { Return_type <method_name_1> (parameter_list); type final <variable_name_1>; Return_type <method_name_2> (parameter_list); type final <variable_name_2>; // … Return_type <method_name_N> (parameter_list); type final <variable_name_N>; } Note: The java compiler adds public and abstract keywords before the interface method and public, static and final keywords before data members. Sampath Kumar S, AP

Example : Defining an Interfaces: 12/28/2018 Example : Defining an Interfaces: Sampath Kumar S, AP

12/28/2018 Example: Interface interface IT{ void itClass(); } class thirdIt implements IT{ public void itClass(){ System.out.println(“This is 3rd IT"); public static void main(String args[]){ thirdIt obj = new thirdIt(); obj.itClass(); } Output: This is 3rd IT Sampath Kumar S, AP

Multiple inheritance in Java by interface 12/28/2018 Multiple inheritance in Java by interface If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance Sampath Kumar S, AP

Example: Multiple Inheritance 12/28/2018 Example: Multiple Inheritance interface Printable{ void print(); } interface Showable{ void show(); } class A implements Printable, Showable { public void print(){ System.out.println("Hello"); } public void show(){ System.out.println("Welcome"); } Sampath Kumar S, AP

Example: Cont.., Output: Hello Welcome 12/28/2018 Example: Cont.., public static void main(String args[]){ A obj = new A(); obj.print(); obj.show(); } } Output: Hello Welcome Sampath Kumar S, AP

How Multiple Inheritance possible? 12/28/2018 How Multiple Inheritance possible? interface Printable{ void print(); } interface Showable{ } class A implements Printable,Showable { public void print(){ System.out.println("Hello"); } public static void main(String args[]){ A obj = new A(); obj.print(); } Output: Hello Sampath Kumar S, AP

Example: Extends & Implements 12/28/2018 Example: Extends & Implements interface Printable{ void print(); } interface Showable extends Printable{ void show(); } class A implements Showable{ public void print(){ System.out.println("Hello"); public void show(){ System.out.println("Welcome"); Sampath Kumar S, AP

Example: Cont.., Output: Hello Welcome 12/28/2018 Example: Cont.., public static void main(String args[]){ A obj = new A(); obj.print(); obj.show(); } } Output: Hello Welcome Sampath Kumar S, AP

Marker or Tagged interface 12/28/2018 Marker or Tagged interface An interface that have no member is known as marker or tagged interface. They are used to provide some essential information to the JVM so that JVM may perform some useful operation Example:     public interface Serializable{   }   Sampath Kumar S, AP

12/28/2018 Sampath Kumar S, AP

12/28/2018 Thank You  Sampath Kumar S, AP