Found 34494 Articles for Programming

What is the difference between super and this, keywords in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

517 Views

The this is a keyword in Java which is used as a reference to the object of the current class. Using it you can − Differentiate the instance variables from local variables if they have same names, within a constructor or a method. Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation. Example class Superclass { int age; Superclass(int age) { this.age = age; } public void ... Read More

Can constructors be inherited in Java?

Monica Mona
Updated on 30-Jul-2019 22:30:20

3K+ Views

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors. Example public interface InterfaceTest { public InterfaceTest(){ } public abstract void display(); public abstract void show(); } Still, if you try to write constructors in an interface it will generate a compile time error. Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected InterfaceTest(){ ^ 1 error C:\Sample>

Can interfaces have constructors in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

541 Views

No, interfaces can’t have constructors for the following reasons − All the members of an interface are abstract, and since a constructor cannot be abstract. Still, if you try to write a constructor within an interface it will generate a compile time error. Example public interface InterfaceTest { InterfaceTest(){ } public abstract void display(); public abstract void show(); } Error C:\Sample>javac InterfaceTest.java InterfaceTest.java:2: error: expected public InterfaceTest(){ ^ 1 error

What is the super() construct of a constructor in Java?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

367 Views

The super keyword is similar to this keyword. Following are the scenarios where a super keyword is used. It is used to differentiate the members of superclass from the members of the subclass if they have same names. It is used to invoke the superclass constructor from the subclass. Whenever you want to call the constructor of the superclass from a method or another constructor you can do so as: Example class Person { Person(String name) { System.out.println("Hello "+ name); } } class Student ... Read More

How do you get a directory listing sorted by creation date in Python?

Rajendra Dharmkar
Updated on 11-Sep-2023 16:23:26

6K+ Views

In the process of managing files and directories in Python, there are often situations where you need to obtain a directory listing sorted by creation date. The task of sorting files and directories by their creation timestamps can be beneficial for various purposes, such as analyzing the most recently added files or organizing data based on chronological order. Python has several methods and techniques to achieve this goal effectively and efficiently. By making use of the "os" module, the "pathlib" module, or third−party libraries, you can effortlessly obtain a sorted directory listing based on creation date. In this exhaustive article, ... Read More

How to declare, create, initialize and access an array in Java?

Sharon Christine
Updated on 25-Feb-2020 10:56:19

243 Views

You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}

How to know/change current directory in Python shell?

Sarika Singh
Updated on 26-Aug-2023 08:05:42

38K+ Views

A portable method of interacting with the operating system is offered through the OS Python Module. The module, which is a part of the default Python library, contains tools for locating and modifying the working directory. The following contents are described in this article. How to obtain the current working directory: os.getcwd() Changing the current working directory: os.chdir() The __file__ function returns the path to the current script file (.py). Getting the current working directory- os.getcwd() The function os.getcwd() returns as a string str the absolute path to Python's current working directory. "Get current working directory" (getcwd) refers ... Read More

How to check if a file is a directory or a regular file in Python?

Rajendra Dharmkar
Updated on 11-Sep-2023 16:35:54

430 Views

When working with files and directories in Python, it's a crucial point to distinguish between regular files and directories. Knowing the type of each file is most essential for implementing different file handling operations, such as reading, writing, or navigating through directory structures. Python makes available several methods and techniques to determine if a given path points to a directory or a regular file. By utilizing the "os" module, the "pathlib" module, or other specialized functions, you can easily differentiate between these two types and perform the necessary operations accordingly. In this exhaustive article, we will explore different methods to ... Read More

What is the purpose of a default constructor in Java?

varma
Updated on 12-Mar-2020 05:21:59

2K+ Views

Default constructors in Java:A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a method. However, constructors have no explicit return type. There are two types of constructors namely −parameterized constructors − Constructors with arguments.no-arg constructors − Constructors without arguments.Example Live Demopublic class Sample{    int num;    Sample(){       num = 100;    }    Sample(int num){       this.num = num;    }    public static void main(String args[]){       System.out.println(new Sample().num);       System.out.println(new Sample(1000).num);    } }Output100 1000Default ConstructorIt ... Read More

How to safely open/close files in Python?

Rajendra Dharmkar
Updated on 03-Aug-2023 12:50:29

2K+ Views

When it comes to file operations in Python, adhering to best practices is crucial to ensure the security and integrity of your data. Mishandling files can lead to data corruption, resource leaks, and even security vulnerabilities. This article aims to delve into the best practices for safely opening and closing files in Python, accompanied by five illustrative code examples with stepwise explanations to enhance your understanding. Utilizing the 'with' Statement Python offers a convenient approach to opening and closing files using the 'with' statement. The 'with' statement guarantees automatic closure of the file once the code block inside ... Read More

Advertisements