Found 34492 Articles for Programming

Can we override a private or static method in Java

Nancy Den
Updated on 27-Jun-2020 12:48:55

7K+ Views

No, we cannot override private or static methods in Java.Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.ExampleLet us see what happens when we try to override a private method − Live Democlass Parent {    private void display() {       System.out.println("Super class");        } } public class Example extends Parent {    void display() // trying to override display() {       System.out.println("Sub class");    }    public static void main(String[] args) {       Parent obj = new Example(); ... Read More

Declare static variables and methods in an abstract class in Java

Rishi Rathor
Updated on 27-Jun-2020 12:51:19

6K+ Views

If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.A static variable is a class variable. A single copy of the static variable is created for all instances of the class. It can be directly accessed in a static method.An abstract class in Java is a class that cannot be instantiated. It is mostly used as the base for subclasses to ... Read More

Restrictions applied to Java static methods

Nishtha Thakur
Updated on 26-Jun-2020 15:50:46

3K+ Views

If the static keyword is applied to any method, it becomes a static method.If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.There are a few restrictions imposed on a static methodThe static method cannot use non-static data member or invoke non-static method directly.The this and super cannot be used in static context.The static method can access only static type data ... Read More

Object Serialization with Inheritance in Java Programming

Nitya Raut
Updated on 26-Jun-2020 15:57:47

723 Views

Serialization is the process of changing the state of an object into the byte stream so that the byte stream can return back into a copy of the objectIn Java, an object is said to be serializable if its class or parent classes implement either the Serializable interface or the Externalizable interface.Deserialization is converting the serialized object back into a copy of the object.There are three cases of Object Serialization with inheritance.The child class is automatically serializable if the parent class is serializableA child class can still be serialized even if the parent class is not serializableIf we want the ... Read More

Disassembler for Python bytecode

Anvi Jain
Updated on 27-Jun-2020 14:24:11

2K+ Views

The dis module in Python standard library provides various functions useful for analysis of Python bytecode by disassembling it into a human-readable form. This helps to perform optimizations. Bytecode is a version-specific implementation detail of the interpreter.dis() functionThe function dis() generates disassembled representation of any Python code source i.e. module, class, method, function, or code object.>>> def hello(): print ("hello world") >>> import dis >>> dis.dis(hello) 2    0 LOAD_GLOBAL 0 (print)      3 LOAD_CONST 1 ('hello world')      6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)      9 POP_TOP      10 LOAD_CONST 0 (None)      13 RETURN_VALUEBytecode ... Read More

Accessing The Unix/Linux password database (pwd)

Vrundesha Joshi
Updated on 27-Jun-2020 14:24:42

261 Views

The pwd module in standard library of Python provides access to the password database of user accounts in a Unix/Linux operating system. Entries in this Password database are atored as a tuple-like object. The structure of tuple is according to following passwd structure pwd.h file in CPython APIIndexAttributeMeaning0pw_nameLogin name1pw_passwdOptional encrypted password2pw_uidNumerical user ID3pw_gidNumerical group ID4pw_gecosUser name or comment field5pw_dirUser home directory6pw_shellUser command interpreterThe pwd module defines the following functions −>>> import pwd >>> dir(pwd) ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'getpwall', 'getpwnam', 'getpwuid', 'struct_passwd']getpwnam() − This function returns record in the password database corresponding to the specified user name>>> pwd.getpwnam('root') pwd.struct_passwd(pw_name ... Read More

Built-in objects in Python (builtins)

Anvi Jain
Updated on 27-Jun-2020 14:28:23

2K+ Views

The builtins module is automatically loaded every time Python interpreter starts, either as a top level execution environment or as interactive session. The Object class, which happens to be the base class for all Python objects, is defined in this module. All built-in data type classes such as numbers, string, list etc are defined in this module. The BaseException class, as well as all built-in exceptions, are also defined in it. Further, all built-in functions are also defined in the built-ins module.Since this module is imported in the current session automatically, normally it is not imported explicitly. All the built-in ... Read More

Top-level script environment in Python (__main__)

Nitya Raut
Updated on 27-Jun-2020 14:28:42

457 Views

A module object is characterized by various attributes. Attribute names are prefixed and post-fixed by double underscore __. The most important attribute of module is __name__. When Python is running as a top level executable code, i.e. when read from standard input, a script, or from an interactive prompt the __name__ attribute is set to '__main__'.>>> __name__ '__main__'From within a script also, we find a value of __name__ attribute is set to '__main__'. Execute the following script.'module docstring' print ('name of module:', __name__)Outputname of module: __main__However, for an imported module this attribute is set to name of the Python script. ... Read More

Python Utilities for with-statement contexts (contextlib)

Daniol Thomas
Updated on 27-Jun-2020 14:29:17

310 Views

contextlib module of Python's standard library defines ContextManager class whose object properly manages the resources within a program. Python has with keyword that works with context managers. The file object (which is returned by the built-in open() function) supports ContextManager API. Hence we often find with keyword used while working with files.Following code block opens a file and writes some data in it. After the operation is over, the file is closed, failing which file descriptor is likely to leak leading to file corruption.f = open("file.txt", "w") f.write("hello world") f.close()However same file operation is done using file's context manager capability ... Read More

The Python Debugger (pdb)

Jennifer Nicholas
Updated on 27-Jun-2020 14:38:10

2K+ Views

In software development jargon, 'debugging' term is popularly used to process of locating and rectifying errors in a program. Python's standard library contains pdb module which is a set of utilities for debugging of Python programs.The debugging functionality is defined in a Pdb class. The module internally makes used of bdb and cmd modules.The pdb module has a very convenient command line interface. It is imported at the time of execution of Python script by using –m switchpython –m pdb script.pyIn order to find more about how the debugger works, let us first write a Python module (fact.py) as follows ... Read More

Advertisements