1) Inheritance & Constructor
Declare a class employee having emp_id and empname as members. Extend class
employee (inheritance) to have a subclass called salary having designation and
monthly_salary as members. Define the following:
– Required constructors.
– A method to find and display all details of employee drawing salary more than
20000/-.
– Method main for creating an array for storing these details given as command line
argument and showing usage of above methods.
import java.util.*;
class employee
{
Scanner s=new Scanner(System.in);
int emp_id;
String empname;
/*void getdata()
{
System.out.println("enter name::");
empname=s.next();
System.out.println("enter empid::");
empid=s.nextInt();
}*/
}
class salary extends employee
{
//Scanner s=new Scanner(System.in);
double monthly_salary;
String designation;
salary(double monthly_salary,String designation,int emp_id,String empname)
{
this.emp_id=emp_id;
this.empname=empname;
this.designation=designation;
this.monthly_salary=monthly_salary;
}
void condition()
{
System.out.println("monthly salary is:"+monthly_salary);
if(monthly_salary>20000)
{
System.out.println("name of employee is::"+empname);
System.out.println("id of employee is::"+emp_id);
System.out.println("monthly salary is::"+monthly_salary);
System.out.println("designation of employee is::"+designation);
}
else
{
System.out.println("Cannot display details....");
}
}
}
public class hierarchial1
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int id,i;
String name,desig;
double sal;
for(i=0;i<3;i++)
{
System.out.println("enter id of employee..");
id=s.nextInt();
System.out.println("enter name of employee..");
name=s.next();
System.out.println("enter designation of employee...");
desig=s.next();
System.out.println("enter salary of employee");
sal=s.nextDouble();
salary x=new salary(sal,desig,id,name);
x.condition();
}
// salary x=new salary(sal,desig,id,name);
//x.condition();
}
}
2) Method Overloading & Overriding
package com.techvidvan.methodoverriding;
public class Addition
{
int add(int a, int b)
{
return (a + b);
}
int add(int a , int b , int c)
{
return (a + b + c) ;
}
double add(double a , double b)
{
return (a + b);
}
double add(int a , double b)
{
return (a + b);
}
public static void main( String args[])
{
Addition ob = new Addition();
System.out.println("Calling add method with two int parameters: " +ob.add(17,
25));
System.out.println("Calling add method with three int parameters: " +ob.add(55,
27, 35));
System.out.println("Calling add method with two double parameters: "
+ob.add(36.5, 42.8));
System.out.println("Calling add method with one int and one double parameter: "
+ob.add(11, 24.5));
}
}
package com.techvidvan. methodoverriding;
//Base Class
class Parent
{
void view()
{
System.out.println("This is a parent class method");
}
}
class Child extends Parent
{
@Override
void view()
{
System.out.println("This is a child class method");
}
}
//Driver class
public class MethodOverriding
{
public static void main(String args[])
{
Parent obj = new Parent();
obj.view();
Parent obj1 = new Child();
obj1.view();
}
}
3) Garbage Collection
class Employee {
private int ID;
private String name;
private int age;
private static int nextId = 1;
// it is made static because it
// is keep common among all and
// shared by all objects
public Employee(String name, int age)
{
this.name = name;
this.age = age;
this.ID = nextId++;
}
public void show()
{
System.out.println("Id=" + ID + "\nName=" + name
+ "\nAge=" + age);
}
public void showNextId()
{
System.out.println("Next employee id will be="
+ nextId);
}
protected void finalize()
{
--nextId;
// In this case,
// gc will call finalize()
// for 2 times for 2 objects.
}
}
public class UseEmployee {
public static void main(String[] args)
{
Employee E = new Employee("GFG1", 56);
Employee F = new Employee("GFG2", 45);
Employee G = new Employee("GFG3", 25);
E.show();
F.show();
G.show();
E.showNextId();
F.showNextId();
G.showNextId();
{
// It is sub block to keep
// all those interns.
Employee X = new Employee("GFG4", 23);
Employee Y = new Employee("GFG5", 21);
X.show();
Y.show();
X.showNextId();
Y.showNextId();
X = Y = null;
System.gc();
System.runFinalization();
}
E.showNextId();
}
}
4) Polymosphism
class Hillstations{
void location(){
System.out.println("Location is:");
}
void famousfor(){
System.out.println("Famous for:");
}
}
class Manali extends Hillstations {
void location(){
System.out.println("Manali is in Himachal Pradesh");
}
void famousfor(){
System.out.println("It is Famous for Hadimba Temple and adventure sports");
}
}
class Mussoorie extends Hillstations {
void location(){
System.out.println("Mussoorie is in Uttarakhand");
}
void famousfor(){
System.out.println("It is Famous for education institutions");
}
}
class Gulmarg extends Hillstations {
void location(){
System.out.println("Gulmarg is in J&K");
}
void famousfor(){
System.out.println("It is Famous for skiing");
}
}
class main{
public static void main(String args[]){
Hillstations A = new Hillstations();
Hillstations M = new Manali();
Hillstations Mu = new Mussoorie();
Hillstations G = new Gulmarg();
A.location();
A.famousfor();
M.location();
M.famousfor();
Mu.location();
Mu.famousfor();
G.location();
G.famousfor();
}
}