Skip to main content

Erasure

Generic programming means to write code that can be reused for objects of many different types.
Before javaSE 5 generics was achieved with inheritance, afterwards to implement generics the compiler applies type Erasure
Erasure: "as if a correction mad by erasing" is the process of translating or rewriting code that uses generics into non-generic one, all info between angle bracket is erased "Type variables are erased & replaced by their bonding types 'Object for variables without bounds' ".
NB:
-bridge method to preserve polymorphism .
"assume a class or interface that extends a parameterized class, and I want to override a method that exist in the parameterized class, after erasure, the method signatures do not not match "the one in the parameterized class is replaced by their bounding type"
public void setXXX(Object x) //one for parameterized class "generic class" after erasure
public void setXXX(ClassType x) // my overridden method
different argument, different methods. to solve
a Java compiler generates a bridge method to ensure that sub-typing works as expected  
public void setXXX(Object x){ setXXX( (ClassType)  x);} //auto-generated by erasure

Comments

Popular posts from this blog

The post-office & the postman

If we were to talk about old messaging system where there exist post-office, postman & mailbox. each component had its own functionality that we looked for when trying to visualize how those component where to interact in a computerized version. Simple scenario: Mail is added in mail box Postman arrive pick mails from his area mailboxes and take them to the post-office. Post-office organize mails by areas. Postman takes mails related to his area "distribute it in mailboxes". A person can go to post-office and  pick his own mail "in case of failure or wishes for early delivery". Mapping in a computerized version: Scenario: Observer design pattern which can use push or pull scenario, to inform those whom are registered for an event about its occurrence. Component: Post-Office = Message-Broker Post-Office-Box = Message-Storage-Validity Mailbox = Topic/Queue Postman !!! where's the postman ? Apache kafka act as a message broker which d...

Not all Ps sting

  If someone meant to say Ps and pronounce it Bees. would this confuse you :). Ps is for the P that is the start of Properties and Practice Each application should have some properties and follow certain practices. Properties: Below are 5 properties we should try to have in our application with a small description of how to include them Scalable, Scale => Increase workload (horizontally scaling) Statless, no state should be shared among different application instances,  Concurrency, concurrent processing = Threads. Loosely coupled, decompose the system into modules, each has minimal dependencies on each other "modularization", encapsulating code that changes together "High cohesion".  API first, Interfaces, implementation can be changed without affecting other application. favor distribution of work across different teams.  Backing Services, "DB, SMTP, FTP ..." , treating them as attached resources, meaning they can easily be changed. Manageable, changi...

exploring black box

Once loved watching a comic old movie, where a clause was often said:"what's in this ring box ?" accompanied with a surprising answer:"an elephant is in this ring box " :o Reflection discover whats in the black box "an object", could know what is in an object as if a mirror that reflects it content "ability to examine the run-time properties of the object " small ex wanted to return a generic list containing result of a sql query "as if hibernate" List<T>list=new ArrayList<T>(); ResultSetMetaData metadata=resultSet.getMetaData(); String[] columnName=new String[metadata.getColumnCount()]; for(int i=0;i<columnName.length;i++){//column name equivalent to object instance variable      columnName[i]=metadata.getColumnLabel(i+1); } int index=0; T t; while (resultSet.next()) {   list.add((T)cls.newInstance()); t=list.get(index); for(int i=0;i<columnName.length;i++){//initializati...