Skip to main content
edited title
Link
Tulains Córdova
  • 39.6k
  • 13
  • 102
  • 157

Why instantiate an object to a Basebase class rather than a specific Sub classsubclass?

removed excessive white-space and other small improvements
Source Link

For example  :

 URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json");  HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();  connection.connect();   InputStream inputStream = connection.getInputStream();  Reader reader = new InputStreamReader(inputStream);  int contentLength = connection.getContentLength();  char[] charArray = new char[contentLength];  reader.read(charArray);  String responseData = new String(charArray); 

Here we could have created InputStreamReader instead for parent class Reader:

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelperSQLiteOpenHelper in an activity:

public class Helper extends SQLiteOpenHelper { ... } public class DrinkActivity extends Activity { protected void onCreate(...) { … } SQLiteOpenHelper helper = new Helper(this); ... } 

Instead of referencing the base class SQLiteOpenHelper here, why not create

Helper helper = new Helper(this); 

after all HelperHelper extends SQLiteOpenHelperSQLiteOpenHelper.

What is the benefit of coding it this way?

For example  :

 URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json");  HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();  connection.connect();   InputStream inputStream = connection.getInputStream();  Reader reader = new InputStreamReader(inputStream);  int contentLength = connection.getContentLength();  char[] charArray = new char[contentLength];  reader.read(charArray);  String responseData = new String(charArray); 

Here we could have created InputStreamReader instead for parent class Reader:

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelper in an activity:

public class Helper extends SQLiteOpenHelper { ... } public class DrinkActivity extends Activity { protected void onCreate(...) SQLiteOpenHelper helper = new Helper(this); ... } 

Instead of referencing the base class SQLiteOpenHelper here, why not create

Helper helper = new Helper(this) 

after all Helper extends SQLiteOpenHelper.

What is the benefit of coding it this way?

For example:

URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json"); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); int contentLength = connection.getContentLength(); char[] charArray = new char[contentLength]; reader.read(charArray); String responseData = new String(charArray); 

Here we could have created InputStreamReader instead for parent class Reader:

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelper in an activity:

public class Helper extends SQLiteOpenHelper {  } public class DrinkActivity extends Activity { protected void onCreate() { … } SQLiteOpenHelper helper = new Helper(this);  } 

Instead of referencing the base class SQLiteOpenHelper here, why not create

Helper helper = new Helper(this); 

after all Helper extends SQLiteOpenHelper.

What is the benefit of coding it this way?

Tweeted twitter.com/StackProgrammer/status/714340549466177537

What the point of initializing a Why instantiate an object to morea Base class instead of morerather than a specific SubClassSub class?

For example :

 URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json"); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); int contentLength = connection.getContentLength(); char[] charArray = new char[contentLength]; reader.read(charArray); String responseData = new String(charArray); 

Here we could have created InputStreamReaderInputStreamReader instead for parent class ReaderReader:

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelper in an activity:

public class Helper extends SQLiteOpenHelper { ............................ ... } public class DrinkActivity extends Activity {  protected void onCreate(.........)  SQLiteOpenHelper helper = new Helper(this); ..... ... ...} 

insteadInstead of referencing to the base class SQLiteOpenHelper ,SQLiteOpenHelper here, why not create Helper helper = new Helper(this)

Helper helper = new Helper(this) 

after all Helper extends the SQLiteOpenHelper.

The point i'm trying to make is whatWhat is the benefit of coding init this manager ?way?

What the point of initializing a object to more Base class instead of more specific SubClass?

For example :

 URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json"); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); int contentLength = connection.getContentLength(); char[] charArray = new char[contentLength]; reader.read(charArray); String responseData = new String(charArray); 

Here we could have created InputStreamReader instead for parent class Reader

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelper in an activity

public class Helper extends SQLiteOpenHelper { ............................... public class DrinkActivity extends Activity { protected void onCreate(.........) SQLiteOpenHelper helper = new Helper(this); ........ ... 

instead of referencing to the base class SQLiteOpenHelper , here why not create Helper helper = new Helper(this)

after all Helper extends the SQLiteOpenHelper

The point i'm trying to make is what is the benefit of coding in this manager ??

Why instantiate an object to a Base class rather than a specific Sub class?

For example :

 URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json"); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); InputStream inputStream = connection.getInputStream(); Reader reader = new InputStreamReader(inputStream); int contentLength = connection.getContentLength(); char[] charArray = new char[contentLength]; reader.read(charArray); String responseData = new String(charArray); 

Here we could have created InputStreamReader instead for parent class Reader:

InputStreamReader reader = new InputStreamReader(inputStream); 

Or for another example when creating a SQLiteOpenHelper in an activity:

public class Helper extends SQLiteOpenHelper {  ... } public class DrinkActivity extends Activity {  protected void onCreate(...)  SQLiteOpenHelper helper = new Helper(this);  ... } 

Instead of referencing the base class SQLiteOpenHelper here, why not create

Helper helper = new Helper(this) 

after all Helper extends SQLiteOpenHelper.

What is the benefit of coding it this way?

Source Link
mhrzn
  • 235
  • 1
  • 3
  • 8
Loading