Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Java Programming-Chapter3 (MSBTE Solutions)

The document discusses key concepts in Java such as inheritance, interfaces, and packages, including types of inheritance, dynamic method dispatch, and the differences between method overloading and overriding. It explains how interfaces enable multiple inheritance, the definition and creation of packages, and provides examples of Java programs demonstrating these concepts. Additionally, it covers final variables and methods, as well as how to access packages from other packages.

Uploaded by

manasikamble2807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Java Programming-Chapter3 (MSBTE Solutions)

The document discusses key concepts in Java such as inheritance, interfaces, and packages, including types of inheritance, dynamic method dispatch, and the differences between method overloading and overriding. It explains how interfaces enable multiple inheritance, the definition and creation of packages, and provides examples of Java programs demonstrating these concepts. Additionally, it covers final variables and methods, as well as how to access packages from other packages.

Uploaded by

manasikamble2807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit-III Inheritance, Interface & Package

CHAPTER 3
SUMMER-19 (22412)
SRNO QUESTIONS MARKS
1 List the types of inheritances in Java. 2M
(Note: Any four types shall be considered)
Types of inheritances in Java: Any
i. Single level inheritance four
ii. Multilevel inheritance types
iii. Hierarchical inheritance ½M
iv. Multiple inheritance each
v. Hybrid inheritance
2 Explain dynamic method dispatch in Java with suitable example. 4M
Dynamic method dispatch is the mechanism by which a call to an Overridden method is
resolved at run time, rather than compile time.
reference, Java determines
which version (superclass/subclasses) of that method is to be executed based upon the type
of the object being referred to at the time the call occurs. Thus, this determination is made
at run time.
-time, it depends on the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be executed
e variable can refer to a subclass object. This is also known as
upcasting. Java uses this fact to resolve calls to overridden methods at run time. Therefore, if
a superclass contains a method that is overridden by a subclass, then when different types of
objects are referred to through superclass reference variable, different versions of the
method are executed. Here is an example that illustrates dynamic method dispatch:
// A Java program to illustrate Dynamic Method Dispatch using hierarchical inheritance
class A{
void m1()
Explanation 2M
{ System.out.println("Inside A's m1 method"); }
Example
}
2M
class B extends A {
void m1()
{ System.out.println("Inside B's m1 method"); }
}
class C extends A{
void m1()
{ System.out.println("Inside C's m1 method"); }
}
class Dispatch{
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
A ref;
ref = a;
ref.m1();
Unit-III Inheritance, Interface & Package

ref = b;
ref.m1();
ref = c;
ref.m1();
}
}
3 Differentiate between method overloading and method overriding. 4M

Any
four
points
1M each

4 Explain the concept of Dynamic method dispatch with suitable example. 6M


