Skip to main content

12 posts tagged with "CS"

View All Tags

Crossing abstraction barrier between parent and child class

· 5 min read

Motivation

This article is inspired by a question I received in a programming methodology class. In this class, in which we write Java code to solve programming exercises, we have the constraint that every attribute of a class should be private and final. It means there is no access to the field outside of the class, and no modification is allowed once this field is initialized. This strict requirement is put in place to enforce immutability when constructing a class object in Java.

Sooner or later, when the exercises get more complex, we tend to move on to an OOP solution whereby multiple classes are constructed and organized with the help of inheritance. The problem then arises when there is a need to access this private final field in the parent class from a subclass. What should we do then?

To give a concrete example, let's say we have the following classes:

class Parent {
private final int value;

Parent(int value) {
this.value = value;
}
}

class Child extends Parent {
Child(int value) {
super(value);
}

int add(int another) {
return super.value + another; // UNABLE TO ACCESS!
}
}

What should we do if the child class wants to access value from the parent?

Solutions

Change modifier

The simplest way to deal with that is to change the access modifier from private to something else - perhaps public or protected. This solution can be legitimate depending on the context. In some cases, perhaps it is perfectly normal to expose this value to other classes.

Add a getter method

From the Oracle's Java tutorial on inheritance

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

So, another possible solution is to have a getter method in the parent class and make that method public. This way child classes (and technically other classes) will have access via the getter. So a quick example will be:

class Parent {
private final int value;

Parent(int value) {
this.value = value;
}

public int getValue() {
return this.value;
}
}

class Child extends Parent {
Child(int value) {
super(value);
}

int add(int another) {
return super.getValue() + another; // CAN ACCESS!
}
}

Having a getter method can be beneficial in the sense that even though now a "private" field is exposed, you still have one layer of abstraction over it. The users of the getter method do not need to know how that value is generated, which can be manipulated (if needed) by some complex preprocessing steps in the getter method. Also, the underlying private field could change drastically and yet the users of the getter method are unaware.

Rethink code design

Lastly, this problem may be a signal to rethink if there is a legitimate need to access a private final field. Given a parent-child relationship, sometimes it's difficult to be clear about which field/method should reside in which classes.

  • Would it be better to have the field in the child class instead?
  • Can we shift what the child class wanted to do with value into the parent class as a general method that the child class can inherit and possibly override?

A better code design might suggest that the private final field can stay as is, maintaining an abstraction barrier between the parent and the child class. One example solution is then:

class Parent {
private final int value;

Parent(int value) {
this.value = value;
}

int add(int another) {
return this.value + another;
}
}

class Child extends Parent {
Child(int value) {
super(value);
}

int add(int another) { // will work if this method is omitted as well,
return super.add(another); // as it will be inherited
}
}

Anti pattern

A problematic walkaround that some might come up with is to redeclare the same field in the child class.

class Parent {
private final int value;

Parent(int value) {
this.value = value;
}
}

class Child extends Parent {
private final int value;

Child(int value) {
super(value);
this.value = value;
}

int add(int another) {
return this.value + another; // will work but not recommended
}
}

This works but is arguably a bad design because it does not make use of inheritance to reduce any duplicates between shared properties. It also could result in the values (that meant to represent the same thing) going out of sync, especially if these fields were not declared as final.

Conclusion

When I was asked the motivating question, my immediate response was: "make a public getter method". To which I was then asked a follow-up question:

  • Why do we resort to using a public getter method, when we want to keep the field private?

Which got me thinking:

  • Why can't private fields be inherited?

This article is a reminder for me to ask the "why" questions more often, and explore the reasons for the answers.

CS3216 "The Last Lecture"

· 5 min read

A few years ago, I chanced upon the video "The Last Lecture" given by Randy Pausch. Randy, a computer science professor at Carnegie Mellon University, delivered a lecture titled "Really Achieving Your Childhood Dreams" in which he talked about his life lessons. The one thing that I took away from his talk was his point on failure and setbacks. In particular, I printed a photo of a brick wall and taped it on the wall right in front of my desk to remind me that:

"Brick walls are there for a reason: they let us prove how badly we want things". I was, therefore, excited to enroll in CS3216, a module modeled after a course taught by Randy. Now it's over => finally time to review and reflect! If you don't know what this module is about, see here for the official course website and assignment details. Below I use "A1", "A2" etc to denote the four major module assignments (again, full and official details are available here).

A1

The assignment required us to find a problem worth solving and design a solution for it. Even though we do not have to implement the solution, it was not a breeze. In a short amount of time, we have to (as a team) decide on a probable idea and validate it with the target audience. I have never done so many user interviews before and I have to say that the whole experience drives home the point that users matter. Defining a problem that needs to be solved and having potential customers that are ready to give advice and feedback are not that easy. But, they are truly valuable. Taking our team's project on improving the module registration workflow as an example, our testers pointed out various blind spots in our design, where we thought that a certain feature would be useful to the target audience but none of them actually noticed it during testing. These 'talk to your users" practices made me realize that when we build products for other human beings, it makes no sense to not listen to them attentively. I may have overlooked the users way too often in pet projects or assignments of school modules. A1 was a much-needed reminder.

A2

