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
.