Method overriding is one of the ways in which Java supports Runtime Polymorphism.
Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at run time, rather than compile time.
When an overridden method is called through a superclass reference, Java determines which
version (superclass/subclasses) of that method is to be executed based upon the type of the
object being referred to at the time the call occurs. Thus, this determination is made at run
time.
At run-time, it depends on the type of the object being referred to (not the type of the
reference variable) that determines which version of an overridden method will be executed
Explanation 3M
A superclass reference variable can refer to a subclass object. This is also known as
Example
upcasting. Java uses this fact to resolve calls to overridden methods at run time.
3M
If a superclass contains a method that is overridden by a subclass, then when different types
of objects are referred to through a superclass reference variable, different version of the
method are executed. Here is an example that illustrates dynamic method dispatch:
/ A Java program to illustrate Dynamic Method // Dispatch using hierarchical inheritance
class A{
void m1()
{ System.out.println("Inside A's m1 method"); }
}
class B extends A{
Unit-III Inheritance, Interface & Package

void m1()
{ System.out.println("Inside B's m1 method"); }
}
class C extends A{
void m1()
{ System.out.println("Inside C's m1 method"); }
}
class Dispatch{
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
Unit-III Inheritance, Interface & Package

WINTER-19 (22412)
SRNO QUESTIONS MARKS
1 List any four Java API packages. 2M
1.java.lang
2.java.util
3.java.io 1/2M for
4.java.awt one Package
5.java.net
6.ava.applet
2 Differentiate between class and interfaces. 4M

3 Describe final variable and final method. 4M


Final method: making a method final ensures that the functionality defined in this method will
never be altered in any way, ie a final method cannot be overridden.
Syntax:
final void findAverage()
{
2M for
//implementation
definition,2M
}
for
Example of declaring a final method:
example
class A{
final void show()
{ System.out.println(“in show of A”); }
}
class B extends A{
Unit-III Inheritance, Interface & Package

void show() // cannot override because it is declared with final


{ System.out.println(“in show of B”); }
}
Final variable: the value of a final variable cannot be changed. Final variable behaves like class
variables and they do not take any space on individual objects of the class.
Example of declaring final variable: final int size = 100;
4 Define packages. How to create user defined package? Explain with example. 6M
Java provides a mechanism for partitioning the class namespace into more manageable parts.
This mechanism is the package. The package is both naming and visibility controlled
mechanism.
Package can be created by including package as the first statement in java source code. Any
classes declared within that file will belong to the specified package. Package defines a
namespace in which classes are stored.
The syntax for defining a package is: package pkg;
Here, pkg is the name of the package eg : package mypack;

Packages are mirrored by directories.


Java uses file system directories to store packages. The class files of any classes which are
declared in a package must be stored in a directory which has same name as package name.
The directory must match with the package name exactly. A hierarchy can be created by
separating package name and sub package name by a period(.) as pkg1.pkg2.pkg3; (Definition of
which requires a directory structure as pkg1\pkg2\pkg3. package - 1M,
Syntax: Package
To access package In a Java source file, import statements occur immediately following the creation
package statement (if it exists) and before any class definitions. - 2M
Syntax: Example - 3M
import pkg1[.pkg2].(classname|*); (Note Any
Example: other
package package1; example can
public class Box{ be
int l= 5; considered)
int b = 7;
int h = 8;
public void display()
{ System.out.println("Volume is:"+(l*b*h)); }
}
Source file:
import package1.Box;
class volume {
public static void main(String args[]){
Box b=new Box();
b.display();
}
}
Unit-III Inheritance, Interface & Package

Implement the given inheritance.

5 6M

interface Salary {
double Basic Salary=10000.0;
void Basic Sal();
}
class Employee{
String Name;
int age;
Employee(String n, int b) {
Name=n;
age=b;
}
void Display(){
System.out.println("Name of Employee:"+Name);
System.out.println("Age of Employee :"+age); (Interface:
} } 1M,
class Gross_Salary extends Employee implements Salary{ Employee
double HRA,TA,DA; class:
Gross_Salary(String n, int b, double h,double t,double d){ 2M,
super(n,b); Gross_Salary
HRA=h; class: 3M)
TA=t; (Any other
DA=d; logic
} considered)
public void Basic_Sal()
{ System.out.println("Basic Salary:"+Basic_Salary); }
void Total_Sal(){
Display();
Basic_Sal();
double Total_Sal=Basic_Salary + TA + DA +HRA;
System.out.println("Total Salary :"+Total_Sal);
} }
class EmpDetails{
public static void main(String args[]){
Gross_Salary s=new Gross_Salary("Sachin",20,1000,2000,7000);
s.Total_Sal();
} }
Unit-III Inheritance, Interface & Package

SUMMER-18 (17515)
SRNO QUESTIONS MARKS
1 Explain how interface is used to achieve multiple Inheritance in Java. 4M
{{**Note:-Any other example can be considered **}}
Multiple inheritances is not possible in java. Multiple inheritance happens when a class is
derived from two or more parent classes. Java classes cannot extend more than one parent
classes, instead it uses the concept of interface to implement the multiple inheritance. It
contains final variables and the methods in an interface are abstract. A sub class implements
the interface. When such implementation happens, the class which implements the interface
must define all the methods of the interface. A class can implement any number of interfaces.
Example of multiple inheritance :

import java.io.*;
class Student{
String name;
int roll_no;
double m1, m2; (Explanation:
Student(String name, int roll_no, double m1, double m2){ 2M,
this.name = name; Example: 2M)
this.roll_no = roll_no;
this.m1 = m1;
this.m2 = m2;
}
}
interface exam
{ public void per_cal(); }
class result extends Student implements exam{
double per;
result(String n, int r, double m1, double m2)
{ super(n,r,m1,m2); }
public void per_cal(){
per = ((m1+m2)/200)*100;
System.out.println("Percentage is "+per);
}
void display(){
System.out.println("The name of the student is"+name);
System.out.println("The roll no of the student is"+roll_no);
per_cal();
}
Unit-III Inheritance, Interface & Package

public static void main(String args[]){


BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("Enter name, roll no mark1 and mark 2 of the student");
String n = bin.readLine();
int rn = Integer.parseInt(bin.readLine());
double m1 = Double.parseDouble(bin.readLine());
double m2 = Double.parseDouble(bin.readLine());
result r = new result(n,rn,m1,m2);
r.display();
} catch(Exception e)
{ System.out.println("Exception caught"+e); }
}
}
2 Write a java program to implement multilevel inheritance with 4 levels of hierarchy. 8M
{{**Note:-Any other example can be considered **}}
class emp{
int empid;
String ename;
emp(int id, String nm){
empid=id;
ename=nm;
}
}
class work_profile extends emp{
String dept;
String job;
work_profile(int id, String nm, String dpt, String j1){
super(id,nm);
dept=dpt; ( Correct
job=j1; logic:4M,
} Correct
} syntax: 4M )
class salary_details extends work_profile{
int basic_salary;
salary_details(int id, String nm, String dpt, String j1,int bs){
super(id,nm,dpt,j1);
basic_salary=bs;
}
double calc(){
double gs;
gs=basic_salary+(basic_salary*0.4)+(basic_salary*0.1);
return(gs);
}
}
class salary_calc extends salary_details{
salary_calc(int id, String nm, String dpt, String j1,int bs)
Unit-III Inheritance, Interface & Package

{ super(id,nm,dpt,j1,bs); }
public static void main(String args[]){
salary_calc e1=new salary_calc(101,"abc","Sales","clerk",5000);
double gross_salary=e1.calc();
System.out.println("Empid :"+e1.empid);
System.out.println("Emp name :"+e1.ename);
System.out.println("Department :"+e1.dept);
System.out.println("Job :"+e1.job);
System.out.println("BAsic Salary :"+e1.basic_salary);
System.out.println("Gross salary :"+gross_salary);
}
}
3 Which are the ways to access package from another package? Explain with example. 4M
There are two ways to access the package from another package.
1. import package.*;
2. import package.classname;
1. Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but
not subpackages. The import keyword is used to make the classes and interfaces of another
package accessible to the current package.
E.g.
package pack;
public class A{
public void msg(){
System.out.println("Hello");
}}
import pack.*; ( Different
class B{ ways to
public static void main(String args[]){ access
A obj = new A(); package :1M
obj.msg(); &
} explanation
} of each :
2. Using packagename.classname 1 ½M )
If you import packagename.classname then only declared class of this package will be
accessible.
Eg.
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Unit-III Inheritance, Interface & Package

4 Write a java program to extend interface assuming suitable data. 6M


{{**Note: - Any other relevant program shall be considered**}}
interface A{
int x=20;
void display();
}
interface B extends A{
int y=30;
void show(); ( Creation of
} interface
class C implements B{ and extend
public void display() it: 4M
{ System.out.println("A's X="+x); } &
public void show(){ implementing
System.out.println("A's X="+x); into
System.out.println("B's Y="+y); class:2M)
}
public static void main(String args[]){
C obj=new C();
obj.display();
obj.show();
}
}
Unit-III Inheritance, Interface & Package

WINTER-18 (17515)
SRNO QUESTIONS MARKS
1 What is interface? Describe its syntax and features. 6M
Definition: Java does not support multiple inheritances with only classes.
Java provides an alternate approach known as interface to support concept of multiple
inheritance. An interface is similar to class which can define only abstract methods and final
variables.
Syntax:
access interface InterfaceName
{
Variables declaration;
Methods declaration;
} Definition:
Features: 1M,
multiple inheritance. 1M for
values are final. syntax and
associated with 2M for
them. Features

ects. It can only be inherited by a class.

2 What are packages in Java? Write a program to create a package and import the package in 8M
another class.
Package: Java provides a mechanism for partitioning the class namespace into more
manageable parts called package (i.e package are container for a classes). The package is both
naming and visibility controlled mechanism.
Package can be created by including package as the first statement in java source code. Any
classes declared within that file will belong to the specified package.
Syntax:
( Definition:
package pkg;
2M,
Here, pkg is the name of the package
any correct
package1:
Program
package package1;
with proper
public class Box{
logic: 6M )
int l= 5;
int b = 7;
int h = 8;
public void display()
{ System.out.println("Volume is:"+(l*b*h)); }
}
Unit-III Inheritance, Interface & Package

}
Source file:
import package1.Box;
class VolumeDemo{
public static void main(String args[]){
Box b=new Box();
b.display();
}
}
3 What is use of super and final with respect to inheritance? 4M
Super Keyword: The super keyword in Java is a reference variable which is used to refer
immediate parent class object. Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super reference variable.
Usage of Java super Keyword

Example:
class A{
int i;
A(int a, iont b)
{ i=a+b; }
void add()
{ System.out.println(“sum of a and b=”+i); }
}
class B extends A{ 2M for
int j; Use of
B(int a,int b, int c){ super
super(a,b); And
j=a+b+c; 2M for
} Final
void add(){ keyword
super.add();
System.out.println(“Sum of a, b and c is:” +j);
}
}

Final Keyword: A parameter to a function can be declared with the keyword “final”.
This indicates that the parameter cannot be modified in the function.
The final keyword can allow you to make a variable constant.
Example:
Final Variable: The final variable can be assigned only once. The value of a final variable can
never be changed.
final float PI=3.14;
Final Methods: A final method cannot be overridden. This means even though a sub class can
call the final method of parent class without any issues but it cannot override it.
Example:
Unit-III Inheritance, Interface & Package

class XYZ{
final void demo()
{ System.out.println("XYZ Class Method"); }
}
class ABC extends XYZ{
void demo()
{ System.out.println("ABC Class Method"); }
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}
The above program would throw a compilation error, however we can use the parent class
final method in sub class without any issues.
class XYZ{
final void demo()
{ System.out.println("XYZ Class Method"); }
}
class ABC extends XYZ{
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}

Output:
XYZ Class Method

