Abstract classes allow for defining a generalized superclass that leaves implementation details to subclasses. An abstract class can define methods that subclasses must implement. Abstract classes cannot be instantiated and exist solely to be extended. Interfaces define behaviors and properties but leave implementation to classes that implement the interface. Abstract classes can contain both abstract and concrete methods while interfaces can only contain abstract methods. Both allow for achieving abstraction and multiple inheritance in Java.
- An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. A class implements an interface by providing method bodies for the abstract methods defined in the interface. Interfaces allow for loose coupling between classes.
- The main reasons to use interfaces are for abstraction, to support multiple inheritance functionality, and to achieve loose coupling between classes. An interface is declared using the interface keyword and contains only method signatures, not method bodies. A class implementing an interface must implement all of its methods.
- Interfaces can extend other interfaces, requiring implementing classes to provide implementations for all methods in the inherited interfaces as well. Object cloning uses the clone()
The document outlines the course units for an Advanced Java Programming course. Unit 1 covers classes, interfaces, inheritance and encapsulation. Unit 2 discusses methods, method overloading, overriding and recursion. Unit 3 focuses on exceptions, threads and immutability. Unit 4 covers dynamic language support and APIs. Unit 5 is about Java annotation processors and agents.
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
In my presentation titled "Abstraction in Java," I have discussed the fundamental concept of abstraction in Java programming. The presentation delves into how abstraction is a key principle in object-oriented programming, explaining its role in hiding the complexity of code while exposing only the necessary details. It includes examples and explanations on implementing abstraction in Java, offering a clear understanding for both beginners and intermediate learners. This presentation serves as an educational guide for those interested in enhancing their Java programming skills and understanding the practical applications of abstraction in software development.
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
multiple inheritance in java, interface implementation, abstraction,
multiple inheritance in java using interface, how to use interface,
how to use java, how to execute a java code
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface cannot have method bodies and can only contain abstract methods and variables that are public, static, and final by default. Classes implement interfaces to inherit their methods. Since Java 8, interfaces can also contain default and static methods.
This document discusses interfaces and packages in Java. It defines an interface as a special Java class that is fully abstract and contains only static final variables and abstract methods. Interfaces can extend other interfaces but not classes. Classes can implement multiple interfaces but extend only one class. Packages in Java organize related classes and interfaces. The Java API contains predefined packages like java.lang and java.util while user-defined packages can be created using the package keyword.
Interfaces provide abstraction and allow for multiple inheritance in Java. They declare methods that classes implementing the interface must define. Interfaces are useful for achieving loose coupling between classes. Some key points about interfaces include:
- Interfaces can only contain public abstract methods and constants, no implementation code.
- Classes implement interfaces to inherit their methods while also allowing inheritance from other classes.
- Interfaces allow Java to support multiple inheritance of interface methods while only single inheritance of classes is supported.
- Common interfaces are used for graphics and user interfaces to provide reusable classes for building interactive applications.
Interfaces define methods that classes can implement. Classes implementing interfaces must define all interface methods. Interfaces can extend other interfaces, requiring implementing classes to define inherited methods as well. Interface variables are implicitly public, static, and final. A class can implement multiple interfaces and override methods with the same name across interfaces. Partial interface implementation requires the class to be abstract.
The document discusses reflection in programming, specifically in Java. It provides an overview of reflection, its advantages like enabling polymorphism and creating adaptable code, and its disadvantages like reduced performance and increased complexity. It also discusses proper uses of reflection, like in development tools, and misuses, like when interfaces could enable the same functionality more cleanly. An example is provided to demonstrate how to use reflection in Java to inspect class details at runtime.
Java is a widely-used, object-oriented programming language known for its simplicity, portability, and reliability. At the core of Java are classes and objects, with classes serving as blueprints for creating objects that encapsulate data and behaviors. The entry point of any Java program is the main method, where execution begins. Java has a variety of data types, including primitive types like int, float, char, and boolean, as well as objects like String. Control flow is managed using conditional statements such as if-else and switch, as well as loops like for, while, and do-while to iterate over data. Arrays allow the storage of multiple values in a single variable, and methods are used to perform specific tasks or operations. Java is built around the principles of object-oriented programming (OOP), which includes encapsulation (hiding internal data), inheritance (deriving new classes from existing ones), polymorphism (using methods in different ways), and abstraction (simplifying complex systems). Exception handling in Java is done using try, catch, and finally blocks, providing a robust mechanism for managing errors. Java also includes packages for organizing code and provides tools for input and output operations, such as the Scanner class for reading user input. Overall, Java's combination of simplicity, powerful features, and cross-platform capabilities makes it a popular choice for building a wide range of applications, from web and mobile apps to enterprise-level systems.
You said:
java basics long paragraph with 3000 characters
ChatGPT said:
ChatGPT
Java is a powerful, high-level, object-oriented programming language widely used for developing various types of applications, ranging from web applications and mobile apps to enterprise systems and large-scale server applications. One of the key features of Java is its platform independence, made possible by the Java Virtual Machine (JVM), which allows Java programs to run on any device that has the JVM installed. The foundational concept in Java programming is the use of classes and objects. A class serves as a blueprint or template for creating objects, which are instances of that class. Each object can have attributes (fields) and behaviors (methods). The main method is the entry point of any Java program, where the program starts its execution. For example, a simple Java program starts by defining a class with a main method that contains the instructions to be executed. Java supports a rich set of data types including primitive types such as int, float, char, boolean, and double, along with more complex types like String, arrays, and user-defined objects. These data types are essential for storing and manipulating data throughout the program.
Control flow in Java is managed using conditional statements and loops. The if-else statement is commonly used for making decisions based on conditions, while the switch statement is helpful when dealing with multiple potential conditions base
Multiple inheritance allows a class to inherit properties and methods from more than one parent class or interface. In Java, a class can only extend one superclass but can implement multiple interfaces. Interfaces provide an alternative to multiple inheritance by allowing classes to inherit behaviors from multiple sources. Variables defined in interfaces are implicitly public, static, and final.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface can only contain abstract methods without a method body. A class implementing an interface must implement all the methods declared in the interface. Interfaces allow for achieving loose coupling between classes.
The document discusses abstract classes and interfaces in Java. It defines abstract classes as classes that can have both abstract and non-abstract methods but cannot be instantiated. Abstract methods are declared without an implementation. Interfaces are collections of abstract methods that classes implement, inheriting the interface's behaviors. The Object class is the implicit superclass of all other classes in Java. It contains methods like getClass(), notify(), and wait() that are commonly used.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. A class implements an interface to provide method body implementations. Key reasons for using interfaces are abstraction, supporting multiple inheritance, and loose coupling between classes. An interface is declared with the interface keyword and contains only abstract methods. A class implementing an interface must implement all of its methods.
1. An interface in Java is a blueprint of a class that defines static constants and abstract methods. It allows for abstraction and multiple inheritance in Java.
2. An interface contains only abstract method signatures and static final variables, while classes implement interfaces by providing method bodies.
3. Interfaces are used to achieve abstraction, loose coupling between classes, and multiple inheritance in cases where a class implements multiple interfaces.
Interface in java By Dheeraj Kumar Singhdheeraj_cse
In Java,
An interface is a way through which unrelated objects use to interact with one another.
Using interface, you can specify what a class must do, but not how it does it.
It is not a class but a set of requirements for classes that implement the interface.
1. Inheritance in Java allows classes to extend other classes and interfaces to implement other interfaces. This allows code reuse and establishes type constraints.
2. Abstract classes can contain both abstract and non-abstract methods while interfaces contain only abstract methods. Interfaces establish contracts that implementing classes must follow.
3. When constructing objects with inheritance, superclass constructors are called before subclass constructors. Abstract classes and interfaces allow incomplete implementations to be extended.
The document discusses object-oriented programming concepts of inheritance, interfaces, and abstract classes. It defines inheritance as allowing hierarchical classifications by inheriting variables, methods, properties, and indexers from a base class. It describes different types of inheritance like single, hierarchical, multi-level, hybrid, and multiple. It then explains the differences between interfaces and abstract classes, such as interfaces defining functionality without implementation and abstract classes allowing partial implementation.
Vbnnmhvbbnnnfcfcgvsbsbnssbbsbsnsbbssbbsbsbssbbsbsbsbsbsbsbdbsbsbsbbbbnnhjzjshdhshsshhsbdbxbxbxxbbcnxnxncncncncncnccnbbbhahhehehehenendhdhdhdbfnffnñnfjdjejejhegegehebebehehehehehehehebeebhehejdjdejjrejrjekjdndbdbddbbdnrbnnnnsnsnhshsil feb cagoudgjvzcczhfHafhsfjgsjgjsjfszfbfbzfsbfzbfzbcbzxbczbzfbzvczgjjscfsjgdjhzdgjjgsvnzgjzzgjjzgzvnzgjv
This document summarizes a presentation on inheritance of interfaces in Java. It defines an interface as a blueprint of a class that contains abstract methods. Interfaces allow classes to support multiple inheritance by implementing multiple interfaces. The document provides the syntax for declaring an interface and explains that a class implements an interface while a class extends another class. It gives an example of a Drawable interface being implemented by Rectangle and Circle classes. Finally, it demonstrates multiple inheritance using interfaces by defining a Printable and Showable interface both being implemented by class A7.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. A class implements an interface by providing method bodies for the abstract methods defined in the interface.
Interfaces in Java declare methods but do not provide method implementations. A class can implement multiple interfaces but extend only one class. Interfaces are used to define common behaviors for unrelated classes and allow classes to assume multiple roles. Nested interfaces group related interfaces and must be accessed through the outer interface or class.
computer organization and assembly language : its about types of programming language along with variable and array description..https://www.nfciet.edu.pk/
Ad
More Related Content
Similar to INTERFACES. with machine learning and data (20)
Interfaces provide abstraction and allow for multiple inheritance in Java. They declare methods that classes implementing the interface must define. Interfaces are useful for achieving loose coupling between classes. Some key points about interfaces include:
- Interfaces can only contain public abstract methods and constants, no implementation code.
- Classes implement interfaces to inherit their methods while also allowing inheritance from other classes.
- Interfaces allow Java to support multiple inheritance of interface methods while only single inheritance of classes is supported.
- Common interfaces are used for graphics and user interfaces to provide reusable classes for building interactive applications.
Interfaces define methods that classes can implement. Classes implementing interfaces must define all interface methods. Interfaces can extend other interfaces, requiring implementing classes to define inherited methods as well. Interface variables are implicitly public, static, and final. A class can implement multiple interfaces and override methods with the same name across interfaces. Partial interface implementation requires the class to be abstract.
The document discusses reflection in programming, specifically in Java. It provides an overview of reflection, its advantages like enabling polymorphism and creating adaptable code, and its disadvantages like reduced performance and increased complexity. It also discusses proper uses of reflection, like in development tools, and misuses, like when interfaces could enable the same functionality more cleanly. An example is provided to demonstrate how to use reflection in Java to inspect class details at runtime.
Java is a widely-used, object-oriented programming language known for its simplicity, portability, and reliability. At the core of Java are classes and objects, with classes serving as blueprints for creating objects that encapsulate data and behaviors. The entry point of any Java program is the main method, where execution begins. Java has a variety of data types, including primitive types like int, float, char, and boolean, as well as objects like String. Control flow is managed using conditional statements such as if-else and switch, as well as loops like for, while, and do-while to iterate over data. Arrays allow the storage of multiple values in a single variable, and methods are used to perform specific tasks or operations. Java is built around the principles of object-oriented programming (OOP), which includes encapsulation (hiding internal data), inheritance (deriving new classes from existing ones), polymorphism (using methods in different ways), and abstraction (simplifying complex systems). Exception handling in Java is done using try, catch, and finally blocks, providing a robust mechanism for managing errors. Java also includes packages for organizing code and provides tools for input and output operations, such as the Scanner class for reading user input. Overall, Java's combination of simplicity, powerful features, and cross-platform capabilities makes it a popular choice for building a wide range of applications, from web and mobile apps to enterprise-level systems.
You said:
java basics long paragraph with 3000 characters
ChatGPT said:
ChatGPT
Java is a powerful, high-level, object-oriented programming language widely used for developing various types of applications, ranging from web applications and mobile apps to enterprise systems and large-scale server applications. One of the key features of Java is its platform independence, made possible by the Java Virtual Machine (JVM), which allows Java programs to run on any device that has the JVM installed. The foundational concept in Java programming is the use of classes and objects. A class serves as a blueprint or template for creating objects, which are instances of that class. Each object can have attributes (fields) and behaviors (methods). The main method is the entry point of any Java program, where the program starts its execution. For example, a simple Java program starts by defining a class with a main method that contains the instructions to be executed. Java supports a rich set of data types including primitive types such as int, float, char, boolean, and double, along with more complex types like String, arrays, and user-defined objects. These data types are essential for storing and manipulating data throughout the program.
Control flow in Java is managed using conditional statements and loops. The if-else statement is commonly used for making decisions based on conditions, while the switch statement is helpful when dealing with multiple potential conditions base
Multiple inheritance allows a class to inherit properties and methods from more than one parent class or interface. In Java, a class can only extend one superclass but can implement multiple interfaces. Interfaces provide an alternative to multiple inheritance by allowing classes to inherit behaviors from multiple sources. Variables defined in interfaces are implicitly public, static, and final.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. An interface can only contain abstract methods without a method body. A class implementing an interface must implement all the methods declared in the interface. Interfaces allow for achieving loose coupling between classes.
The document discusses abstract classes and interfaces in Java. It defines abstract classes as classes that can have both abstract and non-abstract methods but cannot be instantiated. Abstract methods are declared without an implementation. Interfaces are collections of abstract methods that classes implement, inheriting the interface's behaviors. The Object class is the implicit superclass of all other classes in Java. It contains methods like getClass(), notify(), and wait() that are commonly used.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. A class implements an interface to provide method body implementations. Key reasons for using interfaces are abstraction, supporting multiple inheritance, and loose coupling between classes. An interface is declared with the interface keyword and contains only abstract methods. A class implementing an interface must implement all of its methods.
1. An interface in Java is a blueprint of a class that defines static constants and abstract methods. It allows for abstraction and multiple inheritance in Java.
2. An interface contains only abstract method signatures and static final variables, while classes implement interfaces by providing method bodies.
3. Interfaces are used to achieve abstraction, loose coupling between classes, and multiple inheritance in cases where a class implements multiple interfaces.
Interface in java By Dheeraj Kumar Singhdheeraj_cse
In Java,
An interface is a way through which unrelated objects use to interact with one another.
Using interface, you can specify what a class must do, but not how it does it.
It is not a class but a set of requirements for classes that implement the interface.
1. Inheritance in Java allows classes to extend other classes and interfaces to implement other interfaces. This allows code reuse and establishes type constraints.
2. Abstract classes can contain both abstract and non-abstract methods while interfaces contain only abstract methods. Interfaces establish contracts that implementing classes must follow.
3. When constructing objects with inheritance, superclass constructors are called before subclass constructors. Abstract classes and interfaces allow incomplete implementations to be extended.
The document discusses object-oriented programming concepts of inheritance, interfaces, and abstract classes. It defines inheritance as allowing hierarchical classifications by inheriting variables, methods, properties, and indexers from a base class. It describes different types of inheritance like single, hierarchical, multi-level, hybrid, and multiple. It then explains the differences between interfaces and abstract classes, such as interfaces defining functionality without implementation and abstract classes allowing partial implementation.
Vbnnmhvbbnnnfcfcgvsbsbnssbbsbsnsbbssbbsbsbssbbsbsbsbsbsbsbdbsbsbsbbbbnnhjzjshdhshsshhsbdbxbxbxxbbcnxnxncncncncncnccnbbbhahhehehehenendhdhdhdbfnffnñnfjdjejejhegegehebebehehehehehehehebeebhehejdjdejjrejrjekjdndbdbddbbdnrbnnnnsnsnhshsil feb cagoudgjvzcczhfHafhsfjgsjgjsjfszfbfbzfsbfzbfzbcbzxbczbzfbzvczgjjscfsjgdjhzdgjjgsvnzgjzzgjjzgzvnzgjv
This document summarizes a presentation on inheritance of interfaces in Java. It defines an interface as a blueprint of a class that contains abstract methods. Interfaces allow classes to support multiple inheritance by implementing multiple interfaces. The document provides the syntax for declaring an interface and explains that a class implements an interface while a class extends another class. It gives an example of a Drawable interface being implemented by Rectangle and Circle classes. Finally, it demonstrates multiple inheritance using interfaces by defining a Printable and Showable interface both being implemented by class A7.
An interface in Java is a blueprint of a class that defines static constants and abstract methods. Interfaces are used to achieve abstraction and multiple inheritance in Java. A class implements an interface by providing method bodies for the abstract methods defined in the interface.
Interfaces in Java declare methods but do not provide method implementations. A class can implement multiple interfaces but extend only one class. Interfaces are used to define common behaviors for unrelated classes and allow classes to assume multiple roles. Nested interfaces group related interfaces and must be accessed through the outer interface or class.
computer organization and assembly language : its about types of programming language along with variable and array description..https://www.nfciet.edu.pk/
By James Francis, CEO of Paradigm Asset Management
In the landscape of urban safety innovation, Mt. Vernon is emerging as a compelling case study for neighboring Westchester County cities. The municipality’s recently launched Public Safety Camera Program not only represents a significant advancement in community protection but also offers valuable insights for New Rochelle and White Plains as they consider their own safety infrastructure enhancements.
Mieke Jans is a Manager at Deloitte Analytics Belgium. She learned about process mining from her PhD supervisor while she was collaborating with a large SAP-using company for her dissertation.
Mieke extended her research topic to investigate the data availability of process mining data in SAP and the new analysis possibilities that emerge from it. It took her 8-9 months to find the right data and prepare it for her process mining analysis. She needed insights from both process owners and IT experts. For example, one person knew exactly how the procurement process took place at the front end of SAP, and another person helped her with the structure of the SAP-tables. She then combined the knowledge of these different persons.
3. Single Inheritance
In single inheritance, a sub-class is derived from only one super
class.
It inherits the properties and behavior of a single-parent class.
A – Parent class
B – child class
4. Sample program : Write a java program to illustrate the concept of single inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
// Parent class
class One {
void display1()
{
System.out.println(“MEPCO");
}
}
class Two extends One {
public void display2()
{
System.out.println(“JAVA"); }
}
// Driver class
class Main {
public static void main(String[] args)
{
Two g = new Two();
g.display1();
g.display2();
}
}
5. Multilevel Inheritance
• In Multilevel Inheritance, a derived class will be inheriting a
base class, and as well as the derived class also acts as the
base class for other classes
6. Write a simple java program to illustrate the concept of Multilevel inheritance :
class Name {
public void print_firstname()
{
System.out.println(“Mepco");
}
}
Class Middlename extends Name {
public void print_middlename() {
System.out.println(“Schnlek");
}
}
class Lastname extends Middlename{
public void print_lastname() {
System.out.println(“College");
}
}
// Driver class
public class Main {
public static void main(String[] args) {
Lastname g = new Lastname();
Calling method from class One
g.firstname();
g.middlename();
g.lastname();
}
}
7. Hierarchical Inheritance
• In Hierarchical Inheritance, one class serves as a superclass
(base class) for more than one subclass. In the below image,
class A serves as a base class for the derived classes B, C, and
D.
8. Write a simple java program to illustrate the concept of Hierarchial inheritance :
class A {
public void print_A() { System.out.println("Class A"); }
}
class B extends A {
public void print_B() { System.out.println("Class B"); }
}
class C extends A {
public void print_C() { System.out.println("Class C"); }
}
class D extends A {
public void print_D() { System.out.println("Class D"); }
}
public class Test {
public static void main(String[] args)
{ B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
C obj_C = new C();
obj_C.print_A();
obj_C.print_C();
D obj_D = new D();
obj_D.print_A();
obj_D.print_D();
}
}
9. Multiple Inheritance :
• In Multiple inheritances, one class can have more than one
superclass and inherit features from all parent classes.
• Please note that Java does not support multiple inheritances
with classes.
• In Java, we can achieve multiple inheritances only through
Interfaces.
10. What is Interfaces ?
The interface in Java is a mechanism to achieve abstraction.
An interface could only have abstract methods (methods
without a body) and public, static, and final variables by
default.
It is used to achieve abstraction and multiple inheritances in
Java.
In other words, interfaces primarily define methods that
other classes must implement.
12. Syntax :
interface {
// declare constant fields
// declare methods that abstract
// by default.
}
In other words,
13. Rules to declare “interface” class :
• That means all the methods in an interface are declared
with an empty body and are public and all fields are public,
static, and final by default.
• A class that implements an interface must implement all
the methods declared in the interface.
• To implement the interface, use the implements keyword.
15. Concept #1:
• We can have method body in interface. But we need to
make it default method. Let's see an example:
16. interface Interface1 {
default void show()
{
System.out.println(“Greetings!!!");
}
}
interface Interface2 extends Interface1 {
// Abstract method
void display();
}
// Interface 3
interface Interface3 extends Interface1 {
// Abstract method
void print();
}
// Main class
class TestClass implements Interface2, Interface3 {
// Overriding the abstract method from Interface2
public void display()
{ System.out.println(“Hello everyone");
}
• // Overriding the abstract method from Interface3
• public void print()
• {
• System.out.println(“WELOME TO JAVA WORLD");
• }
• // Main driver method
• public static void main(String args[])
• {
• TestClass d = new TestClass();
• // Now calling the methods from both the interfaces
• d.show(); // Default method from API
• d.display(); // Overridden method from Interface1
• d.print(); // Overridden method from Interface2
• }
• }
•
17. Concept #2:
We can have static method in Interface and it is allowed to use it in
Java.
18. Concept 3 : Static Method in Interface
interface Drawable
{
void draw();
static int cube(int x)
{
return x*x*x;
}
}
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle"
);}
}
class A{
public static void main(String args[])
{
Drawable d=new Rectangle();
d.draw();
19. Points to remember :
• One interface can inherit another by the use of keyword
“extends”.
• When a class implements an interface that inherits another
interface, it must provide an implementation for all methods
required by the interface inheritance chain.
• A class implements an interface, but one interface extends another
interface.
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple
inheritance.
20. Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".
8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
9)Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();
}
21. Points
Abstract Class Interface
Type of Methods Can have both abstract and concrete methods
Can have only abstract methods (until Java 7), and from Java 8,
can have default and static methods, and from Java 9, can
have private methods.
Variables Can have final, non-final, static, and non-static variables. Only static and final variables
Inheritance Can extend only one class (abstract or not) A class can implement multiple interfaces
Constructors Can have constructors Cannot have constructors
Implementation Can provide the implementation of interface methods Cannot provide the implementation of abstract class methods
22. Practice program 1 :
Create a Drawable interface has only one method “draw”. Its
implementation is provided by Rectangle and Circle classes.
23. Source code :
1. interface Drawable{
2. void draw();
3. }
4. //Implementation: by second user
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class Circle implements Drawable{
9. public void draw(){System.out.println("drawing circle");}
10. }
11. //Using interface: by third user
12. class TestInterface1{
13. public static void main(String args[]){
14. Drawable d=new Circle();
15. d.draw();
16. }}
24. interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw()
{
System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}}
25. Practice program 2 :
Let's see another example of java interface which provides
the implementation of Bank interface.
26. 1. interface Bank{
2. float rateOfInterest();
3. }
4. class SBI implements Bank{
5. public float rateOfInterest(){return 9.15f;}
6. }
7. class PNB implements Bank{
8. public float rateOfInterest(){return 9.7f;}
9. }
10.
class TestInterface2{
11.
public static void main(String[] args){
12.
Bank b=new SBI();
13.
System.out.println("ROI: "+b.rateOfInterest());
14.
}}