Generalization and Specialization
The Generalization and Specialization relationships are both reciprocal and hierarchical. In other words, Generalization and specialization are the inverse of each other. It only differs in the design process.
Generalization is a bottom-up design process whereas Specialization is a top-down design process.
Generalization is a bottom-up design process whereas Specialization is a top-down design process.
They both belong to inheritance.
Like Parrot and Sparrow specialize Bird, and Bird generalizes from Parrot and Sparrow. These relationships are hierarchical because they create a relationship tree.
If we are moving down in the hierarchy we will get Specialization and if we will go up in the hierarchy then we will get Generalization. In java, Generalization can relate to the “extends” keyword
Note: - Generalization is the process of extracting common features from two or more classes, and combining them into a generalized super class. Common features can be attributes, associations, or methods.
Finally we can say, a super class is a generalization of its subclass (es). Inversely, a subclass is a Specialization of its super class.
Representation:-
Bird.java
package com.gaurav.generalization;
public abstract class Bird {
public void fly(){
System.out.println("Birds can fly");
}
public void twoLegs(){
System.out.println("Birds have two legs");
}
}
Parrot.java
package com.gaurav.generalization;
public class Parrot extends Bird {
public String sweetVoice() {
return "Very sweet voice";
}
}
Sparrow.java
package com.gaurav.generalization;
public class Sparrow extends Bird {
public String twitterVoice() {
return "having twittering voice";
}
}
Realization in Java
Circle.java
package com.gaurav.realization;
public class Circle implements Figure {
public double pie = 3.27;
public double radius = 5.0;
public double area() {
return pie * radius * radius;
}
}
Representation:-
Generalization can be represented as below:-
Realization can be represented as below:-
Example :- In the below example, we can see that DataRetrieval.class is completely dependent on ConnectionUtil.class.
ConnectionUtil.java
package com.gaurav.dependency;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private Connection con;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
} catch (Exception e) {
System.out.println("Exception message is-" + e.getMessage());
}
return con;
}
}
DataRetrieval.java
package com.gaurav.dependency;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataRetrieval {
private ResultSet rs = null;
private Statement stmt = null;
public String completeProcess(ConnectionUtil connectionUtil) {
String status = "Process Completed";
try {
Connection con = connectionUtil.getConnection();
String queryStr = "SELECT * FROM employee";
stmt = con.createStatement();
rs = stmt.executeQuery(queryStr);
while (rs.next()) {
System.out.println("Employee Id is->" + rs.getInt(1));
System.out.println("Employee Name is-" + rs.getString(2));
System.out.println("Employee Email is-" + rs.getString(3));
}
} catch (Exception e) {
status = "Execution failed";
System.out.println("Exeception message in DataManipulation is - "
+ e.getMessage());
}
return status;
}
public static void main(String... args) {
ConnectionUtil connectionUtil = new ConnectionUtil();
DataRetrieval dataManipulation = new DataRetrieval();
String str = dataManipulation.completeProcess(connectionUtil);
System.out.println("Execution status - " + str);
}
}
Note:- For the execution of above program, we have to execute the create and insert query for the creation of employee table and insertion of data in that table. I am using MySql, So here i am placing Create and Insert query for creation and insertion of data in employee table.
CREATE TABLE employee (
id DOUBLE ,
name VARCHAR (50),
email VARCHAR (100)
);
INSERT INTO employee (id, name, email) VALUES('1','GAURAV','gaurav@yahoo.co.in');
Representation:-
Bird.java
package com.gaurav.generalization;
public abstract class Bird {
public void fly(){
System.out.println("Birds can fly");
}
public void twoLegs(){
System.out.println("Birds have two legs");
}
}
Parrot.java
package com.gaurav.generalization;
public class Parrot extends Bird {
public String sweetVoice() {
return "Very sweet voice";
}
}
Sparrow.java
package com.gaurav.generalization;
public class Sparrow extends Bird {
public String twitterVoice() {
return "having twittering voice";
}
}
Realization in Java
Realization is the relationship between the class and the interface. It is achieved using the “implements” keyword in Java. A realization relationship connector appears as a dashed line with an unfilled arrowhead.
Figure.java
package com.gaurav.realization;
public interface Figure {
public double area();
}
public interface Figure {
public double area();
}
Rectangle.java
package com.gaurav.realization;
public class Rectangle implements Figure {
public double length = 10.0;
public double width = 10.0;
public double area() {
return length * width;
}
}
public class Rectangle implements Figure {
public double length = 10.0;
public double width = 10.0;
public double area() {
return length * width;
}
}
Circle.java
package com.gaurav.realization;
public class Circle implements Figure {
public double pie = 3.27;
public double radius = 5.0;
public double area() {
return pie * radius * radius;
}
}
Representation:-
Generalization can be represented as below:-
Realization can be represented as below:-
Dependency in Java
Dependency relationship shows that a class is dependent on another class for its existence or implementation. This relationship is shown as a dotted line with an arrow from source class to the dependent class.
Dependency can be represented as below:-
Representation:-
Example :- In the below example, we can see that DataRetrieval.class is completely dependent on ConnectionUtil.class.
ConnectionUtil.java
package com.gaurav.dependency;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private Connection con;
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "root");
} catch (Exception e) {
System.out.println("Exception message is-" + e.getMessage());
}
return con;
}
}
DataRetrieval.java
package com.gaurav.dependency;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class DataRetrieval {
private ResultSet rs = null;
private Statement stmt = null;
public String completeProcess(ConnectionUtil connectionUtil) {
String status = "Process Completed";
try {
Connection con = connectionUtil.getConnection();
String queryStr = "SELECT * FROM employee";
stmt = con.createStatement();
rs = stmt.executeQuery(queryStr);
while (rs.next()) {
System.out.println("Employee Id is->" + rs.getInt(1));
System.out.println("Employee Name is-" + rs.getString(2));
System.out.println("Employee Email is-" + rs.getString(3));
}
} catch (Exception e) {
status = "Execution failed";
System.out.println("Exeception message in DataManipulation is - "
+ e.getMessage());
}
return status;
}
public static void main(String... args) {
ConnectionUtil connectionUtil = new ConnectionUtil();
DataRetrieval dataManipulation = new DataRetrieval();
String str = dataManipulation.completeProcess(connectionUtil);
System.out.println("Execution status - " + str);
}
}
Note:- For the execution of above program, we have to execute the create and insert query for the creation of employee table and insertion of data in that table. I am using MySql, So here i am placing Create and Insert query for creation and insertion of data in employee table.
CREATE TABLE employee (
id DOUBLE ,
name VARCHAR (50),
email VARCHAR (100)
);
INSERT INTO employee (id, name, email) VALUES('1','GAURAV','gaurav@yahoo.co.in');
No comments:
Post a Comment