final class: We cannot extend a final class. Consider the below example:
final class XYZ{ }
class ABC extends XYZ {
void demo()
{ System.out.println("My Method"); }
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}

Output:
The type ABC cannot subclass the final class XYZ
Unit-III Inheritance, Interface & Package

SUMMER-23 (22412)
SRNO QUESTIONS MARKS
1 Give syntax to create a package and accessing package in java. 2M
To Create a package follow the steps given below:
• Choose the name of the package
• Include the package command as the first line of code in your Java Source File.
• The Source file contains the classes, interfaces, etc. you want to include in the package
• Compile to create the Java packages

Syntax to create a package:


package nameOfPackage;
Example: syntax to
package p1; create a
Accessing Package: package-1
• Package can be accessed using keyword import. M and
• There are 2 ways to access java system packages: accessing
• Package can be imported using import keyword and the wild card(*) but drawback package-1
of this shortcut approach is that it is difficult to determine from which package a M
particular member name.

Syntax: import package_name.*;


For e.g. import java.lang.*;
• The package can be accessed by using dot(.) operator and can be terminated using
semicolon(;)

Syntax: import package1.package2.classname;


2 Describe the package in java with suitable example. 4M
• Java provides a mechanism for partitioning the class namespace into more manageable
parts called package (i.e package are container for a classes).
• The package is both naming and visibility controlled mechanism. Package can be created
by including package as the first statement in java source code.
• Any classes declared within that file will belong to the specified package.

Syntax: package pkg;


pkg is the name of the package
eg : package mypack; Description-
• Java uses file system directories to store packages. The class files of any classes which are 2 M,
declared in a package must be stored in a directory which has same name as package name. Example -
• The directory must match with the package name exactly. 2M
• A hierarchy can be created by separating package name and sub package name by a
period(.) as pkg1.pkg2.pkg3; which requires a directory structure as pkg1\pkg2\pkg3.
• The classes and methods of a package must be public.
• To access package In a Java source file, import statements occur immediately following
the package. Statement (if it exists) and before any class definitions.
• Syntax: import pkg1[.pkg2].(classname|*);

• Example:
Unit-III Inheritance, Interface & Package

package1:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h));
}
}
}
Source file:
import package1.Box;
class VolumeDemo
{
public static void main(String args[])
{
Box b=new Box();
b.display();
}
}
3 Describe interface in java with suitable example. 4M

