Question

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

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
List three principles that must be considered for designing classes in java. Illustrate each one with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • This Java program must use classes and object-oriented design (OOD) as I'm in a more advanced...

    This Java program must use classes and object-oriented design (OOD) as I'm in a more advanced Java class. (Sales Commission Calculator) A large company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. You’ve been supplied with a list of the items sold...

  • Principles of Total Quality There are three principles of total quality: customer focus, continuous improvement, and tea...

    Principles of Total Quality There are three principles of total quality: customer focus, continuous improvement, and teamwork. Using the South University Online Library, find three articles that describe current practices in healthcare organizations. Each article should illustrate one principle of total quality. Refer to professional journals such as Modern Healthcare, Provider Magazine, and Nursing Homes. Write a paper based on your review of the articles. Include the following in the paper: Describe each quality principle and show how it is...

  • What does the Java 2D API provide? List at least three classes that define Java2D shapes....

    What does the Java 2D API provide? List at least three classes that define Java2D shapes. (Advanced Java Language)

  • Java is an object-oriented programming language that enables us to define classes and to instantiate them...

    Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...

  • List the three major classes of carbohydrates and give a specific example of each. 2. Write the reaction (using wor...

    List the three major classes of carbohydrates and give a specific example of each. 2. Write the reaction (using words, not formulas) for the hydrolysis of sucrose. 3. Cellulose and amylose are both polysaccharides. What specific test can be used to differentiate between the two compounds? 4. What is the definition of a reducing sugar? 5. What purpose does the control test tube of water serve for the carbohydrate tests? Reminder: there is one more problem on the next page]

  • List and give specific examples of at least six ways you would use digital media to...

    List and give specific examples of at least six ways you would use digital media to promote a product in the candy industry. Be specific and detailed with your examples (e.g. don’t just say “use Facebook”) l Please be specific and detailed for each example

  • List three different ethical principles one should employ when confronted with an ethical conflict, and explain their us...

    List three different ethical principles one should employ when confronted with an ethical conflict, and explain their use employing an example you have personally experienced.

  • 9. Java coder Courtney is writing a program that derives a list of unique words contained...

    9. Java coder Courtney is writing a program that derives a list of unique words contained in a given text file: the word values themselves, and a total count for the file. What Collection type would be best suited for the job? Check only one) O (a) BinarySearch (b) List □ (c) Map (d) Set D (e) Queue 10.Software-Engineer Ellie is designing a similar program to Courtney. She needs to track a count for each unique word contained in a...

  • The following is for java programming. the classes money date and array list are so I are are pre...

    The following is for java programming. the classes money date and array list are so I are are pre made to help with the coding so you can resuse them where applicable Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT