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

hlo1

The document discusses inheritance in real-time application development, categorizing it into single and multiple inheritance, with a focus on Java's interface implementation. It explains the characteristics and rules of interfaces in Java, including the distinction between implemented and non-implemented methods, and highlights the introduction of concrete methods in interfaces from Java 8 onwards. Additionally, it provides examples of interface implementation and execution flow in Java applications.

Uploaded by

MONICA NAHAK
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)
5 views

hlo1

The document discusses inheritance in real-time application development, categorizing it into single and multiple inheritance, with a focus on Java's interface implementation. It explains the characteristics and rules of interfaces in Java, including the distinction between implemented and non-implemented methods, and highlights the introduction of concrete methods in interfaces from Java 8 onwards. Additionally, it provides examples of interface implementation and execution flow in Java applications.

Uploaded by

MONICA NAHAK
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
You are on page 1/ 15

Dt : 22/8/2023

Note:

=>In realtime application development,the inheritances are categorized

into two types:

1.Single Inheritance

ii
2.Multiple Inheritance

ath
1.Single Inheritance:

aip
=>The process of taking the features(components) from one class at-a-time

is known as Single Inheritance.


hM
Ex:

above programs
tes

2.Multiple Inheritance:

=>The process of taking the features(components) from more than one


a
nk

class at-a-time is known as Multiple Inheritance.


Ve

Diagram:

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

Note:

=>Multiple Inheritance Process using classes not available in Java,

because it generate replication(duplicate) of programming components and


raises ambiguity.The ambiguity state applications will give wrong results.

=>Multiple Inheritance process in Java can be performed using Interfces.

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

*imp

Interfaces in Java:

ii
=>Interface is a collection of Variables,abstract methods and concrete

ath
methods from Java8 version onwards.

(Upto Java7 version,Interface is a Collection of variables and abstract

aip
methods,but cannot hold Concrete methods)
hM
faq:

define abstract methods?

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


tes

abstract methods.
a
nk

Structure of abstract methods:

return_type method_name(para_list);
Ve

faq:

define Concrete methods?

=>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

ii
}

ath
-------------------------------------------------------------------

Coding rules of Interface:

aip
Rule-1 : we use "interface" keyword to construct interfaces
hM
syntax:

interface Interface_name

{
tes

//Interface_body

}
a
nk

Rule-2 : The programming components which are declared within the interface
Ve

are automatically "public"

Note:

=>The programming components which are declared in classes

without any access modifiers are considered as "default"


Rule-3 : The interfaces can be declared with both primitive datatype

and NonPrimitive datatype variables

Rule-4 : The variables which are declared within the interface are

automatically static and final variables

ii
Note:

ath
(i)static variables in interfaces will get the memory within the

interface while interface loading and can be accessed with

aip
interface_name

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


hM
initialized cannot be modified.

(final variables are also known as Constant Variables or

Secured Variables)
tes

Rule-5 : The methods which are declared within the interface are
a
nk

automatically NonStatic abstract methods.

(static abstract methods are not available)


Ve

Rule-6 : Interfaces cannot be instantiated in Java,which means we cannot

create object for Interfaces.

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


and the classes are known as implementation classes.

Rule-8 : These implementation classes must construct the body for all

abstract methods of Interface.

ii
ProjectName : Interface_App1

ath
packages,

p1 : ITest.java

aip
package p1;
public interface ITest
{
public static final int k=100;
hM
public abstract void dis();

}
tes

