Java Interfaces

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

An ______ method cannot be made final or static.

interface

In Java, a class can implement multiple ______, addressing the absence of multiple inheritance of class behavior.

interfaces

Unlike classes, ______ are always completely abstract, containing only method signatures but no implementation.

interfaces

While classes can inherit from only one superclass, a Java class can ______ as many interfaces as possible.

<p>implement</p> Signup and view all the answers

The standard input stream ______ is used to read characters from the keyboard or any other standard input device.

<p>System.in</p> Signup and view all the answers

Specialized stream classes like ______ are designed to automatically handle Unicode conventions when reading or writing character data.

<p>FileReader</p> Signup and view all the answers

In Java, dividing a program into multiple processes that can run in parallel is achieved through ______ which enables multiple flows of control.

<p>multithreading</p> Signup and view all the answers

A key advantage of Java threads is that they are ______, meaning they share the same memory space and can easily communicate.

<p>lightweight</p> Signup and view all the answers

Unlike Java Applications, Java ______ are specifically designed to be embedded into a web page for execution within a web browser.

<p>Applets</p> Signup and view all the answers

______ is a Java API used to connect to and execute queries against databases, allowing Java applications to interact with structured data.

<p>JDBC</p> Signup and view all the answers

Flashcards

Interface (Java)

A special kind of class in Java that contains methods and variables, similar to classes, but always completely abstract.

Multi-threading

A programming paradigm that allows a program to be divided into multiple processes that can run in parallel.

System.in

A class in Java used to read characters from the keyboard or any other standard input device.

System.out

A class in Java that produces the result of a program on an output device like the computer screen.

Signup and view all the flashcards

System.err

A class in Java that outputs all the error data that a program might throw, on a computer screen or any standard output device.

Signup and view all the flashcards

JDBC

A Java API to connect and execute queries with databases.

Signup and view all the flashcards

Input Stream

Streams used to read data that must be taken as an input from a source array or file or any peripheral device.

Signup and view all the flashcards

Output Stream

These streams are used to write data as outputs into an array or file or any output peripheral device.

Signup and view all the flashcards

Study Notes

Interfaces in Java

  • Interfaces in Java, are unique to the language, and play a role similar to multiple-inheritance although Java has no concept of multiple inheritance
  • Interfaces in Java are a special kind of class that contain methods and variables
  • Interfaces are always completely abstract unlike classes which are not
  • Multiple inheritance is not allowed in Java because of the diamond problem
  • Interfaces are defined like a class except the keyword interface is used instead of class
  • Member elements are static and final
  • While methods are defined, the code to implement the method is not
  • Classes inherit from only one superclass
  • Classes implement as many interfaces as possible
  • After defining an interface, it is required to implement it with class
  • Teacher and Student are two classes each with their own unique components
  • The CV interface includes all the components both in Teacher and Student
  • In this example, the interface CV is declared and is implemented by Teacher and Students classes

More about Interfaces

  • Interface methods are abstract methods appearing in the interface
  • Interface methods cannot be final or static
  • An interface cannot implement another interface
  • An Interface does not have constructors
  • An interface cannot implement another class (abstract or non-abstract)
  • Interfaces can inherit as many other interfaces

I/O Stream Basics

  • Programs access external data from an input source to accomplish tasks
  • Input sources include:
    • Disk files
    • Keyboards
    • Network sockets
  • Program results are sent to an output destination
  • Output destinations are:
    • Disk files
    • Screens
    • Network connections
  • Abstractions in I/O streams simplify input/output, eliminating distinctions between keyboard and network

Java I/O Streams

  • Java provides various I/O streams via its I/O package, enabling input-output operations
  • These streams support all types of objects, data types, characters, and files
  • There are three standard or default streams:
    • System.in: Standard input stream for reading characters from the keyboard or other input devices.
    • System.out: Standard output stream to produce output on the computer screen.
    • System.err: Standard error stream for outputting error data to the screen.
  • Streams are divided into two primary classes: Input Stream and Output Stream, based on the type of operations
    • InputStream: Used to read data from a source array, file, or peripheral device, like FileInputStream
    • OutputStream: Writes data as output into a file or array. For example, FileOutputStream

Input Output Stream Types

  • Streams can be divided into two primary classes depending on the type of file:
    • ByteStream: Processes data byte by byte (8 bits) with classes like FileInputStream and FileOutputStream for reading and writing to a destination
    • CharacterStream: Uses Unicode for character storage. Character streams allow reading/writing character by character. FileReader and FileWriter read from and write to the source and destination respectively

Multithreading: Concepts and Advantages

  • Multithreading enables multiple flows of control and helps divide a program into processes that run in parallel
  • Multithreading is an approach to programming
  • Multithreading's benefits include:
    • Efficient utilization of system resources
    • Useful for solving particular problems

Java Multithreading Details

  • Java is intrinsically multi-threaded, it allow program execution to occur concurrently in a single execution
  • Java threads often called lightweight threads run in the same memory space
  • They share the same memory space, Java threads can easily communicate

Multitasking Types

  • Multitasking has 2 enabled ways:
    • Process-Based Multitasking: heavyweight, each process gets unique memory area
    • Thread-Based Multitasking: lightweight, threads get the same address space Communication costs are lower

Creating Threads

  • Threads can be created by two mechanisms:
    • Extending the Thread class
    • Implementing the Runnable Interface
  • If extending the Thread class, one cannot extend other classes due to lack of multiple inheritance, which interfaces do support
  • Extending the Thread class provides basic thread functionality with in-built methods

Thread Class-based Thread Creation

  • Create a class that extends the java.lang.Thread class
  • This overrides a thread class's run() method, starting a thread's activity in run()
  • Instantiate the new class, call start() to begin the thread with the run() instance on the thread object

