0% found this document useful (0 votes)
0 views

Class Running Notes 12th Oct

The document provides an overview of interfaces in Java, detailing their structure, rules, and coding practices. It explains the differences between abstract and concrete methods, as well as the characteristics of variables declared in interfaces. Additionally, it includes coding examples to illustrate the implementation of interfaces in Java classes.

Uploaded by

kg666235
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Class Running Notes 12th Oct

The document provides an overview of interfaces in Java, detailing their structure, rules, and coding practices. It explains the differences between abstract and concrete methods, as well as the characteristics of variables declared in interfaces. Additionally, it includes coding examples to illustrate the implementation of interfaces in Java classes.

Uploaded by

kg666235
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dt : 12/10/2022

*imp

Interfaces in Java:

=>Interfaces are collection of variables,abstract methods and concrete

methods from Java8 version onwards.

(Upto Java7 version Interfaces are collection of Variables and abstract

i
thi
methods,which means Interface cannot hold concrete methods upto Java7)

ipa
faq:

define abstract methods?


Ma
=>The methods which are declared without method_body are known as abstract

methods.
sh
Structure of abstract methods:
ate

return_type method_name(para_list);
nk

faq:

define concrete methods?


Ve

=>The methods which are declared with method_body are known as Concrete

methods.

Structure of Concrete methods:


return_type method_name(para_list)

//method_body

---------------------------------------------------------------

Coding Rules of Interface:

i
thi
Rule-1 : we use "interface" keyword to declare interfaces.

ipa
syntax:

interface Interface_name

{
Ma
//members

}
sh

Rule-2 : The programming components which are declared in interfaces are


ate

automatically "public"

Note:
nk

=>The programming components which are declared in classes without any

access modifier are considered as "default"


Ve

Rule-3 : Interfaces can be declared with both Primitive DataType variables

Non-Primitive DataType variables

Rule-4 : The variables which are declared in interfaces are automatically


Static and final Variables.

Note:

(i)Static Variables in interfaces will get the memory within the interface

while interface loading and accessed with Interface_name.

(ii)final variables must be initialized with values and once initialized

i
thi
cannot be modified.

ipa
faq:

Can we declare NonStatic variables in interfaces?


Ma
=>No,we cannot declare NonStatic variables in interfaces because the

variables automatically "static".


sh
Rule-5 : The methods which are declared in interfaces are automatically

NonStatic abstract methods.


ate

(There is no concept of static abstract methods)


nk

Rule-6 : Interfaces cannot be Instantiated,which means we cannot create

object for Interfaces.


Ve

Rule-7 : Interfaces are implemented to classes using "implements" keyword

and the classes are known as "Implementation classes".

syntax:
class ImplClass implements Interface

//members

Rule-8 : These implementation classes must construct body for abstract

i
thi
methods of Interface.

Note:

ipa
=>We create object for implementation class and the object is known as

"Implementation Object".
Ma
Rule-9 : Interface canbe declared with any number of abstract methods

without restriction.
sh

Rule-10 : Implementation class must construct body for all abstract methods
ate

of Interface.
nk

Rule-11 : Implementation classes can also be declared with Non-Implemented

methods.
Ve

Diagram:
i
thi
ipa
Ex: Ma
ITest.java

package test;
public interface ITest {
int k=30;
sh
void m(int x);
void dis();
}
ate

IClass.java
nk

package test;
public class IClass implements ITest{
public void m(int x)//Implemented and Overriding method
{
Ve

System.out.println("===m(x)====");
System.out.println("The value x:"+x);
}
public void dis()//Implemented and Overriding method
{
System.out.println("====dis()====");
System.out.println("The value k:"+k);
}
public void show(int z)//Non-Implemented method
{
System.out.println("====show(z)====");
System.out.println("The value z:"+z);
}
}

DemoInterface1.java(MainClass)

package maccess;
import test.*;
public class DemoInterface1 {
public static void main(String[] args) {

i
thi
System.out.println("The value k:"+ITest.k);
//ITest.k=300;//Error
//ITest ob = new ITest();//Error
IClass ob = new IClass();//Imple

ipa
ob.m(123);
ob.dis();
ob.show(124);
} Ma
}

o/p:

The value k:30


sh
===m(x)====
ate

The value x:123

====dis()====
nk

The value k:30

====show(z)====
Ve

The value z:124

----------------------------------------------------------------------

You might also like