Test 3 Key
Test 3 Key
1.
Ans:
In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the
derived class act as the parent class to other class.
As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act
as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.
Program:
class Car{
public Car() {
System.out.println("Class Car");
}
public void vehicleType() {
System.out.println("Vehicle Type: Car");
}
}
classMaruti extends Car{
publicMaruti() {
System.out.println("Class Maruti");
}
public void brand() {
System.out.println("Brand: Maruti");
}
public void speed() {
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800() {
System.out.println("Maruti Model: 800");
}
public void speed(){
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
2.
To establish the connection between base class constructor and derived class
constructors JVM provides two implicit methods they are:
super()
super(...)
super()
super() It is used for calling super class default constructor from the context of
derived class constructors.
3)A) How interfaces are extended in java? Give demonstration using a java
program. 4M
Using the keyword interface, you can fully abstract a class’ interface from its
implementation.
That is, using interface, you can specify what a class must do, but not how it does
it.
interface A {
void meth1();
void meth2();
interface B extends A {
void meth3();
System.out.println("Implement meth1().");
System.out.println("Implement meth2().");
System.out.println("Implement meth3().");
}
class IFExtend {
ob.meth1();
ob.meth2();
ob.meth3();
Or
interface one
interface two
interface three
int c=a+b;
System.out.println("sum is="+c);
int c=a-b;
System.out.println("sub is="+c);
int c=a*b;
System.out.println("mul is="+c);
{
int c=a/b;
System.out.println("div is="+c);
class Six
f.sum(10,20);
f.sub(20,10);
f.mul(5,5);
f.div(10,5);
interface EmpFace
{
void compute_salary();
}
class Employee implements EmpFace
floatbasicSal=10000;
float da=basicSal*15/100;
float hra=basicSal*10/100;
float grosssal=basicSal+da+hra;
System.out.println ("Gross Salary= "+grosssal);
float da=basicSal*20/100;
float hra=basicSal*15/100;
float grosssal=basicSal+da+hra;
System.out.println ("Gross Salary= "+grosssal);
m.compute_salary();
}
}
4) A) Write a java interface Iface having callMe() method in it,
implementIface with a class IfaceImpl to implement callMe(). Write a
demo class to demonstrate this.
4M
interface Iface
Void callMe();
class Demo {
i.callMe();
or
I1.callMe();
}
4) B) Develop a Java program using inheritance and define Animal as the
super class with void sleep(), void eat() as the methods. Class Dog extends
Animal which includes void bark() method? Define a Demo class to create a
Dog object and call all the methods in the inheritance. 6M
class Animal
void sleep()
void eat()
void bark ( )
class demo
{
public static void main (String args[ ])
do.sleep();
do. eat();
do. bark();
a. sleep ( );
a. eat();
3) Abstract class can have Interface has only static and final
final, non-final, static and variables.
non-static variables.
6) Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
5)b) Develop a Java program Animal class as abstract class having atleast one abstract Method in
it,write a Monkey class as subclass of Animal class,test how the methods of Animal and Monkey
classes are executed?
interface Exam
{
void percent_cal();
}
class Student
{
String name;
int roll_no,mark1,mark2;
Student(String n, int r, int m1, int m2)
{
name=n;
roll_no=r;
mark1=m1;
mark2=m2;
}
void display()
{
System.out.println ("Name of Student: "+name);
System.out.println ("Roll No. of Student: "+roll_no);
System.out.println ("Marks of Subject 1: "+mark1);
System.out.println ("Marks of Subject 2: "+mark2);
}
}
class Result extends Student implements Exam
{
Result(String n, int r, int m1, int m2)
{
super(n,r,m1,m2);
}
public void percent_cal()
{
int total=(mark1+mark2);
float percent=total*100/200;
System.out.println ("Percentage: "+percent+"%");
}
void display()
{
super.display();
}
}
6)b) Develop a java program that creates a super class called Figure that
stores the dimensions of two-dimensional object. It also defines a method
called area() that computes the area of an object. The program derives two
subclasses form Figure. The first is Rectangle and second is Triangle.
Each of these subclasses overrides area() so that it returns the area of a
rectangle and a Triangle respectively.
class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
double area()
{
System.out.println("Area for Figure is undefined."); return 0;
}
}
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
Public class sixb
{
public static void main(String args[])
{
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}