Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 226 of 589
A static initialization block in Java
Instance variables are initialized using initialization blocks. However, the static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded. There can be multiple static initialization blocks in a class that is called in the order they appear in the program.A program that demonstrates a static initialization block in Java is given as follows:Examplepublic class Demo { static int[] numArray = new int[10]; static { System.out.println("Running static initialization block."); for (int i = 0; i < numArray.length; i++) { ...
Read MoreDemonstrate static variables, methods and blocks in Java
The static variable is a class level variable and it is common to all the class objects i.e. a single copy of the static variable is shared among all the class objects.A static method manipulates the static variables in a class. It belongs to the class instead of the class objects and can be invoked without using a class object.The static initialization blocks can only initialize the static instance variables. These blocks are only executed once when the class is loaded.A program that demonstrates this is given as follows:Examplepublic class Demo { static int x = 10; static ...
Read MoreReference static field after declaration in Java
The static field variable is a class level variable and it belongs to the class not the class object.So the static field variable is common to all the class objects i.e. a single copy of the static field variable is shared among all the class objects.A program that demonstrates referencing the static field variable after declaration is given as follows:Examplepublic class Demo { static int a = 7; static int b = a + 5; public static void main(String[] args) { System.out.println("a = " + a); System.out.println("b = " + b); ...
Read MoreHow to use the asList() method of the Arrays class to create a Vector object
A Vector can be created from an Array using the java.util.Arrays.asList() method.A program that demonstrates this is given as follows:Exampleimport java.util.Arrays; import java.util.Vector; public class Demo { public static void main(String args[]) { Integer[] arr = { 3, 1, 9, 6, 4, 8, 7 }; Vector vec = new Vector(Arrays.asList(arr)); System.out.println("The Vector elements are: " + vec); } }OutputThe Vector elements are: [3, 1, 9, 6, 4, 8, 7]Now let us understand the above program.The Integer Array arr[] is defined. Then a Vector is created using the Arrays.asList() ...
Read MoreDeriving a Class in Java
A class can be derived from the base class in Java by using the extends keyword. This keyword is basically used to indicate that a new class is derived from the base class using inheritance. It can also be said that it is used to extend the functionality of the class.A program that demonstrates this is given as follows:Exampleclass A { void funcA() { System.out.println("This is class A"); } } class B extends A { void funcB() { System.out.println("This is class B"); } } public class Demo { public ...
Read MoreCreating a Multilevel Inheritance Hierarchy in Java
Inheritance involves an object acquiring the properties and behaviour of another object. So basically, using inheritance can extend the functionality of the class by creating a new class that builds on the previous class by inheriting it.Multilevel inheritance is when a class inherits a class which inherits another class. An example of this is class C inherits class B and class B in turn inherits class A.A program that demonstrates a multilevel inheritance hierarchy in Java is given as follows:Exampleclass A { void funcA() { System.out.println("This is class A"); } } class B extends A ...
Read MoreVariable in subclass hides the variable in the super class in Java
Sometimes the variable in the subclass may hide the variable in the super class. In that case, the parent class variable can be referred to using the super keyword in Java.A program that demonstrates this is given as follows:Exampleclass A { char ch; } class B extends A { char ch; B(char val1, char val2) { super.ch = val1; ch = val2; } void display() { System.out.println("In Superclass, ch = " + super.ch); System.out.println("In subclass, ch = " + ch); } } ...
Read MoreUsing run-time polymorphism in Java
A single action can be performed in multiple ways using the concept of polymorphism. Run-time polymorphism can be performed by method overriding. The overridden method in this is resolved at compile time.A program that demonstrates run-time polymorphism in Java is given as follows:Exampleclass Animal { void sound() { System.out.println("Animal makes sound"); } } class Cat extends Animal { void sound() { System.out.println("Cat Meows"); } } class Dog extends Animal { void sound() { System.out.println("Dog Barks"); } } class Cow extends Animal { void sound() ...
Read MoreClass that contains a String instance variable and methods to set and get its value in Java
A class declaration can contain a String instance and methods to set and get its value in Java.A program that demonstrates this is given as follows:Exampleclass Name { private String name; public void setName(String n) { name = n; } public String getName() { return name; } } public class Demo { public static void main(String[] args) { Name n = new Name(); n.setName("John Smith"); System.out.println("The name is: " + n.getName()); } }OutputThe name is: John SmithNow let ...
Read MoreWhat is Default access level in Java?
The default access level is available when no access level is specified. All the classes, data members, methods etc. which have the default access level can only be accessed inside the same package.A program that demonstrates the default access level in Java is given as follows:Exampleclass Employee { int empno; String name; void insert(int e, String n) { empno = e; name = n; } void display() { System.out.println("Employee Number: " + empno); System.out.println("Name: " + name); } } public class Demo ...
Read More