Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Lab EXPERIMENT 6 problem statements and solutions.

1. Write program to demonstrate this and super keywords

class Person
{  
int id;  
String name;  
Person(int id, String name)
{  
this.id=id;  
this.name=name;  
}  
}  
class Emp extends Person
{  
float salary;  
Emp(int id, String name, float salary)
{  
super(id,name); //reusing parent constructor  
this.salary=salary;  
}  
void display()
{
System.out.println(id+" "+name+" "+salary);
}  
}  
class Main
{  
public static void main(String[] args)
{  
Emp e1=new Emp(18,"vicky",45000f);  
e1.display();  
}

2. Write program to demonstrate dynamic method dispatch
Execute the program which is given in class

3. Write a program to demonstrate final variable and methods

Final keyword:
class bike
{
final int speedlimit=90;

void run()
{
speedlimit=400;

}
public static void main(String args[])
{

bike obj=new bike();


obj.run();

}
}

Final Method:

class Bike{
final void run()
{
System.out.println("running");
}
}

class hero extends Bike


{
void run()
{
System.out.println("running safely with 100kmph");
}

public static void main(String args[])


{
hero h= new hero ();
h.run();
}
}

Java final keyword

In Java, the final keyword is used to denote constants. It can be used with variables,


methods, and classes.
Once any entity (variable, method or class) is declared final, it can be assigned only
once. That is,
 the final variable cannot be reinitialized with another value

 the final method cannot be overridden

 the final class cannot be extended

4. Write a program to demonstrate use of abstract class

abstract class Shape


{
abstract void draw();
}

class Rectangle extends Shape


{
void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("drawing circle");
}
}
class Main
{
public static void main(String args[])
{
Shape c=new Circle();
Shape r=new Rectangle();
c.draw();
r.draw();
}
}

You might also like