Masai Inheritance Assignment Code
Question 1: class Relationship
import java.util.Scanner;
class User {
String city, state;
int pincode;
// Default constructor
User() {}
// Parameterized constructor
User(String city, String state, int pincode) {
this.city = city;
this.state = state;
this.pincode = pincode;
}
// Getter methods
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getPincode() {
return pincode;
}
}
class Customer extends User {
String name, email;
long mobileno;
// Default constructor
Customer() {}
// Parameterized constructor
Customer(String city, String state, int pincode,
String name, String email, long mobileno) {
super(city, state, pincode); // Call parent class constructor
this.name = name;
this.email = email;
this.mobileno = mobileno;
}
// Getter methods
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public long getMobileNum() {
return mobileno;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input from the user
System.out.print("Enter Customer City: ");
String city = scanner.nextLine();
System.out.print("Enter Customer State: ");
String state = scanner.nextLine();
System.out.print("Enter Customer Pincode: ");
int pincode = scanner.nextInt();
scanner.nextLine(); // Consume leftover newline
System.out.print("Enter Customer Name: ");
String name = scanner.nextLine();
System.out.print("Enter Customer Email: ");
String email = scanner.nextLine();
System.out.print("Enter Customer Mobile Number: ");
long mobileNum = scanner.nextLong();
// Creating a Customer object using the parameterized constructor
Customer cus = new Customer(city, state, pincode,
name, email, mobileNum);
// Displaying details using getter methods
System.out.println("Customer Name is: " + cus.getName());
System.out.println("Customer Email is: " + cus.getEmail());
System.out.println("Customer Mobile Number is: " + cus.getMobileNum());
System.out.println("Customer City is: " + cus.getCity());
System.out.println("Customer State is: " + cus.getState());
System.out.println("Customer Pincode is: " + cus.getPincode());
scanner.close();
}
}
Question 2: power of super
Code Paste here!!!
Question 3:
Code Paste here!!!
Question 4:
Code Paste here!!!
Question 5:
Code Paste here!!!
0 Comments