0% found this document useful (0 votes)
7 views9 pages

Java Inner Interfaces and Exception Handling

The document discusses InnerInterfaces and InnerAbstractClasses in Java, detailing their declaration within classes, interfaces, and abstract classes, along with examples. It also summarizes programming components such as variables, methods, blocks, constructors, classes, interfaces, and abstract classes. Additionally, it explains the exception handling process, including try, catch, and finally blocks.

Uploaded by

kg666235
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Java Inner Interfaces and Exception Handling

The document discusses InnerInterfaces and InnerAbstractClasses in Java, detailing their declaration within classes, interfaces, and abstract classes, along with examples. It also summarizes programming components such as variables, methods, blocks, constructors, classes, interfaces, and abstract classes. Additionally, it explains the exception handling process, including try, catch, and finally blocks.

Uploaded by

kg666235
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dt : 28/10/2022

*imp

InnerInterfaces in Java:

(i)InnerInterfaces in Classes:

=>we can also declare InnerInterfaces in Classes and which can be

Static member InnerInterfaces or NonStatic member InnerInterfaces.

(ii)InnerInterfaces in Interfaces:

=>we can also declare InnerInterfaces in Interfaces and which are

automatically Static member InnerInterfaces.

(iii)InnerInterfaces in AbstractClasses:

=>we can also declare InnerInterfaces in AbstractClasses and which can

static member InnerInterfaces or NonStatic member InnerInterfaces.

Ex:

[Link]

package test;
public class SubClass {
public interface ITest2{
public abstract void m2(int x);
}//Instance member InnerInterface
public static interface ITest22{
public abstract void m22(int y);
}//Static member InnerInterface
}//OuterClass

[Link]

package test;
public interface ITest {
public static interface ITest3{
public abstract void m3(int a);
}//Static member InnerInterface
}//OuterInterface

[Link]

package test;
public abstract class AClass {
public interface ITest4{
public abstract void m4(int p);
}//Instance member InnerInterface
public static interface ITest44{
public abstract void m44(int q);
}//Static member InnerInterface
}//OuterAbstractClass

[Link](MainClass)

package maccess;
import test.*;
public class DemoInnerInterface {
public static void main(String[] args) {
[Link]("****InnerInterface in Class****");
SubClass.ITest2 ob2 = (int x)->
{
[Link]("====m2(x)===");
[Link]("The value x:"+x);
};
ob2.m2(12);
SubClass.ITest22 ob22 = (int y)->
{
[Link]("====m22(y)===");
[Link]("The value y:"+y);
};
ob22.m22(13);
[Link]("****InnerInterface in Interface****");
ITest.ITest3 ob3 = (int a)->
{
[Link]("====m3(a)===");
[Link]("The value a:"+a);
};
ob3.m3(14);
[Link]("****InnerInterface in
AbstractClass****");
AClass.ITest4 ob4 = (int p)->
{
[Link]("====m4(p)===");
[Link]("The value p:"+p);
};
ob4.m4(15);
AClass.ITest44 ob44 = (int q)->
{
[Link]("====m44(q)===");
[Link]("The value q:"+q);
};
ob44.m44(16);
}
}

o/p:

****InnerInterface in Class****

====m2(x)===

The value x:12

====m22(y)===

The value y:13

****InnerInterface in Interface****

====m3(a)===

The value a:14

****InnerInterface in AbstractClass****

====m4(p)===

The value p:15

====m44(q)===

The value q:16

============================================================
*Imp

InnerAbstractClasses in Java:

(i)InnerAbstractClasses in Class:

=>we can also declare InnerAbstractClasses in Class and which can be

Static member InnerAbstractClass or NonStatic member InnerAbstractClass.

(ii)InnerAbstractClasses in Interfaces:

=>we can also declare InnerAbstractClasses in Interfaces and which are

automatically Static member InnerAbstractClasses.

(iii)InnerAbstractClasses in AbstractClasses:

=>we can also declare InnerAbstractClasses in AbstractClasses and which

can be Static member InnerAbstractClasses or NonStatic member

InnerAbstractClasses.

Ex:(Assignment)

============================================================

Summary of Programming Components:

(a)Variables

[Link] DataType variables(hold Values)

(i)Static(Outside methods)

(ii)NonStatic

=>Instance(Outside methods)
=>Local(Inside methods)

[Link] DataType variable(object references)

(i)Static

(ii)NonStatic

=>Instance

=>Local

(b)Methods

[Link] methods

(i)Pre-defined

(ii)User defined

[Link] methods(Instance methods)

(i)Pre-defined

(ii)User defined

(c)Blocks

[Link] blocks

[Link] blocks(Instance blocks)

(d)Constructors

=>Non-Static Constructor

(e)Classes

[Link] Classes(Only InnerClasses)

[Link] Classes
(f)Interfaces

[Link] Interfaces(Only InnerInterfaces)

[Link] Interfaces

(g)AbstractClasses

[Link] AbstractClasses(Only InnerAbstractClasses)

[Link] AbstractClasses

==================================================================

Comparision Diagram:

==================================================================

*imp

Exception Handling process:

define Exception?

=>The disturbance which is occured from the application is known as


"exception"

define Exception Handling process?

=>The process which is used to handle the exception is known as Exception

Handling Process.

=>we use the following blocks in Exception Handling process:

[Link] block

[Link] block

[Link] block

=>These blocks are executed automatically when the exception is raised.

[Link] block:

=>try block will hold the statements which are going to raise the

exception.

syntax:

try

//statements

behaviour of try block:

=>when exception raised from try block then one object is created for
Exception_type_class and object reference is thrown onto catch block.

[Link] block:

=>catch block will hold object reference thrown from the try block and

the required msg generated from catch block.

syntax:

catch(Exception_type_class ref_var)

//msg

[Link] block:

=>finally block is part of exception handling process,but executed

automatically without depending on exception.

=>In realtime finally block will hold resource closing operations like

IO close,File close,DB close,...

syntax:

finally

//statements

}
Diagram:

====================================================================

Common questions

Powered by AI

The exception handling process described in the document consists of a try block, one or more catch blocks, and a finally block. The try block contains code that might cause an exception, and if an exception occurs, an object is created and thrown to the appropriate catch block. The catch block handles this object and typically logs or responds to the exception . The finally block, however, executes regardless of whether an exception was thrown, often used to close resources such as files or database connections . This structure ensures robust error management by distinctly separating the operations that might fail from those that handle the failure.

In Java programming, interfaces themselves are always abstract and cannot be instantiated, but the document explains that InnerInterfaces within interfaces are static by default because interfaces do not hold instance-specific data . This characteristic is crucial for design patterns that rely on polymorphism, where behaviors are defined in interfaces and shared among various classes. It allows methods within these interfaces to be accessed without an instance, reinforcing the stateless nature typical of interface usage.

The use of lambda expressions to implement InnerInterfaces allows for concise and readable implementations of single-method interfaces, also known as functional interfaces . In the provided implementations, lambda expressions are used to simplify the instantiation of these interfaces, making the code more streamlined and focused on the operation of the methods rather than the mechanics of interface instantiation. It leverages Java's support for functional programming by enabling developers to write more efficient and expressive code.

The document demonstrates practical usage of InnerInterfaces by instantiating them using lambda expressions. For example, in a class, `SubClass.ITest2` is implemented to define a method `m2` which simply logs the received value. Similarly, `ITest.ITest3` in an interface showcases a similar implementation via lambda to log a parameter . These implementations illustrate how InnerInterfaces can be utilized to encapsulate behavior close to their usage context in Java.

Pre-defined methods are built-in methods provided by Java that can be used without modification, such as `Math.abs()` or `System.out.println()`. User-defined methods, in contrast, are created by programmers to perform specific actions tailored to their applications . The document categorizes both types as either static methods, which do not require an instance to be called, or non-static (instance) methods, which do . This distinction highlights the adaptable and extendable nature of Java's method architecture.

InnerAbstractClasses can be declared in classes, interfaces, and abstract classes with varying configurations. In classes, they can be static or non-static, thus tied to the class structure or instance respectively . Within interfaces, they are static by default, as interfaces themselves do not have instance context . In abstract classes, they can again be static or non-static, providing flexibility depending on whether instance-specific behavior is needed . This flexibility allows developers to encapsulate behavior according to their design patterns and requirements.

InnerInterfaces can be declared in classes, interfaces, and abstract classes in Java. In classes, they can either be static or non-static member InnerInterfaces . In interfaces, they are automatically static member InnerInterfaces . In abstract classes, they can also be either static or non-static member InnerInterfaces . Each context provides different scopes and default behaviors for the InnerInterfaces.

Non-static blocks, also known as instance blocks, are executed each time an instance of a class is created, allowing for initialization code that should run every time an object is instantiated. Static blocks, on the other hand, are executed once when the class is loaded, used typically to initialize static variables . This difference implies that non-static blocks are suited for object-specific initialization, while static blocks are appropriate for class-level initialization activities.

Static configurations allow components to be associated with the class itself rather than instances, while non-static components are tied to instances of classes. For instance, static variables and methods can be accessed without creating an instance of the class, in contrast to non-static variables and methods which require an instance . InnerInterfaces and InnerAbstractClasses can also be either static, meaning they belong to their enclosing class, or non-static, meaning they are instance-dependent, offering flexibility in design .

Constructors in Java are used to initialize new objects and are inherently non-static because they operate on the specific instance being created . This non-static nature means that constructors are called every time a new object is instantiated, and they can't be called on the class itself without creating an object. This essential characteristic ensures that each object can be initialized with unique settings or configurations, supporting Java's object-oriented principles.

You might also like