Runnable Interface-based Thread Creation

  • Make a class that implements the java.lang.Runnable interface and override the run method
  • Then make a Thread object
  • Call the object's start() method

Thread Lifecycles

  • Threads go through multiple states during their lifetime:
    • New State: Newly created; execution not started
    • Active State: A thread that invokes the start() method
      • Runnable State: The thread is ready to run anytime.
      • Running State: The thread receives CPU processes and moves to the Running state
    • Waiting/Blocked State: A thread is temporarily inactive
    • Timed Waiting State: Each thread has a particular time period. To keep threads running for a time the sleep() is invoked
    • Terminated State: Reasons for termination are:
      • Task completion
      • Unusual events
      • A terminated thread is dead and no longer available

Core Java Application Development

  • Java uses Java to develop enterprise, network, web, desktop, android, and games
  • Java develops applets
  • Java application are like an ordinary Java program but powered by a virtual machine
  • Java Applets are Java programs which have the capability of being embedded in a web page, and work client side

Definitions

  • Java Application are executed independently without a Web browser
  • Java Applets are designed to be included with an HTML web document which requires a Java-enabled web browser for execution.

Method Requirements

  • Java Applications require a main() for execution
  • Java Applets do not require the main() method for execution but instead the init() method to run.

Compilation Requirements

  • The javac command is used to compile Java Application programs
  • The javac command is used to compile Java Applet programs and run using the appletviewer command

Access

  • Java Applications have full access to the file system and network
  • Java Applets not have local disk and network access.

Development platforms

Java Application Java Applet
Access level Can access all types of resources Access browser-specific services. No operating system access
Installation Installation is required before executing the Java program Not required
Execution Executes programs from the local system Excecution is not possible from the local System

Abstract Window Toolkit

  • AWT, the Abstract Window Toolkit, provides a GUI (Graphical User Interface) in Java so Java programmers develop apps
  • AWT delivers objects such as labels, checkbox, buttons
  • AWT components are platform-dependent; component's view adapts to operating system
  • Java.awt delivers classes for AWT components

AWT Fundamentals

  • Component: Elements like buttons, text fields, controls in Java AWT and require addition to a container to positions these components on the screen
  • Container: A container in AWT that hold components like textfields, buttons etc. These extend through Window, Dialog, Panel, Frame classes
  • Panel: The Panel is the container that doesn't contain title bar, border or menu bar. An instance creates a container to which we add components
  • Window: A container lacking borders, bars and must be created through Frame, Dialog, or a custom Window
  • Frame: contains border/title bar and menu with a most widely used container while coding an AWT app

Swing in Java

  • Java Swing belongs to JFC (Java Foundation Classes) it helps create windowed applications.
  • Java Swing is totally Java coded above AWT
  • Java Swing offers lightweight and independent components, with platform independent coding unlike AWT
  • javax.swing generates classes for JCheckbox, JTextArea, JMenu, among other components. JFC has its power in Swing for JMenu.

Core Differences between AWT and Swing

Java AWT Java Swing
1 Platform Dependent Platform independent
2 Heavweight Light weight
3 does not support pluggable look and feel Supports pluggable look and feel
4 Has less components A lot of components
5 Doesn't follow MVC Follows MVC

Java Database Connectivity (JDBC)

  • Java programmers connect with JDBC, a Java API to enable query executions and connections to the database
  • Sun Microsystems produced specs for Java Apps that are database-connected
  • JDBC aids programmers to code db interfacing, allowing db operations with request creation

JDBC Requirements

  • Applications use JAVA EE to speak to databases for storing things needed in the application
  • The database connectivity requires a driver, such as ODBC to enable database interfacing.
  • JDBC is what the driver works through for MySQL, Oracle,SQL, MS Access.

JDBC Architecture

  • Consists of layers
    • Applications are based on JAVA EE (servlets or applets) to connect with data to the JDBC layer, which has 2 components
      • DriverManager manages connecting to JDBC, with drivers that handle data.

JDBC API description

  • Application: A java program that communicates with a data source.
  • The important classes and interfaces defined in JDBC API are:
    • DriverManager: It is the most important JDBC class and uses the database-specific drivers to connect enterprise-applications to databases.
    • JDBC drivers: To communicate with a data source through JDBC, you need a JDBC driver that communicates with the respective data source.

Building a Swing and JDBC platform

  • JDBC allows application connections and generation of Swing-database application through many steps
  • Creating a MySQL DB as a Database
  • Adding MySQL JDBC driver library to the Java program as MySQL JBDC driver is a JAR
  • Generates interface by creating interfaces with Swing
  • Establishs JDBC operations with the following code Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo","root","root");

Lectures, Resources and Labs

  • Course resources are available at:
    • Java Points
    • W3 Schools
    • Oracle
    • Geeks for Geeks

Summaries

  • Lectures 1-5 cover basic OOP- Object Oriented Programming
  • Lecture 6 covers advanced OOP- interfaces, I/O handling, multithreading.
  • Labs cover fundamentals, more OOP and more
  • The labs can cover three things which are fundamentals, OOP implementation and App Development.
  • Labs 1-3 Fundamentals contains variables, conditions, loops and arrays.
  • Labs 4-7 on OOP implementation contain classes,objects, lists, maps, exception handling, inheritance and polymorphism.
  • Labs 8-10 are for App development Spring Boot.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

OOP Concepts and UML
30 questions

OOP Concepts and UML

RefinedBowenite avatar
RefinedBowenite
Java Interfaces Quiz
10 questions
Java's Approach to Multiple Inheritance
5 questions
Java Interfaces
10 questions

Java Interfaces

StylishSpessartine avatar
StylishSpessartine
Use Quizgecko on...
Browser
Browser