
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

8K+ Views
A static block is a block of code with a static keyword. In general, these are used to initialize the static members of a class. JVM executes static blocks before the main method at the time loading a class.Examplepublic class MyClass { static{ System.out.println("Hello this is a static block"); } public static void main(String args[]){ System.out.println("This is main method"); } }OutputHello this is a static block This is main methodA constructor is similar to method and it is invoked at the time creating an object of the class, it is ... Read More

6K+ Views
Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.ExampleIn the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).Now, you create an object of the subclass and call the method demo().class SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public static void main(String args[]) { SubClass obj = new ... Read More

346 Views
Inheritance can be defined as the process where one (parent/super) class acquires the members (methods and fields) of another (child/sub).If two classes are related to each other with inheritance. If the super class and sub class contains same methods (same name and arguments), When we invoke this it using the sub class object, the method of the sub class will be executed. This mechanism is known as overriding.Overriding final methodsOnce you declare a method final it cannot be overridden If you try to do so, it generates a compile time error −Exampleclass Super{ public final void demo() { ... Read More

687 Views
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation (body) to it. If a class contains at least one abstract method, you must declare it abstract.In other words, if you extend an abstract class it is mandatory to implement (override) all the abstract methods in it or, declare it abstract else a compile time error will be generated for each abstract method (that you ... Read More

3K+ Views
Yes, the super class reference variable can hold the sub class object actually, it is widening in case of objects (Conversion of lower datatype to a higher datatype).But, using this reference you can access the members of super class only, if you try to access the sub class members a compile time error will be generated.ExampleIn the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.The Student extends the person class and in addition to the inherited ... Read More

1K+ Views
When write a Java program/class first of all you need to compile it using the javac command as −javac [name of the class].javaIf your program gets compiled without errors, a .class file (byte code) is created with the specified name. Then you need to execute it using the java command (JVM) as −java [class name]ExampleSuppose we Have created a simple class Calculator which adds two or, three numbers in the file with name Calculator.java in the path D:\sample as −public class Calculator { int addition(int a , int b){ int result = a+b; ... Read More

9K+ Views
An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces.Default Methods - Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{ public static int num = 100; public default void ... Read More

2K+ Views
Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{ }The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members are created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Exampleclass Super{ int a =100; int b = 200; public void ... Read More

354 Views
The process of wrapping up the data and, the code acting on the data together is known as encapsulation. This is a protective mechanism where we hide the data of one class from (an object of) another.Since, variables hold the data of a class to encapsulate a class you need to declare the required variables (that you want to hide) private and provide public methods to access (read/write) them.By doing so, you can access the variables only in the current class, they will be hidden from other classes, and can be accessed only through the provided methods. Therefore, it is ... Read More

3K+ Views
The default methods are introduced in an interface since Java8. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface"); } } public class InterfaceExample implements MyInterface{ public static void main(String ... Read More