Found 9326 Articles for Object Oriented Programming

Java 8 Streams and its operations

Nishtha Thakur
Updated on 26-Jun-2020 15:41:18

830 Views

Streams are sequences of objects from a source, which support aggregate operations. These were introduced in Java 8.With Java 8, Collection interface has two methods to generate a Stream.stream() − Returns a sequential stream considering collection as its source.parallelStream() − Returns a parallel Stream considering collection as its source.Some operations on streams are −sorted − The sorted method is use to sort the stream lexicographically or in ascending orderList id = Arrays.asList("Objects", "Classes", "Interfaces"); List output = id.stream().sorted().collect(Collectors.toList());map − The map method maps the elements in the collection to other objects according to the Predicate passed as a parameterList list1= ... Read More

Can we override a private or static method in Java

Nancy Den
Updated on 27-Jun-2020 12:48:55

7K+ Views

No, we cannot override private or static methods in Java.Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.ExampleLet us see what happens when we try to override a private method − Live Democlass Parent {    private void display() {       System.out.println("Super class");        } } public class Example extends Parent {    void display() // trying to override display() {       System.out.println("Sub class");    }    public static void main(String[] args) {       Parent obj = new Example(); ... Read More

Declare static variables and methods in an abstract class in Java

Rishi Rathor
Updated on 27-Jun-2020 12:51:19

6K+ Views

If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.A static variable is a class variable. A single copy of the static variable is created for all instances of the class. It can be directly accessed in a static method.An abstract class in Java is a class that cannot be instantiated. It is mostly used as the base for subclasses to ... Read More

Restrictions applied to Java static methods

Nishtha Thakur
Updated on 26-Jun-2020 15:50:46

3K+ Views

If the static keyword is applied to any method, it becomes a static method.If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.There are a few restrictions imposed on a static methodThe static method cannot use non-static data member or invoke non-static method directly.The this and super cannot be used in static context.The static method can access only static type data ... Read More

Object Serialization with Inheritance in Java Programming

Nitya Raut
Updated on 26-Jun-2020 15:57:47

714 Views

Serialization is the process of changing the state of an object into the byte stream so that the byte stream can return back into a copy of the objectIn Java, an object is said to be serializable if its class or parent classes implement either the Serializable interface or the Externalizable interface.Deserialization is converting the serialized object back into a copy of the object.There are three cases of Object Serialization with inheritance.The child class is automatically serializable if the parent class is serializableA child class can still be serialized even if the parent class is not serializableIf we want the ... Read More

Java Program to list Weekday names

Samual Sam
Updated on 27-Jun-2020 14:46:33

2K+ Views

To list weekday names, use the getWeekdays () from the DateFormatSymbols class in Java.DateFormatSymbols is a class for encapsulating localizable date-time formatting data.Get weekday month names in an array −String[] days = new DateFormatSymbols().getWeekdays ();Display the weekdays −for (int i = 0; i < days.length; i++) {    String weekday = days[i];    System.out.println(weekday); }The following is an example −Example Live Demoimport java.text.DateFormatSymbols; public class Demo {    public static void main(String[] args) {       String[] days = new DateFormatSymbols().getWeekdays();       for (int i = 0; i < days.length; i++) {          String weekday ... Read More

Java program to reverse bits of a positive integer number

karthikeya Boyini
Updated on 27-Jun-2020 14:47:41

214 Views

The bits of an integer number can be reversed to obtain another number. An example of this is given as follows −Number = 11 Binary representation = 1011 Reversed binary representation = 1101 Reversed number = 13A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String[] args) {       int num = 14;       int n = num;       int rev = 0;       while (num > 0) {          rev = 1;       }       ... Read More

Java program to find the length of the Longest Consecutive 1’s in Binary Representation of a given integer

Samual Sam
Updated on 27-Jun-2020 14:48:28

263 Views

The length of the longest consecutive 1’s in the binary representation of a given integer involves the length of the longest series of 1’s that occur together. An example of this is given as follows −Number = 13 Binary representation = 1101The length of the longest consecutive 1’s in binary representation of 13 = 2A program that demonstrates this is given as follows −Example Live Demopublic class Example {    public static void main(String strings[]) {       int num = 55;       int n = num;       int count = 0;       while (num!=0) {          num = (num & (num

Display two-digit day of the month in Java

karthikeya Boyini
Updated on 27-Jun-2020 14:50:08

656 Views

Use the ‘d’ date conversion character to display two-digit day of the month, for example, 27, 28, 20, etc.System.out.printf("Two-digit day of the month: %td", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year ... Read More

Display two-digit month in Java

Samual Sam
Updated on 27-Jun-2020 14:50:32

2K+ Views

Use the ‘m’ date conversion character to display two-digit month.System.out.printf("Two-digit month: %tm", d);Above, d is a date object −Date d = new Date();The following is an example −Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");       String format = dateFormat.format(d);       System.out.println("Current date and time = " + format);       System.out.printf("Four-digit Year = %TY", d);       System.out.printf("Two-digit Year = %ty", d);   ... Read More

Advertisements