Abstract Class:: Class Private Private Private Int Public Int This This This

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Abstract Class:

1. abstract class Employee {


2. private String name;
3. private String address;
4. private int number;
5.
6. public Employee(String name, String address, int number) {
7. System.out.println("Constructing an Employee");
8. this.name = name;
9. this.address = address;
10. this.number = number;
11. }
12.
13. public double computePay() {
14. System.out.println("Inside Employee computePay");
15. return 0.0;
16. }
17.
18. public void mailCheck() {
19. System.out.println("Mailing a check to " + this.name + " " +
this.address);
20. }
21.
22. public String toString() {
23. return name + " " + address + " " + number;
24. }
25.
26. public String getName() {
27. return name;
28. }
29.
30. public String getAddress() {
31. return address;
32. }
33.
34. public void setAddress(String newAddress) {
35. address = newAddress;
36. }
37.
38. public int getNumber() {
39. return number;
40. }
41. }
42. class Salary extends Employee {
43. private double salary; // Annual salary
44.
45. public Salary(String name, String address, int number, double
salary) {
46. super(name, address, number);
47. setSalary(salary);
48. }
49.
50. public void mailCheck() {
51. System.out.println("Within mailCheck of Salary class ");
52. System.out.println("Mailing check to " + getName() + " with
salary " + salary);
53. }
54.
55. public double getSalary() {
56. return salary;
57. }
58.
59. public void setSalary(double newSalary) {
60. if(newSalary >= 0.0) {
61. salary = newSalary;
62. }
63. }
64.
65. public double computePay() {
66. System.out.println("Computing salary pay for " + getName());
67. return salary/52;
68. }
69. }
70. public class abstract_employee {
71.
72. public static void main(String [] args) {
73. Salary s = new Salary("Vrushali Jani", "Vadodara, Gujarat", 3,
3600.00);
74. Employee e = new Salary("Neha Shinde", "Mumbai, Maharashtra",
2, 2400.00);
75. System.out.println("Call mailCheck using Salary reference
--");
76. s.mailCheck();
77. System.out.println("\nCall mailCheck using Employee
reference--");
78. e.mailCheck();
79. }
80. }

Interfaces (Using default,static and private methods)


interface Alarm {

default String turnAlarmOn() {


return "Turning the alarm on.";
}

default String turnAlarmOff() {


return "Turning the alarm off.";
}

interface Vehicle {

String getBrand(String brand);

String speedUp();

String slowDown();

default String calling_privateMethod() {


return showModel();
}
default String turnAlarmOn() {
return "Turning the vehicle alarm on.";
}

default String turnAlarmOff() {


return "Turning the vehicle alarm off.";

}
private String showModel() {
return "Car Model : BMW 3200 CS";
}
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}

class Car implements Vehicle , Alarm {

private String brand;

// constructors/getters

@Override
public String getBrand(String brand) {
return this.brand=brand;
}

@Override
public String speedUp() {
return "The car is speeding up.";
}

@Override
public String slowDown() {
return "The car is slowing down.";
}
public String turnAlarmOn() {
return Vehicle.super.turnAlarmOn() + " " + Alarm.super.turnAlarmOn();
}

@Override
public String turnAlarmOff() {
return Vehicle.super.turnAlarmOff() + " " + Alarm.super.turnAlarmOff();
}

public String calling_privateMethod() {


return Vehicle.super.calling_privateMethod();
}

class interface_methods
{
public static void main(String[] args) {
Vehicle car = new Car();
System.out.println(car.getBrand("CAR NAME : BMW"));
System.out.println(car.calling_privateMethod());
System.out.println(car.speedUp());
System.out.println(car.slowDown());
System.out.println(car.turnAlarmOn());
System.out.println(car.turnAlarmOff());
System.out.println("Calculated HorsePower of the Car is :
"+Vehicle.getHorsePower(2500,480));
}
}

Partial Implementation of Interface (Using Abstract Class)


interface Study{
void theory();
void practical();
}
abstract class Room implements Study{
void assignments();
@Override
public void theory() {
System.out.println("Theory Work in Class Room");
}
}

class Lab extends Room{

@Override
public void practical() {
System.out.println("Practical Work in Lab");

}
}
public class partial_interface{

public static void main(String []a) {

Lab L= new Lab();


L.theory();
L.practical();
}
}

Nested Class (Circuit)


//outer class
class Electronic
{
String circuitName;
String circuitType;
double circuitCost;
//constructor of outer class
Electronic(String name, String type, double cost)
{
this.circuitName = name;
this.circuitType = type;
this.circuitCost = cost;
}
String getCircuitName()
{
return this.circuitName;
}
//nested class
class Circuit
{
String circuitType;
double circuitCost;
void setCircuitType()
{
this.circuitType = "Transistor";
this.circuitCost = 430.0;
}
//get circuit type using this method
String getCircuitType()
{
return this.circuitType;
}
//get circuit cost using this method
double getCircuitCost()
{
return this.circuitCost;
}
}
}
public class NestedClass_Circuit
{
public static void main(String[] args)
{
Electronic elObj = new Electronic("Amplifier", "Integrated", 375.0);
Electronic.Circuit circuit = elObj.new Circuit();
//printing here the values before reset it
System.out.println("\nCircuit Name : " + elObj.circuitName);
System.out.println("\nCircuit Type : " + elObj.circuitType);
System.out.println("\nCircuit Cost : " + elObj.circuitCost);
//resetting some value
circuit.setCircuitType();
//printing here the values before reset it
System.out.println("\n\nCircuit Name : " + elObj.getCircuitName());
System.out.println("\nCircuit Type : " + circuit.getCircuitType());
System.out.println("\nCircuit Cost : " + circuit.getCircuitCost());
}
}
Nested Class (Volume)
//Outer class
class Volume
{
double x, y, z;
Volume(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
// Nested inner class
class Measurement{
//method to calculate the total area
void totalArea(double i, double j)
{
System.out.println("\nArea for the provided container is " + (i *
j));
}
//method to calculate the total volume
void totalVolume(double i, double j, double k)
{
System.out.println("\nVolume for the provided container is " + (i *
j * k));
}
}
}
public class NestedClass_Volume
{
public static void main(String[] args)
{
//passing here all the parameter to constructor
Volume volObj = new Volume(30.0, 25, 18.0);
Volume.Measurement volMeasureObj = volObj.new Measurement();
// calculating total area
volMeasureObj.totalArea(volObj.x, volObj.y);
// calculating total volume
volMeasureObj.totalVolume(volObj.x, volObj.y, volObj.z);
}
}

You might also like