Showing posts from May, 2014

Using Protected Access Modifier Correctly in Java

The attributes or methods with protected access modifier in parent class are inherited in subclass, hence the subclass can access them. When the subclass or parent class object is created in a class that reside in different package, we can not access the protected attributes from the object reference. So protected access modifier works like private as far access from user-classes (i.e. where we instantiate an object of that class) are concern. It works like public as far inheritance is concern, as its accessible in subclass code. That is why it comes between private and public access. So here comes a fundamental question, should we make our instance attributes protected if we want to make them available to subclasses? From good software design perspective, the straight answer is BIG NO, we should not make the instance attributes protected even if we want to make them inheritable, why? For the same reason as discussed in previous sections i.e. it breaks the encapsulation principle,

Role of Access Modifiers in Java Inheritance

What methods or instance attributes are inherited is decided by their access modifiers in parent class. Private attributes and methods are not inherited in subclasses. All public attributes and methods are inherited in sub-classes. So you noticed, by making ID and name private in Person class, we stopped their direct availability in subclass Student. And by making get/set methods public for both attributes, we allowed Student class to read/update the inherited attributes without violating the encapsulation principle. I hope you already know, if a class contains some public methods/attributes, they can be called or accesses from reference of that class's object, no matter where the object of that class is created e.g. a static main method or any where else. There is another access modifier which is specially used with inheritance, its called protected . Protected access modifier is lighter than private but stronger than public access. Protected attributes or methods in parent cl

Inheritance without Compromising Encapsulation

Encapsulation is another very important concept in OOP that states, a class must hide its data from other software components and should not allow reading or writing instance attributes directly. And this access must be given via public get and set methods. (I would share the Encapsulation in separate article). If you see the Student constructor, we have clearly violated the encapsulation principle for the inherited attributes by accessing those attributes directly. So a better design would making the ID and name attribute private in Person class and allow the subclasses to initialize them via parent class constructor. Lets see the updated code again: package; public class Person { private int ID; private String name; public Person() { } public Person(int ID, String name) { this.ID = ID; = name; } public int getID() { return ID; } public void setID(int ID) { this.ID =

How Inheritance Works in Java - Basic Sample Code

I hope you have well understood the basic objective and benefits of using inheritance from my previous article , in this article we would see simple example code. Let us assume we are building an information system to manage different operations in an academic institution. It would involve different type of entities e.g. students, teachers, managers, etc. Lets define a general component i.e. Person first, the Person class would hold the common attributes of different type of related entities. Lets see how it would look like: package; public class Person { int ID; String name; } We have defined ID and name attribute because these are common among student, teacher and manager. So we can create Student class by inheriting it from Person class. In this way we add only those attributes in Student which are not present in Person class. Because, the attributes in Person class would be inherited in Student class. Here is the student class: pack

Java Database Connectivity JDBC using MySQL - Video Tutorial in Urdu

Following topics are covered in the tutorials: Connecting with database Understanding MySQL Driver - MySQL Connector/J Creating connection object from DriverManager class Creating Statement object Running Select query Creating ResultSet object Iterating the ResultSet Running update query Dunning delete query Running Insert query Understanding MySQL to Java data types mappings

Using MySQL Workbench to Create Database and Running SQL Queries - Urdu

In below Video tutorial, following is demonstrated: 1. Important Settings while installing MySQL Server 2. How use MySQL Workbench to create database, tables, and run different SQL queries Some text contents used in tutorial: 1. Database Server, MySQL Community Edition 5.6 After Installation, A configuration wizard would run. Setup following: 1. Set port to 3306 2. Set root user account 3. Check auto start option 2. MySQL Workbench 3. MySQL Connector/J

Inheritance in Java

Inheritance is one of the fundamental concepts of Object Oriented Programming paradigm. As the name suggest, in inheritance, we make new classes by inheriting functionality from already defined classes. As you know, a class can contains attributes and methods, so inheritance facilitate developers to include already defined attributes or methods in new classes without copying the code from existing classes. Soon I would explain this concept with the help of code sample. Lets first see, what are the main benefits of inheritance. Benefits of Inheritance There are 2 fundamental benefits of inheritance:  It provides Code Re-usability, hence less time to develop new components. As we make new software components (also called classes) by extending existing classes. It improves the quality of new components. After the software is developed, one of the most important task is Testing it as per requirements. It requires resources who test the developed software components. When we create