1 answer

List three principles that must be considered for designing classes in java. Illustrate each one with...

Question:

List three principles that must be considered for designing classes in java. Illustrate each one with an example.

Please be specific with the principles and detailed examples. Thank You


Answers

Answer : Three principles for Designing classes in java are :
1) Encapsulation
2) Inheritance
3) Polymorphism

1.Encapsulation : The process of encapsulating that is combining the variables and methods of the java class in to a single unit is known as encapsulation.
This principle is used to provide data security to the application or the program and hence it is also known as Data abstraction or Data hiding.

Code For Encapsulation:

public class carDetails {
   private String color;
   private String brand;

   public int getColor() {
      return color;
   }

   public String getBrand() {
      return brand;
   }

   public int setColor(String color) {
      this.color = color;
   }

   public String setBrand(String brand) {
      this.brand = brand;
   }
}

The above example consits of the getter and setter methods, where the getter methods are used to return the value to the user and setter methods are used to assign the values.

2.Inheriatnce : This designing principle of java classes provide code reusability to the application as well as the programmer.
Using Inheritance we need to write the functionality of an application again and again instead we can just provide the mostly used functionality to parent class and can inherit the functionality using Inheritance.
The class which contains all the mostly used functionality is known as the parent class or Base class and the class which inherits this properties is known as the child class or derived class.

Code for Inheritance :

class Teacher {
String subjectName;

Teacher(String subjectName) {
   this.subjectName = subjectName;
}

public void getSubjectName() {
   System.out.println("The Subject provided by the teacher is "+subjectName);
}
}

public class student extends Teacher {
student(String subjectName) {
    super(subjectName);
}

public static void main(String args[]) {
    student obj = new student("Maths")
   obj.getSubjectName();
}
}


In the above code the subject functionality of the teacher class i.e Parent class is being inherited by the student class i.e child class.
We can more derived class like the student class for e.g principal,manager etc.. and inherit the method of the teacher class.

3.Polymorphism : This deisgning principle of java classes provides the functionality of taking many different forms to the objects of the class.
The object which has more than one IS-A relationship is considered to be polymorphism. There are 2 types of polymorphism in java i.e Run time polymorphism and compile time polymorphism.
polymorphism allows to have same method name for more than one function.

Code for Polymorphism :

class A{
A(){
   System.out.println("This is A class");
}

public add(int a, int b){
   System.out.println("This method adds only int values from class A");
   return a+b;
}

public add(float a,float b){
   System.out.println("This method adds only float values from class A");
   return a+b;
}
}

class B extends A{
   B(){
    System.out.println("This is B class");  
   }

   public add(int a, int b){
   System.out.println("This method adds only int values from class B");
   return a+b;
   }

   public add(float a,float b){
   System.out.println("This method adds only float values from class B");
   return a+b;
   }
  

}

class Test {
   public static void main(String args[]){
   A obj1 = new A();
   System.out.println(obj1.add(5,4));
   System.out.println(obj1.add(5.2,4.2));
   B obj2 = new B();
   System.out.println(obj2.add(5,4));
   System.out.println(obj2.add(5.2,4.2));
   }

}


When we execute the above polymorphism code we will observe that it prints differnt kind of outputs for each function call even though the calculated value will be the same by both the objects.   

comment down for any query

please give thumbs up if this answer helps you

.

Similar Solved Questions

