Monthly Archives: June 2023

INTERFACES IN JAVA FIRST PART

INTERFACES IN JAVA INTRODUCTION We introduce interfaces in the context of OOP programming. At the beginning of the course, one of the aspects we focused on was the public interface. One of the basic principles by which we must design our classes is that of encapsulation. In particular, we must make attributes private, as well as methods used only by the class, and expose to the outside world the minimum functionality for those who will use our class. ABSTRACT CLASSES Often a set of responsibilities can be identified, defined in a general way, for example, with [...]

By |2023-06-02T20:15:44+00:00June 2, 2023|0 Comments

INTERFACES IN JAVA SECOND PART

INTERFACES IN JAVA MULTIPLE IMPLEMENTATION OF INTERFACES In this second part, which is a bit more substantial than usual, we will finish the interface talk by analyzing the missing aspects. Let us look at the advantages in using interfaces with a somewhat more articulated example. We introduce two interfaces. We declare the class AClass which implements both interfaces. Let's write a second class BClass which, however, implements only the second interface. Let us summarize with a figure what has been written so far. [...]

By |2023-06-03T21:29:28+00:00June 3, 2023|0 Comments

SEALED CLASS AND SEALED INTERFACE IN JAVA

SEALED CLASSES AND SEALED INTERFACES IN JAVA This brings us to the important question what is a sealed class? Let's take a step back. We are in the condition where a class can be completely inherited, or if declared as final it cannot be inherited at all. The sealed classes fall between these two extremes by posing alternative solutions to us. Let's look at the definition. With the sealed keyword the class becomes sealed however the permits keyword allows us to specify a list of classes that can inherit it. [...]

By |2023-06-05T19:47:58+00:00June 5, 2023|0 Comments

ENUMERATIONS IN JAVA

ENUMERATIONS IN JAVA INTRODUCTION TO ENUMERATIONS Let's start by looking at this example. An enumeration is declared and created through the keyword enum. Then we go on to enter the name we want to give our enum, as if it were a class. An enumeration in Java is an actual Data Type, if we want to be transparent, we have to say that he represents a somewhat special class. Within the body we define a set of constants. The elements of an enumeration are called enumerative constants, and in fact they are implicitly regarded as elements [...]

By |2023-06-07T20:15:28+00:00June 7, 2023|0 Comments

EXCEPTIONS IN JAVA FIRST PART

EXCEPTIONS IN JAVA INTRODUCTION An exception is an error condition that is not detected at runtime, that is, during program execution. Such an error prevents the program from progressing properly and requires handling. If the exception is not handled, the program stops. We introduce the base classes from which all exceptions are derived. In Java all exceptions are represented by classes. See the figure. Throwable which is derived from Object, means to lift, is the base class of all exceptions. The difference between the two subclasses Exception and Error is that Exception is the superclass of [...]

By |2023-06-10T22:39:04+00:00June 10, 2023|0 Comments

EXCEPTIONS IN JAVA SECOND PART

EXCEPTIONS IN JAVA USE THE KEYWORD THROWS If we do not handle a certain group of exceptions in the code, it may happen that the program ends with an error message. If a method has the potential to generate exceptions, those using that method need to know this so that they can take their own countermeasures. Let's look at how to declare a method with the throws keyword. exc-list are the exception classes that the method is unable or unwilling to handle. The classes excluded from this mechanism are those whose superclass is Error and RuntimeException. [...]

By |2023-06-12T21:50:31+00:00June 12, 2023|0 Comments

TYPE WRAPPERS AND AUTOBOXING

TYPE WRAPPERS We will see how Java's primitive types can be converted and considered objects. There are several reasons for having to make this conversion, and one of them is Generics. A Type Wrapper is a wrapper that we go and build around a primitive type. To be more precise, it is a class that encapsulates the primitive type. Among the most important classes we can point out numerical types. Classes are subclasses of Number. All numeric Wrapper classes have a special static method that takes the primitive type as a [...]

By |2023-06-14T18:20:52+00:00June 14, 2023|0 Comments

GENERICS IN JAVA FIRST PART

GENERICS IN JAVA INTRODUCTION TO GENERICS Generics are parametric classes, interfaces, and methods; said like that, it's not like it's immediately understandable. It will be in a while when we see the first generic class. Classes, interfaces, and methods operate on parametric data types, conventionally specified through a letter T, or others that act as placeholders for any Data Type of Reference Type. Through Generics we are able to design much more flexible programs, let's start right away with the code. A GENERIC CLASS PART 1 Observe this class. The private [...]

By |2023-06-16T14:39:57+00:00June 16, 2023|0 Comments

GENERICS IN JAVA SECOND PART

GENERICS IN JAVA BOUNDED TYPES To understand how to implement the Bounded Type we start right away with an example. The attribute t in the figure is of type Object, as we have seen in previous lessons to turn it into a Type Wrapper we need to use the method intValue(). If we try to compile this class we get an error. It is not recognized by the compiler because intValue() is a method of class Number and we are working with a type Object which is at a higher level, [...]

By |2023-06-17T19:12:55+00:00June 17, 2023|0 Comments

LAMBDA EXPRESSIONS IN JAVA

LAMBDA EXPRESSIONS IN JAVA INTRODUCTION A Lambda Expression we can define to a first approximation as an anonymous method. This immediately tells us two things. The first is that a lambda expression is a method; the second is that the method has no name. To be more precise, however, we must say that a lambda expression is an anonymous class that implements a particular type of interface called a Functional Interface. FUNCTIONAL INTERFACES They are the foundation on which Lambda Expression is based. A Functional Interface contains only one abstract method. Let's look at an example. [...]

By |2023-06-18T19:50:01+00:00June 18, 2023|0 Comments
Go to Top