hlo1
hlo1
Note:
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
above programs
tes
2.Multiple Inheritance:
Diagram:
===============================================================
Note:
================================================================
*imp
Interfaces in Java:
ii
=>Interface is a collection of Variables,abstract methods and concrete
ath
methods from Java8 version onwards.
aip
methods,but cannot hold Concrete methods)
hM
faq:
abstract methods.
a
nk
return_type method_name(para_list);
Ve
faq:
=>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
-------------------------------------------------------------------
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
Note:
Rule-4 : The variables which are declared within the interface are
ii
Note:
ath
(i)static variables in interfaces will get the memory within the
aip
interface_name
Secured Variables)
tes
Rule-5 : The methods which are declared within the interface are
a
nk
Rule-8 : These implementation classes must construct the body for all
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
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
ClassFiles:
tes
ITest.class
a
IClass.class
nk
DemoInterface1.class(MainClass)
Ve
ii
ath
aip
hM
===============================================================
tes
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
===Implemented-m1(x)====
===Implemented-m2(y)====
ii
===NonImplemented-m3(z)====
ath
The value z:13
===================================================================
=
faq:
(ii)NonImplemented methods
tes
(i)Implemented methods:
a
=>The methods which are taken from the interface and constructed body
nk
(ii)NonImplemented methods:
================================================================
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.*;
int v1 = s.nextInt();
System.out.println("Enter the value-2:");
int v2 = s.nextInt();
System.out.println("****Choice****");
ii
System.out.println("\t1.GreaterValue"
ath
+ "\n\t2.SmallerValue");
aip
int choice = s.nextInt();
switch(choice)
hM
{
case 1:
System.out.println("GreaterValue:"+res1);
a
nk
break;
case 2:
Ve
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:
12
tes
13
a
nk
****Choice****
1.GreaterValue
Ve
2.SmallerValue
GreaterValue:13
==================================================================
Assignment:
ii
ath
aip
hM
===================================================================
===================================================================
*imp
a
methods.
ii
ath
aip
hM
a tes
nk
Ve