1 answer
Dynamics VISIONC 21333 (Kinetics and Kinematies of Particles ) he two blocks shown are originally at...
Dynamics VISIONC 21333 (Kinetics and Kinematies of Particles ) he two blocks shown are originally at rest. Neglecting the masses of the pulleys and the effect of friction in the pulleys and between block A and the horizontal surface, determine (a) the acceleration of each block, (b) the tension i...
1 answer
Please fill in the blanks (1) ~ (4). Thank you! On the monthly common payment, Question...
Please fill in the blanks (1) ~ (4). Thank you! On the monthly common payment, Question 1 (Default Points: 1 points) Assume that you borrow 30million JPY from some bank and repay under the plan of total equally payment to pay off at 30 years =360 months later. Calculate the following: Csatisiet (1)...
1 answer
Question 22 25 pts Consider the following structure. CH .CH.CH CH CH CHE CH, CH What...
Question 22 25 pts Consider the following structure. CH .CH.CH CH CH CHE CH, CH What is the relationship to the structure given below? CH3-CH-CH2-CH-CH-CH2-CH2-CH constitutional isomers. cis-trans Isomers configurational isomer they are the same compound they are different compounds...
1 answer
Please show work and make answer clear. i really appreciate the help!! In the following figure,...
please show work and make answer clear. i really appreciate the help!! In the following figure, a circular disk with a hole that's eccentrically placed is shown. The disk had an areal density p (mass per unit area) and rı = Express the mass moment of inertia (in-plane rotation), about O, i...
1 answer
Revenue and cash receipts journals; accounts receivable subsidiary and general ledgers Transactions related to revenue and...
Revenue and cash receipts journals; accounts receivable subsidiary and general ledgers Transactions related to revenue and cash receipts completed by Crowne Business Services Co. during the period April 2–30 are as follows: Apr. 2. Issued Invoice No. 793 to Ohr Co., $6,730. Apr. 5. Rece...
1 answer
Computer Science: Computer Architecture 3. Do the following problems: Consider a circuit with 4 binary inputs....
Computer Science: Computer Architecture 3. Do the following problems: Consider a circuit with 4 binary inputs. It counts the number of 1’s on its input and expresses (encodes or represents) the count as binary values on 2 output lines. a. Draw a truth table to represent the functions of the ci...
1 answer
EBook Consider the following network representation of a transportation problem: Des Moines 25 14 30 Jefferson...
eBook Consider the following network representation of a transportation problem: Des Moines 25 14 30 Jefferson City 16 > Kansas City 15 10 20 Omaha $ St Louis 10 Supplies Demands The supplies, demands, and transportation costs per unit are shown on the network. The optimal (cost minimizing) distr...
1 answer
The acceleration of a device depends on the time t in time (0 s, 50.0 s)...
The acceleration of a device depends on the time t in time (0 s, 50.0 s) according to the expression a(t) = a0 − bt. Parameter values ​​are a0 = 1.5 m / s and b = 3,000 × 10-2 1 / s. At time t = 0, the device is in place. Set the speed of the device at 16.0 s....
2 answers
QUESTION 1 How would the accounting equation be effected by a business paying a creditor? O...
QUESTION 1 How would the accounting equation be effected by a business paying a creditor? O increase liability, increase equity O increase asset, increase liability O decrease asset decrease equity O decrease asset, decrease liability QUESTION 4 Prepaid rent is what type of account? O revenue O liab...
1 answer
In 2003 for $12,509 when we in exponent of this machine is 0.5 and the cost...
In 2003 for $12,509 when we in exponent of this machine is 0.5 and the cost indeva 16. The approximate cost of a 500-lup motor in 2008 is closest to $17.678 b. S15,604 c. $27,705 d. $20,306 1. The approximate cost of the 500-hp motor in 2019 is closest to a $27,706 b. $25,055 c. $24,120 d $23,270 An...
1 answer
Cullumber Lakes Park is a private camping ground near the Mount Miguel Recreation Area. It has...
Cullumber Lakes Park is a private camping ground near the Mount Miguel Recreation Area. It has compiled the following financial information as of December 31, 2022 Service revenue (from camping fees) $132,600 Dividends $9,540 Sales revenue (from general store) 23,400 Notes payable 53,000 Accounts pa...
1 answer
A cube is measure 1 centimeter on each side and has a mass of 2 grams. Will it float on water?
A cube is measure 1 centimeter on each side and has a mass of 2 grams. Will it float on water?...
1 answer
Part A For the following gluconeogenesis reactions, select all of the coupled reactions. NOTE: Pi stands...
Part A For the following gluconeogenesis reactions, select all of the coupled reactions. NOTE: Pi stands for phosphate. o phosphoenolpyruvate + H20 = 2-phosphoglycerate 0 3-phosphoglycerate + ATP = 1,3-bisphosphoglycerate + ADP o pyruvate + HCO3 + ATP = oxaloacetate + ADP + Pi o fructose-6-phosphate...
1 answer
Name: /232 Student Number 9.) Which of the following pairs must be isomers? Explain your conclusion...
Name: /232 Student Number 9.) Which of the following pairs must be isomers? Explain your conclusion in each instance and indicate the type of isomerization present if applicable. (5 marks each) a.) [CrCl2(NHs)2(OH2)2]Cl and [CrCl2(OH2)2(NH3)2]Cl b.) [PtCl(NH3)Br2 and [PtBr2(NHa)4]Cl2 c.) [Co(NHs)e]C...