coderhalt
coderhalt
CoderHalt
40 posts
Don't wanna be here? Send us removal request.
coderhalt · 4 years ago
Link
0 notes
coderhalt · 4 years ago
Text
Method Overloading in Java
Method Overloading is a feature OOPS that allows a class to have more than one method having the same name, if their argument type are different in either number or type. These methods are called overloaded methods and this feature is called method overloading in Java. Let's see example Advantages of Method Overloading - It reduces execution time because the binding is done during compilation time. - Flexibility is achieved by overloading mechanism. - Code reusability is achieved, hence it saves memory in program. - Code complexity is reduced. Hence provides consistency of program. Read the full article
0 notes
coderhalt · 4 years ago
Text
Method Overriding in Java
If a child class has same method as declared in the parent class, it is known as method overriding in java. Overriding is done so that a child class can give its own implementation to a method which is already defined in the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method in Java.
Tumblr media
Rules for Method Overriding - The argument type list should be exactly the same as that of the overridden method. - The return type should be the same or a co-variant type of the return type declared in the original overridden method in the superclass. - The access level scope can not be decrease, it can be similar or higher than the overridden method's access level. For example: If the superclass method is declared public then the overriding method in the sub class cannot be either private or protected. - Instance method can be overridden only if they are inherited by the subclass. - A method declared as final or private cannot be overridden. - A method declared as static cannot be overridden but it can be re-declared. - If a method cannot be inherited, then it cannot be overridden too. - If overriding method throws any exceptions then its parent class method must throw same or its parent type checked exception. - Constructors cannot be overridden in Java Advantages of method overriding : - Helps in writing generic code based on parent class or interface as object resolution happens at runtime - Provides multiple implementation of same method and can invoke parent class overridden method using super keyword - Defines what behavior a class can have and implementation of behavior has been taken care by class which is going to implement. Read the full article
0 notes
coderhalt · 4 years ago
Text
Abstract class vs interface
Abstract class and interface both are used to achieve abstraction in java where we can declare the abstract methods. Abstract class and interface both can't be instantiated (can not create object). But there are many differences between abstract class and interface in java that are given below.
Tumblr media
Abstract ClassInterface An abstract class can extend only one class or one abstract class at a time in java. An interface can extend any number of interfaces at a time at multilevel.An abstract class can extend another regular class or abstract class. An interface can only extend another interface.An abstract class can have both abstract method and concrete methods. An interface can have only abstract methods and not concrete method.In abstract class “abstract” keyword is used to declare a method as an abstract In an interface keyword “abstract” is optional to declare for a method An abstract class can have protected and public abstract methods An interface can have only public abstract methodsAn abstract class can have variable with any access modifiersinterface can have only public static final (constant) variablee.g public abstract class Vehicle{ public abstract void modelName(); }e.g public interface Vehicle{ void modelName(); } Read the full article
0 notes
coderhalt · 4 years ago
Text
Package in Java
A package in java is like a container that organizes a set of related classes and interfaces. We can define our own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes, interfaces etc. to prevent from naming collision.
Tumblr media
Package naming convention - Package name should be a lowercase letter such as java, lang, io, class, etc. - If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang according to java naming conventions. Advantage of Java Package - Java packages are used to categorize the classes and interfaces for easy maintainability. - It provides access protection in program. - package removes naming collision between program. - The classes contained in the packages of another program can be reused Built-in Package in Java Built-in packages are existing java packages that come along with the JDK (Java devalopement kit) for e.g. java.io, java.lang, java.util, java.net, java.sql, java.swing, etc. Read the full article
0 notes
coderhalt · 4 years ago
Text
Access modifiers in Java
In Java, access modifiers are used to set the accessibility of classes, interfaces, variables, methods, constructors, data members. We can change the access level of variables, constructors, methods, and class by applying the access modifier with it. There are four types of access modifiers in java ModifierDescriptionPublicdeclarations are visible everywhere including outside package.Defaultdeclarations are visible only within the same package.Privatedeclarations are visible within the same class only.Protecteddeclarations are visible within the package or all subclasses of another package.
Tumblr media
Public When methods, variables, classes, are declared public, then we can access them from anywhere either within same package and from outside package. The public access modifier has no scope of restriction. Read the full article
0 notes
coderhalt · 4 years ago
Text
Encapsulation in Java
Encapsulation in java is the process of wrapping the data and code in a group. In Encapsulation concept, the variables of a class are always hidden from other classes. data members can only be accessed using the methods of their current class. We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.
Tumblr media
Advantages of Encapsulation in Java - We can make the class read-only or write-only by setter or getter method. It means our class variables can have only read or write permission by choice of using the method. - Control Over Data – Can control on stored data, if you want a positive integer only then can write the logic in the method. - Data Hiding – By declaring a Variable (data) private we can hide data for other classes. - Easy Testing – Can do easily unit testing. - Flexible: You can change one part of the code without affecting other parts of code. Let's see the example of a encapsulated class in java. Read the full article
0 notes
coderhalt · 4 years ago
Text
Inheritance in Java
Inheritance is one of the Basic Concepts of OOPs in which one object can have all properties and behaviors of the parent object in java. It’s creating a parent-child relationship between two classes using inheritance. The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is inherited is known as superclass (parent or base class). The extend keyword is used to perform inheritance in Java. The syntax of Java Inheritance extends keyword is used to inherit the properties of parent class. Following is the syntax of extends keyword. class Subclass-name extends Superclass-name { //methods and fields } Note:The child class inherits all the members and methods that are declared as public or protected. If the members or methods of super class are declared as private then the child class cannot use them directly. The private members can be accessed only in its own class. Such private members can only be accessed using public or protected getter and setter methods defined in super class. Benefits of Inheritance - Inheritance helps in code reusability. The child class can use the code defined in the parent class without re-writing it. - Inheritance can save time and effort of programmer as the main code need not be written again. - Inheritance provides a clear model structure which is easy to understand for developer and client. - Inheritance leads to less development and maintenance costs. Read the full article
0 notes
coderhalt · 4 years ago
Text
Data Hiding in Java
In simple words, data hiding is an object-oriented programming technique of hiding internal object details i.e. data members (Variables) in Java. Data hiding guarantees the restriction of data access to class members & maintain object integrity. By declaring data members as private access modifiers we can achieve data hiding in java. We can use getter method to get access of data members and setter method to change or set the variable. Without these 2 method (getter and setter method) we can not access and modify private data members outside of class. Read the full article
0 notes
coderhalt · 4 years ago
Text
Interface in Java
An interface is a complete abstract class that includes a group of methods without a body, so it is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface and not the method body. It is also used to achieve multiple inheritance in Java (Only interface allows multiple inheritance in java). Interfaces are syntactically similar to classes in java, but we cannot create instance (objects) of an Interface because their methods are declared without any body. Advantages of Interface - It Support multiple inheritance in java. - It helps to achieve fully abstraction. - Abstraction is used to achieve loose coupling. Syntax interface Language { public void getName(); } NOTE : Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default. Interface Key Points - Methods inside a interface must not be static, final, native or strictfp. - All variables declared inside interface are by default public, static and final. - All methods declared inside interfaces are by default public and abstract, even if we don't use public or abstract keyword. - Interface can extend one or more other interface same time. - Interface cannot implement a class. - Interface can be nested inside another interface. Read the full article
0 notes
coderhalt · 4 years ago
Text
Constructor in Java
A constructor is a special type of method that is used to initialize objects in java. The constructor is called when an object of a class is created. At the time of calling constructor, memory for the object will be allocated in the memory. Unlike Java methods, a constructor always has the same name as that of the class and does not have any return type. There are some rules for the constructor. - Constructor name must be the same as its class name. - A Constructor has no return type not even void type. - A constructor cannot be with abstract, static, final, and synchronized modifiers. - Only public, default, private, protected modifiers is applicable. Types of Java constructors There are two types of constructors in Java: - Default constructor (no-arg constructor) - Parameterized constructor Default constructor in Java If a Java class does not contains at least one constructor in this case the java compiler generates a default constructor to initialize the objects. The default constructor is used to provide the default values to the object created inside the constructor. Note : - We can not use both default and our own generated constructor same time in program. - It will be always no - arg constructor - Access modifiers for default constructor will always be same as class modifiers. (e.g : Read the full article
0 notes
coderhalt · 4 years ago
Text
super keyword in Java
The super keyword is used in subclasses ( child class ) to access superclass members (variables, constructors and methods) in Java. Learning super keyword will be very easier after having the knowledge of inheritance in Java. Usage of Java super Keyword - super can be used to refer immediate parent class instance variable. - It can be used to invoke immediate parent class method. - super() can be used to invoke immediate parent class constructor. Read the full article
0 notes
coderhalt · 4 years ago
Text
this keyword in Java
this keyword in java is used to refer to the current object inside a method or a constructor. Main purpose of this is to remove ambiguity between the instance variables and parameters and this keyword resolves the problem of ambiguity. Usage of java this keyword - Used to refer current class instance variable. - Used to invoke current class method (implicitly) - Used to invoke current class constructor. - It can be passed as an argument in the method call. - It can be passed as argument in the constructor call. - It can be used to return the current class instance from the method. Read the full article
0 notes
coderhalt · 4 years ago
Text
Static keyword in Java
static keyword is mainly used for memory management in java. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method in whole class. The static keyword belongs to the class so every static variable and method can be called directly inside a class. Java static variable When you declare a variable as static keyword, then a single copy of the variable will be shared among all objects at the class level. Static variables are, global variables and static variables can be created at class-level only. Java Static Methods Static Methods can access class variables(static variables) without creating object(instance) of the class, however non-static methods and non-static variables can only be accessed by creating objects.Static methods can be accessed directly in static and non-static methods in Java. Read the full article
0 notes
coderhalt · 4 years ago
Text
final keyword in Java
final keyword is a non-access modifier used for finalizing the implementations of classes, methods, and variables. Lets understand it with variables, method and classes.  Java final variable final variables in java are nothing but constants. We cannot change the value of a final variable once it is initialized in program. Example class FinalVariable { public static void main(String args) { // create a final variable final int age = 32; // try to change the final variable age = 45; System.out.println("Age is : " + age); } } In the above program, we have created a final variable named age. And we tried to change the value of the final variable in code. Read the full article
0 notes
coderhalt · 4 years ago
Text
Method in Java
A method is a set of code which can be called or invoked anywhere in a program, just by using the method’s name. It provides the reusability of code in java. It is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation and we can use this method many times. Types of Method There are two types of methods in Java : - Predefined Method - User-defined Method Predefined Methood In Java, predefined methods are the method that is already defined by Java Developer in the Java class libraries is known as predefined methods. It is also known as the standard library method or built-in method. We can directly call and use these methods just by calling them in the program at any point. For e.g. length(), equals(), compareTo(), sqrt(), String toLowerCase(), etc. Read the full article
0 notes
coderhalt · 4 years ago
Text
Class and Object in java
Everything in Java programming is associated with class and object, along with its attributes and methods. To understand it let's take a example : in real life, a car is an object. The car has properties, such as weight, speed, seating capacity and color, and methods, such as drive and brake. Class in Java A class is a user defined blueprint or like a prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of that class. In general, class declarations can include these components, in order A class in Java can contain : - Blocks - Methods - Constructors - Fields - Nested class and interface Read the full article
0 notes