Java does not support multiple inheritances with only classes. Java provides an alternate
approach known as interface to support concept of multiple inheritance. An interface is
similar to class which can define only abstract methods and final variables.
Syntax:
access interface InterfaceName
{
Variables declaration;
Methods declaration;
} Interface
Example: explanation
interface sports – 2 M, any
{ suitable
int sport_wt=5; example –
public void disp(); 2M
}
class test
{
int roll_no;
String name;
int m1,m2;
test(int r, String nm, int m11,int m12)
{
roll_no=r;
Unit-III Inheritance, Interface & Package

name=nm;
m1=m11;
m2=m12;
}
}
class result extends test implements sports
{

result (int r, String nm, int m11,int m12)


{
super (r,nm,m11,m12);
}
public void disp()
{
System.out.println("Roll no : "+roll_no);
System.out.println("Name : "+name);
System.out.println("sub1 : "+m1);
System.out.println("sub2 : "+m2);
System.out.println("sport_wt : "+sport_wt);
int t=m1+m2+sport_wt;
System.out.println("total : "+t);
}

public static void main(String args[])


{
result r= new result(101,"abc",75,75);
r.disp();
}
}

Output :

D:\>java result

Roll no : 101
Name : abc

sub1 : 75

sub2 : 75

sport_wt : 5

total : 155
Unit-III Inheritance, Interface & Package

Write a program to implement the following inheritance. Refer Fig. No. 1.

4 6M

interface Exam
{
int sports_mark=20;
}
class Student
{
String S-name;
int Roll_no, m1, m2, m3;
Student(String n, int a, int b, int c, int d)
{
S-name = n;
Roll_no = a;
m1 = b;
m2 = c;
m3 = d;
}
6 M for
void showdata()
correct
{
program
System.out.println("Name of student :"+S-name);
System.out.println("Roll no. of the students :"+Roll_no);
System.out.println(“Marks of subject 1:”+m1);
System.out.println(“Marks of subject 2:”+m2);
System.out.println(“Marks of subject 3:”+m3);
}
}
class Result extends Student implements Exam
{
Result(String n, int a, int b, int c, int d)
{
super(n, a, b, c, d );
}
void dispaly()
{
super.showdata();
Unit-III Inheritance, Interface & Package

int total=(m1+m2+m3);
float result=(total+Sports_mark)/total*100;
System.out.println(“result of student is:”+result);
}
}
class studentsDetails
{
public static void main(String args[])
{
Result r=new Result("Sachin",14, 78, 85, 97);
r.display();
}
}
Unit-III Inheritance, Interface & Package

SUMMER-22 (22412)
SRNO QUESTIONS MARKS
1 Define the interface in Java. 2M
Interface is similar to a class.
1M for each
It consists of only abstract methods and final variables.
point, Any two
To implement an interface a class must define each of the method Declared in the interface.
points
It is used to achieve fully abstraction and multiple inheritance in Java.
2 Enlist any four inbuilt packages in Java. 2M
1.java.lang
2.java.util ½ M for each
3.java.io package Any
4.java.awt four packages
5.java.net
6.java.applet
3 Explain single and multilevel inheritance with proper example. 4M
Single level inheritance: In single inheritance, a single subclass extends from a single
superclass.

Example :
class A
{
void display()
{
System.out.println(“In Parent class A”); 1M for each
} explanation
} 1M for each
class B extends A //derived class B from A example
{
void show()
{
System.out.println(“In child class B”);
}
public static void main(String args[])
{
B b= new B();
b.display(); //super class method call
b.show(); // sub class method call
}
}
Note : any other relevant example can be considered.
Unit-III Inheritance, Interface & Package

Multilevel inheritance:
In multilevel inheritance, a subclass extends from a superclass and then the same subclass
acts as a superclass for another class. Basically it appears as derived from a derived class.

Example:
class A
{
void display()
{
System.out.println(“In Parent class A”);
}
}
class B extends A //derived class B from A
{
void show()
{
System.out.println(“In child class B”);
}
}
class C extends B //derived class C from B
{
public void print()
{
System.out.println(“In derived from derived class C”);
}
public static void main(String args[])
{
C c= new C();
c.display(); //super class method call
c.show(); // sub class method call
c.print(); //sub-sub class method call
}
}
Note : any other relevant example can be considered.
4 Explain how to create a package and how to import it 4M
To create package following steps can be taken: 3M for steps to
1) Start the code by keyword „package‟ followed by package name. create
Example : package mypackage; 1M to import
2) Complete the code with all required classes inside the package with appropriate access
Unit-III Inheritance, Interface & Package

modifiers.
3) Compile the code with „javac‟ to get .class file.
Example: javac myclass.java to get myclass.class
4) Create a folder which is same as package name and make sure that class file of package
is present inside it. If not, copy it inside this folder.

