In this post, I am writing different Java Programs based on OOPS. I hope this post very helpful to those are facing technical round interviews.In the Interview room interviewer may ask about oops and their examples. As a fresher,might have been knowledge on OOPS concepts but some people do not have programming knowledge on Object Oriented Programming.So, i am writing here few frequently asked OOPS java programs.
1. How Can you explain encapsulation concept with programs? can you write on it?
Ans:
class EncapsulationDemo
{
private String empName;
private int empAge;
//Getter and Setter methods
public String getEmpName()
{
return empName;
}
public int getEmpAge()
{
return empAge;
}
public void setEmpName(String newValue)
{
empName=newValue;
}
public void setEmpAge(int newValue)
{
empAge =newValue;
}
}
public class EnTest
{
public static void main(String args[])
{
EncapsulationDemo ed=new EncapsulationDemo();
ed.setEmpName("pavan");
ed.setEmpAge(26);
System.out.println("Employee name:"+ed.getEmpName());
System.out.println("Employee Age:"+ed.getEmpAge());
}
}
For more details on Encapsulation: Encapsulation tutorial
The Result Will be:
E:\>javac EnTest.java
E:\>java EnTest
Employee name:pavan
Employee Age:26
2. How to achieve Multiple Inheritance using Multiple Interfaces?
interface Boy
{
double wgt=65.7;
void weight();
}
interface Girl
{
double wgt=42.3;
void weight();
}
class Both implements Boy,Girl
{
public void weight()
{
//Both average weight of Boy and girl
double wgt=(Boy.wgt+Girl.wgt)/2;
System.out.println("Both average weight="+wgt);
}
}
class MultiInherit
{
public static void main(String args[])
{
Both b=new Both();
b.weight();
}
}
The Result will be:
E:\>javac MultiInherit.java
E:\>java MultiInherit
Both average weight=54.0
3. How to Override a method by using polymorphism? Give Example?
class Commericial
{
private String name;
void setName(String name)
{
this.name=name;
}
//retrieve the name
String getName()
{
return name;
}
void calculateBill(int units)
{
System.out.println("customer:"+getName());
System.out.println("Bill Amount:"+units*4.00);
}
}
class Domestic extends Commericial
{
void calculateBill(int units)
{
System.out.println("customer:"+getName());
System.out.println("Bill Amount:"+units*2.75);
}
}
//calculate electricity bill for commercial and domestic users
class ElectericityBill
{
public static void main(String args[])
{
//call calculateBill()using the commercial object
Commericial cl=new Commericial();
cl.setName("lucky");
cl.calculateBill(50);
//call calculateBill() using the domestic object
Domestic d=new Domestic();
d.setName("pavan");
d.calculateBill(75);
}
}
For More Details : Polymorphism in java
The Result Will be:
E:\>javac ElectericityBill.java
E:\>java ElectericityBill
customer: lucky
Bill Amount: 200.0
customer: pavan
Bill Amount: 206.25
4. How can you display message without main() method?
Example 1:
//No main() method
class Test
{
static{
System.out.println("how are you");
}
}
Result will be:
E:\javaprograms>javac Test.java
E:\javaprograms>java Test
Hello World!
Exception in thread "main" java.lang.NoSuchMethodError: main
In the above program, we have displayed a message without main() method and we got error message also.If we want skip that error message we have to fallow the below code
class Test
{
static{
System.out.println("how are you");
System.ext(0);
}
}
Result will be:
E:\javaprograms>javac Test.java
E:\javaprograms>java Test
Hello World!
5. Write a program for Dynamic Binding
class Human
{
public void eat()
{
System.out.println("humans do eat");
}
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
//reference is of parent class
Human h=new Boy();
h.eat();
}
}
Result will be:
E:\javaprograms>javac Boy.java
E:\javaprograms>java Boy
boys do eat
6. Write a program for static Binding
In this program we have created an object of Boy class and calling the method of eat() of the same class.Since nothing is ambiguous here,compiler would be able to resolve this binding during compile time,such kind of binding is known as static binding
class Human
{
public void eat()
{
System.out.println("humans do eat");
}
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
Boy b=new Boy();
b.eat();
}
}
Result will be:
E:\javaprograms>javac Boy.java
E:\javaprograms>java Boy
boys do eat
7. Where do you use super() method in your project?Explain it?
class One
{
int i=10;
void show()
{
System.out.println("super class method:i="+i);
}
}
class Two extends One
{
//sub class variable
int i=20;
//sub class method
void show()
{
System.out.println("super class method:i="+i);
}
}
class SuperDemo
{
public static void main(String args[])
{
//create sub class object
Two t=new Two();
t.show();
}
}
Result will be:
E:\javaprograms>javac SuperDemo.java
E:\javaprograms>java SuperDemo
super class method:i=20
Find More Java programs: Basic java programs
1. How Can you explain encapsulation concept with programs? can you write on it?
Ans:
class EncapsulationDemo
{
private String empName;
private int empAge;
//Getter and Setter methods
public String getEmpName()
{
return empName;
}
public int getEmpAge()
{
return empAge;
}
public void setEmpName(String newValue)
{
empName=newValue;
}
public void setEmpAge(int newValue)
{
empAge =newValue;
}
}
public class EnTest
{
public static void main(String args[])
{
EncapsulationDemo ed=new EncapsulationDemo();
ed.setEmpName("pavan");
ed.setEmpAge(26);
System.out.println("Employee name:"+ed.getEmpName());
System.out.println("Employee Age:"+ed.getEmpAge());
}
}
For more details on Encapsulation: Encapsulation tutorial
The Result Will be:
E:\>javac EnTest.java
E:\>java EnTest
Employee name:pavan
Employee Age:26
2. How to achieve Multiple Inheritance using Multiple Interfaces?
interface Boy
{
double wgt=65.7;
void weight();
}
interface Girl
{
double wgt=42.3;
void weight();
}
class Both implements Boy,Girl
{
public void weight()
{
//Both average weight of Boy and girl
double wgt=(Boy.wgt+Girl.wgt)/2;
System.out.println("Both average weight="+wgt);
}
}
class MultiInherit
{
public static void main(String args[])
{
Both b=new Both();
b.weight();
}
}
The Result will be:
E:\>javac MultiInherit.java
E:\>java MultiInherit
Both average weight=54.0
3. How to Override a method by using polymorphism? Give Example?
class Commericial
{
private String name;
void setName(String name)
{
this.name=name;
}
//retrieve the name
String getName()
{
return name;
}
void calculateBill(int units)
{
System.out.println("customer:"+getName());
System.out.println("Bill Amount:"+units*4.00);
}
}
class Domestic extends Commericial
{
void calculateBill(int units)
{
System.out.println("customer:"+getName());
System.out.println("Bill Amount:"+units*2.75);
}
}
//calculate electricity bill for commercial and domestic users
class ElectericityBill
{
public static void main(String args[])
{
//call calculateBill()using the commercial object
Commericial cl=new Commericial();
cl.setName("lucky");
cl.calculateBill(50);
//call calculateBill() using the domestic object
Domestic d=new Domestic();
d.setName("pavan");
d.calculateBill(75);
}
}
For More Details : Polymorphism in java
The Result Will be:
E:\>javac ElectericityBill.java
E:\>java ElectericityBill
customer: lucky
Bill Amount: 200.0
customer: pavan
Bill Amount: 206.25
4. How can you display message without main() method?
Example 1:
//No main() method
class Test
{
static{
System.out.println("how are you");
}
}
Result will be:
E:\javaprograms>javac Test.java
E:\javaprograms>java Test
Hello World!
Exception in thread "main" java.lang.NoSuchMethodError: main
In the above program, we have displayed a message without main() method and we got error message also.If we want skip that error message we have to fallow the below code
class Test
{
static{
System.out.println("how are you");
System.ext(0);
}
}
Result will be:
E:\javaprograms>javac Test.java
E:\javaprograms>java Test
Hello World!
5. Write a program for Dynamic Binding
class Human
{
public void eat()
{
System.out.println("humans do eat");
}
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
//reference is of parent class
Human h=new Boy();
h.eat();
}
}
Result will be:
E:\javaprograms>javac Boy.java
E:\javaprograms>java Boy
boys do eat
6. Write a program for static Binding
In this program we have created an object of Boy class and calling the method of eat() of the same class.Since nothing is ambiguous here,compiler would be able to resolve this binding during compile time,such kind of binding is known as static binding
class Human
{
public void eat()
{
System.out.println("humans do eat");
}
}
class Boy extends Human
{
public void eat()
{
System.out.println("boys do eat");
}
public static void main(String args[])
{
Boy b=new Boy();
b.eat();
}
}
Result will be:
E:\javaprograms>javac Boy.java
E:\javaprograms>java Boy
boys do eat
7. Where do you use super() method in your project?Explain it?
class One
{
int i=10;
void show()
{
System.out.println("super class method:i="+i);
}
}
class Two extends One
{
//sub class variable
int i=20;
//sub class method
void show()
{
System.out.println("super class method:i="+i);
}
}
class SuperDemo
{
public static void main(String args[])
{
//create sub class object
Two t=new Two();
t.show();
}
}
Result will be:
E:\javaprograms>javac SuperDemo.java
E:\javaprograms>java SuperDemo
super class method:i=20
Find More Java programs: Basic java programs
No comments:
Post a Comment