Monday 18 October 2021

Interface java Example Interview

 //interface Only public, static and final are allowed.

//No. Interfaces can’t have constructors.
interface Test{

static int s=2;
final int d=4;
void get();
int getmultiple();

}


// initializers not allowed in interfaces
/*interface demoStatic(){
{ System.out.println("empty implemented");}

static{
System.out.println("static implemented");
}
}*/


interface B{
int getdivide(int i);
// int getadd();//implementation is required in Class A
}

class A implements B{

public int getdivide(int i ){
return i*i;
}
}


class p{

interface Q{
int i=111;
}
}

interface NextTest extends Test{
int t=5;
void get();

int getadd();

// int getValue();//Demo is not abstract and does not override abstract method getValue() in NextTest
}

class Demo implements Test, NextTest {

public int getadd(){
return d+t;
}

public int getmultiple(){
return d*t;
}
public void get(){
System.out.println("demo get method implemented");
}


}
class XDemo extends Demo{

}

/*class XXDemo implements NextTest{
void methodD(){
i=4;
//No, because interface fields are static and
//final by default and you can’t change
//their value once they are initialized. In the above code, methodB()
//is changing value of interface field A.i. It shows compile time error.
}
}*/


public class MyClass {
public static void main(String args[]) {

System.out.println("Interface");

Test s=new XDemo();
s.get();
// System.out.println("sum : "+ s.getadd());//it will check in Test interface error not found

System.out.println("multiple : "+ s.getmultiple());
NextTest st=new XDemo();
System.out.println("add : "+ st.getadd());

// XDemo s=new NextTest();//NextTest is abstract; cannot be instantiated
XDemo sp=new XDemo();
System.out.println("addd : "+ sp.getadd());
System.out.println("multiple : "+ sp.getmultiple());



B newS=new A();

System.out.println("divide : "+ newS.getdivide(12));
// p ps=new Q();
B bs=new B(){
public int getdivide(int i){
return i*i; }

};

System.out.println("divide : "+ bs.getdivide(12));
}
}

Find the character element which appears maximum number of times in an String

 //Find the element which appears maximum number of times in an array


import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {

String name="amitrawat";

char[] word=name.toCharArray();

for(int i=0;i<word.length;i++){

for(int j=i+1;j<word.length;j++){
char first=word[i];
char second=word[j];
char temp;

if(first>second){
temp=first;
word[i]=second;
word[j]=temp;
}
}

}

System.out.println("max number : " + String.valueOf(word));


//getting max character and times
char maxchar=word[0];
int times=0;
for(int i=0;i<word.length;i++){

char w=word[i];

if(maxchar==w){
times++;

}else if(maxchar<w){
times=1;
maxchar=w;

}
}

System.out.println("char max : " + maxchar);
System.out.println("char times: " + times);
}
}
OUTPUT
max number : aaaimrttw
char max  : w
char times: 1

Find the element which appears maximum number of times in an array

import java.util.Arrays;
public class MyClass {
public static void main(String args[]) {
int max = 0, count = 0;
int[] a={1,2,3,4,8,8,9,8,3,3,5,3,4,5,6,8};

//sorting array
for(int i=0;i<a.length;i++){

for(int j=i+1;j<a.length;j++){
int first=a[i];
int second=a[j];
int temp=0;

if(first>second){

temp=first;
a[i]=second;
a[j]=temp;

}
}
}
// Arrays.sort(a);
//finding highest number with times
for(int i=0;i<a.length;i++){
int num=a[i];

if(max==num){
count++;
}else if(max<num){
max=num;
count=1;

}
}

System.out.println("max number : " + max);
System.out.println("times: " + count);

}
}

OUTPUT
max number : 9
times: 1

Monday 11 October 2021

Overloading and overriding Interview Example Code

 class Test {


static int s = 4;
final int d = 5;

static void sit() {
System.out.println(" sit method of test class ");
}

void get() {
System.out.println(" get method test class");

}
}

class Test1 extends Test {
static int s = 6;
static int a = 2;
final int d = 3;

static void sit() {
System.out.println("sit method test1 class");
}

void get() {

System.out.println("get method get test1 class");
}
}

class Test2 extends Test1 {
static int s = 7;

static void sit() {
System.out.println("sit method test2 class");
}

void get() {
System.out.println("get method get test2 class");
}

}

class Demo {
}

public class HelloWorld {
public static void main(String[] args) {
System.out.println("1 : test2 class instance of test2 class.......... ");
System.out.println();
Test2 obj2 = new Test2();
//class c inherit static
System.out.println("static varialble : " + obj2.a); // 2
System.out.println();

System.out.println("final varibale : " + obj2.d); //3
System.out.println();

obj2.sit();//print method of test2 class

System.out.println();
System.out.println();

System.out.println("2 : test1 class instance of test2 class.......... ");
System.out.println();
Test1 obj1 = new Test2();

obj1.sit();// print method of class test1 : static method
System.out.println();


obj1.get();//print method of class test2 :non static method
System.out.println();


//testing the type cast
Test objtrail1 = new Test1();//correct
Test objtrail2 = new Test2();//correct

// Test1 cannot be converted to Test2
// Test2 objtrail3=new Test1();//incorrect type cast error

// incompatible types: Test cannot be converted to Test2
// Test2 objtrail3=new Test();//incorrect type cast error


// Test1 obje=new Test();//error: incompatible types: Test cannot be converted to Test1

System.out.println();
System.out.println();
System.out.println("3 : test class instance of test1 class.......... ");

Test obje = new Test1();//the instance of test1 but print the static and final variable and static method of test
System.out.println();

System.out.println("print the static variable of test class " + obje.s);//4
System.out.println();

System.out.println("print the final variable of test class " + obje.d);//5
System.out.println();

obje.sit();//print the method of static test class
System.out.println();

obje.get();//print method of non static test1 class

System.out.println();


// System.out.println("3 : test2 class instance of test class.......... ");


// Test2 objet=new Test();

// objet.sit();//error: incompatible types: Test cannot be converted to Test2
// Test2 objet=new Test1();//error: incompatible types: Test1 cannot be converted to Test2


// Test s=new Demo();//error: incompatible types: Demo cannot be converted to Test
}
}



output :  

1 : test2 class instance of test2 class.......... 

static varialble : 2

final varibale : 3

sit method test2 class


2 : test1 class instance of test2 class.......... 

sit  method test1 class

get  method get test2 class



3 : test class instance of test1 class.......... 

print the static  variable of test class  4

print the final  variable of test  class 5

 sit method of  test class 

get  method get test1 class