In this assignment, we had to identify an innovation on the market and make a presentation for it. The added challenge is to do it the Pecha Kucha way (20 slides, auto-advance every 20 seconds). While this is certainly a test of our presentation skills, the technologies that we discussed and heard from our peers were what I found to be interesting. Our team gave a presentation on Neuralink, a "mind-blowing" piece of technology that aimed to create a human-computer interface to drastically improve the lives of paralyzed patients. During the research, I realized that even though the idea of an implant that inserts threads into areas of the brain can deter many of us, the methodical approach with the help of precision surgical robots gave me great confidence that the future may be wild. It was refreshing to look at the current work done by others in the industry and ponder about what counts as innovative today.

A3 & A4

A3 is an assignment that required us to build a progressive web application, from "front" to "back". Not only do we have to think fast, but we also need to act even faster. In retrospect, I think this assignment is all about execution. Within the short 2-3 weeks, the team needs to produce an application that works with a database and handles authentication, among other things. Personally, I got to work on the backend and played around with Django REST Framework. Knowing web development and being familiar with the relevant tools beforehand are going to be helpful.

Following the end of A3, we moved on to our final project phase. A4 is an open-ended challenge to solve a problem with a (creative) solution. I may have hinted in my previous paragraphs that finding the right problem to solve is hard, but an innovative solution is equally hard to come by. I am not going into the details about these two assignments because, at this time in the semester, the fatigue and the workload started to drain all my energy. Jokes aside, I think my experience may not generalize well. Of course, I learned about how to work in a team and deliver a product. But, more importantly, I learned that life is more than CS3216/School/things at hand.

Guest Lectures & Writing assignments

The weekly lectures and writing assignments turned out to be rather enjoyable. I learned many things from the various guest speakers. One notable aspect of it is how "CS" or technical people view problems and optimize for a solution. I joined in the lament that "people problem" is the harder problem to solve. On writing assignments, it was fun to try, over the semester, to appeal to Uncle Soo's taste and get that coveted full marks for each writing.

Final thoughts

Just like the last lecture given by Randy, CS3216 is all about head fakes. What you will learn will be drastically different from the intended outcome. Coming into this module, many people, myself included, were looking forward to practicing and gaining technical skills in a team setting. In the end, this module may have disguised itself as a project module, but it is not all about the projects you build, it is all about YOU.

P.S This essay was written as part of my final writing assignment. All I can say is...What a module!

CS2101 Critical Reflection Essay

· 4 min read

While CS2103T is about technical skills and CS2101 is imbued with insights around the topics of communication, both aimed to teach me how to work with others effectively. The former opened my eyes to working in a technical environment and the latter taught me how to interact with others to strive towards excellence in that environment. My reflection will be deeply involved in how the theme of persuasive presentation connects the dots in the module CS2101.

The first part of the module introduced me to persuasion stemming from being audience-centered. Before taking this module, I have never believe in the thinking that in order to persuade someone, one has to be perform strategic needs analysis or conduct pre-presentation surveys. I valued more on being interesting and informational in a presentation or a speech. That led to a rather average performance during my OP1 presentation which I focused on squeezing in all the content about conflict management styles and disregarded the audience profile. It was not effective as I did not put a premium on connecting with the audience. I learned from this experience that persuasive presentation should start with identifying the target audience and also identifying with the target audience. It would be better if I had analyzed the audience who were my fellow computing students, and constructed my content based on their needs and the benefits that they might get out of from learning about the various management styles.

Following from keeping the target audience in mind, persuasion also requires careful consideration of the purpose that the speaker is trying to achieve. After learning about the audience's preconceived notions and their expectations, a convincing presentation should focus on aspects of the completing values framework (CVF) and employ rhetorical appeals. During OP2 product demo, I tried to strike a balance between the informational and promotional aspects of CVF. On top of presenting the features of the product, I empathized on the design considerations and how they were catered to solve problems that potential users might have. I also utilized "Pathos" by opening my presentation with a relatable story that the group of computing students who were the audience could understand. In my explanation, I also included a reference to a famous design principle named "KISS" to establish credibility in my claims. I ripped the benefits of applying what I learned in the module to organize my presentation to be context and purpose driven.

The last part of persuasion in a presentation comes from having an unified and cohesive delivery with strong teamwork. The difficulty in a team presentation context is with transitioning of ideas and the entire flow of presentation. While I understood the importance of coding styles that were taught in CS2103T to make our code appear unified in the technical context, I realized that I lacked that consideration when preparing for a team presentation. Often times the team members decide on what they want to say before a proper discussion on the flow and purpose of the presentation. This often results in minor issues such as the lack of sign posting and unnatural transition between the team members and sometimes serious drawbacks when team members repeat what was communicated earlier or angled their sharing on a separate front. In this aspect, I think while following a structure such as motivated sequence pattern (MSP) is important to achieving soundness in logic and coherence, the planning of talking points among the team members and constant review of the integration between different teammates is crucial.

In conclusion, I was able to identify and practice many of the said ideas in the opportunities provided by the module. Keeping in mind the devices that help build a persuasive presentation, there are also other important aspects of communication such as participating in audience interaction and providing constructive feedback. Communication skills require practice and I am grateful to be armed with the relevant knowledge to further practice what was taught in the module in the future.