Inheritance Iamneao Unit 1
Inheritance Iamneao Unit 1
1. class B + class A {}
3. class B extends A {}
ans:
class B extends A {}
Q2.A class member declared protected becomes a member of a subclass of which type?
1. public member
2. private member
3. protected member
4. static member
ans:
private member
class A {
public int i;
public int j;
A()
i = 1;
j = 2;
class B extends A {
int a;
B()
super();
class super_use {
Ans:
12
In the above example super() in Class B, call the A’s class constructor so I in initialized with 1 and j is
initialized with 2
class A {
public int i;
private int j;
class B extends A {
void display() {
super.j = super.i + 1;
class inheritance {
obj.i=1;
obj.j=2;
obj.display();
Explanation:
by using super keyword we can access A’s class member such as i and j .but here in above program j
is declared as private .So by using super.j we cant deal with j in ClassA
Which represent valid class headers that would be found in this hierarchy?
ans:
public class ScriptedShow extends TelevisionShow {. . .
Q6. What happens if a subclass attempts to override a private method from its superclass?
1. Compile-time Error
class B extends A {
void display() {
System.out.println("B display()");
}
}
class C extends B {
void display() {
System.out.println("C display()");
}
}
4. None of these
2. “has a” relationship
3. “want to be” relationship
ans:
“is a kind of” relationship
CMREC_Java_Unit 1_COD_Inheritance_Overriding
Q1.
Problem Statement
Alice is managing an online store and wants to implement a program in the pricing system for her
products. She has a base class called Product with a public double attribute price.
Additionally, she has a subclass called DiscountedProduct, which extends Product and includes a
private double attribute discountRate.
This subclass has a method called calculateSellingPrice() to determine the final selling price after
applying the discount.
Formula: Discounted Selling Price = Price * (1 - Discount Rate)
Solution:
import java.util.Scanner;
class Product {
public double price;
}
class ProductPricing {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (sellingPrice >= 0) {
System.out.printf("Rs. %.2f%n", sellingPrice);
} else {
System.out.println("Not applicable");
}
scanner.close();
}
}
The PremiumSubscription class inherits from the Subscription class and has a method,
calculateMonthlyCost(), to determine the total cost.
Note
import java.util.Scanner;
class Subscription {
public double monthlyCost;
public double serviceTax;
public double extraFeatureCost;
}
scanner.close();
}
}
Q3.
Problem Statement
Mary is managing a business and wants to analyze its profitability. She has a regular business model
and a seasonal one. Mary is using a program that calculates and compares the profit margins of both
models based on revenue and costs.
The program defines a base class BusinessUtility with a method for calculating the profit margin and
a derived class SeasonalBusinessUtility that overrides the margin calculation method to include an
additional seasonal adjustment.
Note
class BusinessUtility {
public double calculateMargin(double revenue, double cost) {
double margin = ((revenue - cost) / revenue) * 100;
return margin;
}
}
scanner.close();
}
}
CMREC_Java_Unit 1_CY_Inheritance_Overriding
Q1.
Problem Statement
Adhi, a travel enthusiast, wishes to calculate the travel distance of his car based on its speed and fuel
capacity. He decides to create a program using single inheritance.
The Vehicle class holds attributes for speed and fuel capacity, while the Car class extends Vehicle and
includes a method to calculate travel distance.
Adhi inputs speed and fuel capacity, and the program displays the details, including the calculated
travel distance.
Note: Travel Distance = Speed * Fuel Capacity.
Solution:
import java.util.Scanner;
class Vehicle {
public double speed;
public double fuelCapacity;
}
scanner.close();
}
}
Q2.
Problem Statement
Teena's Retail Store has implemented a Loyalty Points System to reward customers based on their
spending. The program includes two classes: Customer and PremiumCustomer.
Loyalty points for Regular Customers are calculated as one point per ten units spent, while Premium
Customers, through the overridden method in the PremiumCustomer class, receive double points.
For Regular Customers:
Loyalty points are calculated as one point for every ten units of currency spent
Loyalty Points = Amount Spent / 10
For Premium Customers:
Premium customers receive double the loyalty points compared to regular customers. That is two
points for every ten units of currency spent.
Loyalty Points = 2 * (Amount Spent / 10)
Solution:
import java.util.Scanner;
class Customer {
public int calculateLoyaltyPoints(int amountSpent) {
return amountSpent / 10;
}
}
Customer customer;
if (isPremium.equals("yes")) {
customer = new PremiumCustomer();
} else {
customer = new Customer();
}
System.out.println(loyaltyPoints);
}
}
CMREC_Java_Unit 1_PAH_Inheritance_Overriding
Q1.
Problem Statement
import java.util.Scanner;
class Parent {
void fun(int n) {
int i, j, k = 0, sum = 0;
int a[] = new int[10];
while (n != 0) {
i = n % 10;
a[k++] = i * i;
n = n / 10;
}
System.out.print(sum);
}
}
class Pizza {
private double basePrice;
private double toppingCost;
scanner.close();
}
}