Programming Articles - Page 2511 of 3363

Does java support hybrid inheritance?

Maruthi Krishna
Updated on 02-Jul-2020 10:01:48

3K+ 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

How to make an object completely encapsulated in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:02:51

377 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

Is it mandatory to override the default methods of an interface in Java?

Maruthi Krishna
Updated on 01-Aug-2019 11:12:37

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

What is diamond problem in case of multiple inheritance in java?

Maruthi Krishna
Updated on 02-Jul-2020 10:04:18

13K+ 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 is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Multiple inheritanceThere are various types of inheritance available namely single, multilevel, hierarchical, multiple ... Read More

Can Enum implements an interface in Java?

Vivek Verma
Updated on 29-May-2025 17:58:20

14K+ Views

Yes, an Enum implements an Interface in Java. It can be useful when we need to implement business logic that is tightly coupled with a specific property of a given object or class. Before implementing the interface with an enum, let's discuss enums and interfaces in Java with a code snippet for better understanding. Enum in Java In Java, an Enum (i.e., an enumeration) is a special data type added in Java version 1.5. Enums are constants by default; the names of an enum type's fields are in uppercase letters. In the Java programming language, you can define an Enum ... Read More

How can we implement a moving text using a JLabel in Java?

raja
Updated on 10-Feb-2020 13:34:34

2K+ Views

A JLabel is a subclass of JComponent class and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface. We can also implement a moving text in a JLabel by using a Timer class, it can set a timer with speed(in milliseconds) and this as an argument.Exampleimport java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.Timer; public class MovingTextLabel extends JFrame implements ActionListener {    private JLabel label;    public MovingTextLabel() {       setTitle("MovingTextLabel");       label= new JLabel(" ... Read More

Binary array after M range toggle operations?

Arnab Chakraborty
Updated on 01-Aug-2019 08:31:49

252 Views

Here we will see one problem. We have one binary array. It has n elements. Each element will be either 0 or 1. Initially, all elements are 0. Now we will provide M commands. Each command will contain start and end indices. So command(a, b) is indicating that the command will be applied from element at position a to element at position b. The command will toggle the values. So it will toggle from ath index to bth index. This problem is simple. check the algorithm to get the concept.AlgorithmtoggleCommand(arr, a, b)Begin    for each element e from index a ... Read More

Biggest Square that can be inscribed within an Equilateral triangle?

Arnab Chakraborty
Updated on 01-Aug-2019 08:29:04

287 Views

Here we will see the area of the biggest square that can be inscribed in an equilateral triangle. The side of the triangle is ‘a’ and the side of the square is x.The side of the triangle ‘a’ is −So x is −Example#include #include using namespace std; float areaSquare(float a) { //a is side of triangle    if (a < 0 ) //if a is negative, then this is invalid       return -1;    float area = a / (1 + 2/sqrt(3));    return area; } int main() {    float a = 7;    cout

Biggest Reuleaux Triangle within A Square?

Arnab Chakraborty
Updated on 01-Aug-2019 08:26:17

132 Views

Here we will see the area of biggest Reuleaux triangle inscribed within a square. The side of the square is ‘a’. And the height of the Reuleaux triangle is h.The height of the Reuleaux triangle is same as a. So a = h. So the area of Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float a) { //side of square is a    if (a < 0) //if a is negative it is invalid       return -1;    float area = ((3.1415 - sqrt(3)) * (a) * (a))/2;    return area; } int main() {    float side = 8;    cout

Biggest Reuleaux Triangle within a Square which is inscribed within a Circle?

Arnab Chakraborty
Updated on 01-Aug-2019 08:18:26

139 Views

Here we will see the area of biggest Reuleaux triangle inscribed within a square, That square is inscribed inside one circle. The side of the square is ‘a’. The radius of the circle is ‘r’. As we know that the diagonal of the square is the diameter of the circle. So −2𝑟 = 𝑎√2 𝑎 = 𝑟√2And the height of the Reuleaux triangle is h.The height of the Reuleaux triangle is same as a. So a = h. So the area of Reuleaux triangle is −Example#include #include using namespace std; float areaReuleaux(float r) { //radius of ciecle is ... Read More

Advertisements