Write a program to implement array, polymorphism, inheritance using methods in java.
Program Code:
(i) Array:
class Array
{
public static void main(String args[])
{
int a[]=new int[4];//declaration and instantiation
a[0]=1;//initialization
a[1]=2;
a[2]=3;
a[3]=4;
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
(ii). Polymorphism
Public class Car
{
void run()
{
System.out.println("running");
}
}
class Swift extends Bike
{
void run()
{
System.out.println("running safely with 80km");
}
public static void main(String args[])
{
Car c = new Swift();//upcasting c.run();
}
}
Output:
running safely with 50km
(iii). Inheritance
class Employee
{
float salary=400;
}
class Programmer extends Employee
{
int bonus=100;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:
Programmer salary is:400
Bonus of Programmer is: 100
Post A Comment:
0 comments: