Inheritance (IS-A) IN Java

Inheritance (IS-A) Inheritance is one of the key features of Object Oriented Programming. Inheritance provided mechanism that allowed a class to inherit property of another class. When a Class extends another class it inherits all non-private members including fields and methods. Inheritance in Java can be best understood in terms of Parent and Child relationship, …

Inheritance (IS-A) IN Java Read More »

Aggregation (HAS-A) in java

Aggregation (HAS-A) HAS-A relationship is based on usage, rather than inheritance. In other words, class A has-a relationship with class B, if code in class A has a reference to an instance of class B. Example class Student { String name; Address ad; } Here you can say that Student has-a Address. Student class has …

Aggregation (HAS-A) in java Read More »

Method Overriding In Java

Method Overriding When a method in a sub class has same name and type signature as a method in its super class, then the method is known as overridden method. Method overriding is also referred to as runtime polymorphism. The key benefit of overriding is the abitility to define method that’s specific to a particular …

Method Overriding In Java Read More »

instanceof operator

In Java, instanceof operator is used to check the type of an object at runtime. It is the means by which your program can obtain run-time type information about an object. instanceof operator is also important in case of casting object at runtime. instanceof operator return boolean value, if an object reference is of specified …

instanceof operator Read More »

Command line argument in Java

The command line argument is the argument passed to a program at the time when you run it. To access the command-line argument inside a java program is quite easy, they are stored as string in String array passed to the args parameter of main() method. Example class cmd { public static void main(String[] args) …

Command line argument in Java Read More »

Java Package

Package are used in Java, in-order to avoid name conflicts and to control access of class, interface and enumeration etc. A package can be defined as a group of similar types of classes, interface, enumeration and sub-package. Using package it becomes easier to locate the related classes. Package are categorized into two forms Built-in Package:-Existing …

Java Package Read More »

Abstract class in java

If a class contain any abstract method then the class is declared as abstract class. An abstract class is never instantiated. It is used to provide abstraction. Although it does not provide 100% abstraction because it can also have concrete method. Syntax : abstract class class_name { } Abstract method Method that are declared without …

Abstract class in java Read More »

Interface In Java

Interface Interface is a pure abstract class.They are syntactically similar to classes, but you cannot create instance of an Interface and their methods are declared without any body. Interface is used to achieve complete abstraction in Java. When you create an interface it defines what a class can do without saying anything about how the …

Interface In Java Read More »

Nested Class

A class within another class is known as Nested class. The scope of the nested is bounded by the scope of its enclosing class. Static Nested Class A satic nested class is the one that has static modifier applied. Because it is static it cannot refer to non-static members of its enclosing class directly. Because …

Nested Class Read More »

String class function

String class function The following methods are some of the most commonly used methods of String class. charAt() charAt() function returns the character located at the specified index. String str = “studytonight”; System.out.println(str.charAt(2)); Output : u equalsIgnoreCase() equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or lower case doesn’t matters with this …

String class function Read More »

StringBuffer class in java

StringBuffer class in java StringBuffer class is used to create a mutable string object. It represents growable and writable character sequence. As we know that String objects are immutable, so if we do a lot of changes with String objects, we will end up with a lot of memory leak. So StringBuffer class is used …

StringBuffer class in java Read More »

StringBuilder class

StringBuilder class StringBuilder is identical to StringBuffer except for one important difference it is not synchronized, which means it is not thread safe. Its because StringBuilder methods are not synchronised. StringBuilder Constructors StringBuilder ( ), creates an empty StringBuilder and reserves room for 16 characters. StringBuilder ( int size ), create an empty string and …

StringBuilder class Read More »

Exception Handling In Java

Exception Handling is the mechanism to handle runtime malfunctions. We need to handle such exceptions to prevent abrupt termination of program. The term exception means exceptional condition, it is a problem that may arise during the execution of program. A bunch of things can lead to exceptions, including programmer error, hardware failures, files that need …

Exception Handling In Java Read More »

Exception Handling Mechanism

n java, exception handling is done using five keywords, try catch throw throws finally Exception handling is done by transferring the execution of a program to an appropriate exception handler when exception occurs. Using try and catch Try is used to guard a block of code in which exception may occur. This block of code …

Exception Handling Mechanism Read More »

Try with Resource Statement

JDK 7 introduces a new version of try statement known as try-with-resources statement. This feature add another way to exception handling with resources management,it is also referred to as automatic resource management. Syntax try(resource-specification) { //use the resource }catch() {…} This try statement contains a paranthesis in which one or more resources is declare. Any …

Try with Resource Statement Read More »

throw Keyword in java

the throw keyword is used to throw an exception explicitly. The only object of the Throwable class or its sub classes can be thrown. Program execution stops on encountering throw statement, and the closest catch statement is checked for a matching type of exception. Syntax : throw ThrowableInstance Creating Instance of Throwable class There are …

throw Keyword in java Read More »

User defined Exception subclass

You can also create your own exception sub class simply by extending java Exception class. You can define a constructor for your Exception sub class (not compulsory) and you can override the toString() function to display your customized message on catch. class MyException extends Exception { private int ex; MyException(int a) { ex=a; } public …

User defined Exception subclass Read More »

Method Overriding with Exception Handling

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions. Example of Subclass overriden Method declaring Checked Exception import java.io.*; class Super { void show() { System.out.println(“parent class”); …

Method Overriding with Exception Handling Read More »

Scroll to Top