Page 494 - Computer science 868 Class 12
P. 494
Member Functions/Methods
Series(…) : parameterised constructor to initialize the data members of both the classes
void calsum() : calculates the sum of the given series
void display() : displays the data members of both the classes
Assume that the super class Number has been defined. Using the concept of inheritance, specify the class Series giving the
details of the constructor(…),void calsum() and void display().
The super class, main function and algorithm need NOT be written.
Ans. class Series extends Number
{
long sum;
Series(int nn)
{
super(nn);
sum=0;
}
void calsum()
{
for (int i=1;i<=n;i++)
sum=sum+factorial(i);
}
void display()
{
super.display();
System.out.println("sum of the series="+sum);
}
}
12. What is an interface? How is it different from a class? [ISC 2019, 2017]
Ans. Interface in Java is a blue print of a class as it specifies what to do, but does not specify how to do. It is a mechanism to achieve
abstraction by hiding certain features and revealing only some important details.
Class Interface
A class can be instantiated i.e., objects of a class can be An interface cannot be instantiated i.e. objects cannot be
created. created.
Classes do not support multiple inheritance. The interface supports multiple inheritance.
13. A superclass Product has been defined to store the details of a product sold by a wholesaler to a retailer. Define a subclass Sales
to compute the total amount paid by the retailer with or without fine along with service tax. [ISC 2017]
Some of the members of both classes are given below:
Class name : Product
Data Members/Instance variables
name : stores the name of the product
code : integer to store the product code
amount : stores the total sale amount of the product (in decimals)
Member Functions/Methods
Product (String n, int c, double p) : parameterized constructor to assign data members: name = n, code = c and
amount = p
void show() : displays the details of the data members
Class name : Sales
Data Members/Instance variables
day : stores number of days taken to pay the sale amount
tax : to store the sen ice tax (in decimals)
totamt : to store the total amount (in decimals)
492492 Touchpad Computer Science-XII