p1 : IClass.java
package p1;
public class IClass implements ITest{
a

public void dis() {


nk

System.out.println("====Implemented-
dis()=====");
System.out.println("The value k:"+k);
Ve

}
}

p2 : DemoInterface1.java(MainClass)
package p2;
import p1.*;
public class DemoInterface1 {
public static void main(String[] args) {
//ITest ob = new ITest();//Error
IClass ob = new IClass();//Implemented Object
ob.dis();
}
}

o/p:

ii
====Implemented-dis()=====

ath
The value k:100

aip
diagram:

================================================================
hM
Dt : 23/8/2023

Execution flow of above program:

ClassFiles:
tes

ITest.class
a

IClass.class
nk

DemoInterface1.class(MainClass)
Ve
ii
ath
aip
hM
===============================================================
tes

Rule-9 : Implementation classes can also be declared with NonImplemented

methods
a
nk
Ve

ProjectName : Interface_App2

packages,

p1 : ITest.java
package p1;
public interface ITest {
public abstract void m1(int x);
public abstract void m2(int y);
}

p1 : IClass.java
package p1;
public class IClass implements ITest{
public void m1(int x)//Implemented and Overriding
methods

ii
{
System.out.println("===Implemented-m1(x)====");

ath
System.out.println("The value x:"+x);
}
public void m2(int y)//Implemented and Overriding

aip
methods
{
System.out.println("===Implemented-m2(y)====");
System.out.println("The value y:"+y);
hM
}
public void m3(int z)//NonImplemented method
{
System.out.println("===NonImplemented-m3(z)====");
System.out.println("The value z:"+z);
tes

}
}
a

p2 : DemoInterface2.java(MainClass)
nk

package p2;
import p1.*;
Ve

public class DemoInterface2 {


public static void main(String[] args) {
IClass ob = new IClass();//Implementation Object
ob.m1(11);
ob.m2(12);
ob.m3(13);
}
}
o/p:

===Implemented-m1(x)====

The value x:11

===Implemented-m2(y)====

The value y:12

ii
===NonImplemented-m3(z)====

ath
The value z:13

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

faq:

wt is the diff b/w


aip
hM
(i)Implemented methods

(ii)NonImplemented methods
tes

(i)Implemented methods:
a

=>The methods which are taken from the interface and constructed body
nk

part of implementation classes are known as Implemented methods.


Ve

(ii)NonImplemented methods:

=>The methods which are constructed directly part of implementation

classes are known as NonImplemented methods,which means the methods


which
are not taken from the Interface.

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

Rule-10 : The interfaces can be implemented to any number implementation

classes.

ii
Layout:

ath
aip
hM
a tes
nk

ProjectName : Interface_App3
Ve

packages,

p1 : IComparable.java
package p1;
public interface IComparable {
public abstract int compareTo(int x,int y);
}
p1 : GreaterValue.java
package p1;
public class GreaterValue implements IComparable{
public int compareTo(int x,int y) {
if(x>y) return x;
else return y;
}

ii
}

ath
p1 : SmallerValue.java
package p1;

aip
public class SmallerValue implements IComparable{
public int compareTo(int x,int y) {
if(x<y) return x;
hM
else return y;
}
}
tes

p2 : DemoInterface3.java(MainClass)

package p2;
a

import java.util.*;
nk

import p1.*;

public class DemoInterface3 {


Ve

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Enter the value-1:");

int v1 = s.nextInt();
System.out.println("Enter the value-2:");

int v2 = s.nextInt();

if(v1>0 && v2>0)

System.out.println("****Choice****");

ii
System.out.println("\t1.GreaterValue"

ath
+ "\n\t2.SmallerValue");

System.out.println("Enter the Choice:");

aip
int choice = s.nextInt();

switch(choice)
hM
{

case 1:

GreaterValue gv = new GreaterValue();


tes

int res1 = gv.compareTo(v1, v2);

System.out.println("GreaterValue:"+res1);
a
nk

break;

case 2:
Ve

SmallerValue sv = new SmallerValue();

int res2 = sv.compareTo(v1, v2);

System.out.println("SmallerValue:"+res2);

break;

default:
System.out.println("Invalid Input...");

}//end of switch

}//end of if

else

ii
System.out.println("Invalid input..");

ath
}

s.close();

aip
}

}
hM
o/p:

Enter the value-1:

12
tes

Enter the value-2:

13
a
nk

****Choice****

1.GreaterValue
Ve

2.SmallerValue

Enter the Choice:

GreaterValue:13

==================================================================
Assignment:

Construct IArithmetic-Application using following Layout:

ii
ath
aip
hM
===================================================================

Rule-11 : Interfaces can be declared with Concrete methods.


tes

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

*imp
a

Concrete Methods in Interfaces:(Java8 - new feature)


nk

=>Java8 version onwards the interfaces can be declared with concrete


Ve

methods.

=>The following concrete methods can be declared in Interfaces:

(a)static concrete methods(Java8 - 2014)

(b)default concrete methods(Java8 - 2014)

(c)private concrete methods(Java9 - 2017)


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

ii
ath
aip
hM
a tes
nk
Ve

You might also like