To import the package inside any other program : Make use of import statement to
include package in your program. It can be used with „*‟ to gain full access to all classes
within package or just by giving class name if just one class access is required. Example :
import mypackage.myclass; or importmypackage.*;
5 How to create user defined package in Java. Explain with a suitable example. 6M
A java package is a group of similar types of classes, interfaces and
sub-packages
It also provides access protection and removes name collisions.
Creation of user defined package:
To create a package a physical folder by the name should be created in the computer.
Example: we have to create a package myPack, so we create a folder d:\myPack
The java program is to be written and saved in the folder myPack. To
add a program to the package, the first line in the java program should be package
<name>; followed by imports and the program logic.
package myPack;
import java.util;
public class Myclass {
//code
}
3M Package
Access user defined package:
creation (Note:
To access a user defined package, we need to import the package in our program. Once
Code snippet
we have done the import we can create the object of the class from the package and thus
can be used for
through the object we can access the instance methods.
describing) 3M
import mypack.*;
for Example
public class
MyClassExample{
public static void main(String a[]) {
(Note Any
Myclass c= new Myclass();
other similar
}
example can
}
be considered )
Example:
package package1;
public class Box
{
int l= 5;
int b = 7;
int h = 8;
public void display()
{
System.out.println("Volume is:"+(l*b*h));
}
}
Unit-III Inheritance, Interface & Package

Source file:
import package1.Box;
class volume {
public static void main(String args[])
{
Box b=new Box();
b.display();
}}
Unit-III Inheritance, Interface & Package

WINTER-23 (22412)
SRNO QUESTIONS MARKS
1 List out different ways to access package from another package. . 2M
There are three ways to access the package from outside the package.
Any 2 correct
• import package.*;
ways
• import package.classname;
–2M
• fully qualified name
2 Differentiate between method overloading and method overriding. 4M

4 M for any
four correct
point

3 Write a program to show the Hierarchical inheritance. 4M


import java.io.*; abstract class shape
{
float dim1,dim2; void getdata()
Unit-III Inheritance, Interface & Package

{
DataInputStream d=new DataInputStream(System.in); try
{
System.out.println("Enter the value of Dimension1: ");
dim1=Float.parseFloat(d.readLine()); System.out.println("Enter the value of Dimension2:
"); dim2=Float.parseFloat(d.readLine());
}
catch(Exception e)
{
System.out.println("General Error"+e);
}
}
void disp()
{
System.out.println("Dimension1= "+dim1); System.out.println("Dimension2= "+dim2);
} 4 M for correct
abstract void area(); program
} (Any relevant
class rectangle extends shape example can be
{ consider)
double area1; void getd()
{
super.getdata();
}
void area()
{
area1=dim1*dim2;
System.out.println("The Area of Rectangle is: "+area1);
}
}
class triangle extends shape
{
double area1; void getd()
{
super.getdata();
}
void area()
{
area1=(0.5*dim1*dim2);
System.out.println("The Area of Triangle is: "+area1);
}
}
class methodover1
{
public static void main(String args[])
{
rectangle r=new rectangle(); System.out.println("For Rectangle"); r.getd();
r.disp();
Unit-III Inheritance, Interface & Package

r.area();
triangle t=new triangle(); t.getd();
t.disp();
t.area();
}
}

OR

class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}

class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class JavaExample
{
public static void main(String args[])
{
B obj1 = new B(); C obj2 = new C(); D obj3 = new D();
//All classes can access the method of class A obj1.methodA();
obj2.methodA(); obj3.methodA();
}
}
Unit-III Inheritance, Interface & Package

Develop and Interest Interface which contains Simple Interest and Compound
6M
4 Interest methods and static final field of rate 25%. Write a class to implement those
methods.
import java.util.Scanner;
import static java.lang.Math.pow;
interface Interest
{
int roi=25;
public void simpleInterest(float principle,float time); public void
Creating
compoundInterest(float principle,float time);
correct
}
interface with-
public class InterestTest implements Interest
2M
{
Implementing
public void simpleInterest(float principle,float time)
interface-1M
{
Calculating
float si = (principle*roi*time)/100;
simple
System.out.println("Simple interested calculate by program is : " + si);
interest and
}
compound
public void compoundInterest(float principle,float time)
interest-
{
2M
double ci = principle * (Math.pow((1.0 +(roi/100)), time)) - principle;
System.out.println("Compound interested calculate by program is : " + ci);
Correct Main
}
method-1M
public static void main(String args[])
{
InterestTest i1 = new InterestTest(); i1.simpleInterest(1000,2);
i1.compoundInterest(1000,2);
}
}

You might also like