Found 9150 Articles for Object Oriented Programming

Can we define a parameterized constructor in an abstract class in Java?

Alshifa Hasnain
Updated on 09-Apr-2025 19:33:58

4K+ Views

A common question in Java OOPs is whether abstract classes can have parameterized constructors. Yes, we can define a parameterized constructor in an abstract class. What is a Parameterized Constructor in Java A parameterized constructor is a special type of class constructor that accepts parameters/arguments when creating an object. Unlike a default (no-arg) constructor, it allows you to initialize an object with specific values at the time of creation. Syntax The Following is the syntax: public class ClassName { private dataType field1; private dataType field2; // Parameterized ... Read More

How to format a date to String in Java?

Narasimha Murthi
Updated on 29-Jun-2020 14:41:19

2K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Formatting Date to StringYou can format a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To format a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Format/convert the ... Read More

How to find the age when date of birth is known? Using Java?

Narasimha Murthi
Updated on 29-Jun-2020 14:41:59

5K+ Views

Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.Finding the ageIf you already have the date of birth of a person, to find the age −Get the ... Read More

How to format a string to date in as dd-MM-yyyy using java?

Maruthi Krishna
Updated on 29-Jun-2020 14:42:48

17K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Parsing String to DateYou can parse a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To parse a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Parse/convert ... Read More

What is a remote interface in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:43:24

4K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ... Read More

Can we write an interface without any methods in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:44:24

2K+ Views

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method. Live Demoimport java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public ... Read More

Can we declare an interface with in another interface in java?

Maruthi Krishna
Updated on 22-Nov-2023 12:56:18

1K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface. To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class. Nested Interfaces Java allows declaring interfaces within another interface, these are known as nested interfaces. While implementing you need to refer to the nested interface as outerInterface.nestedInterface. Example In the following Java example, we have an interface with ... Read More

How to write/declare an interface inside a class in Java?

Maruthi Krishna
Updated on 29-Jun-2020 14:45:53

887 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.Nested interfacesJava allows writing/declaring interfaces within another interface or, within a class these are known as nested interfaces.ExampleIn the following Java example, we have a class with name Sample which contains a nested interface ... Read More

How to write a class inside an interface in Java?

Maruthi Krishna
Updated on 29-Jun-2020 14:34:08

354 Views

Defining a class within an interface is allowed in Java. If the methods of an interface accept a class as an argument and the class is not used elsewhere, in such cases we can define a class inside an interface.ExampleIn the following example we have an interface with name CarRentalServices and this interface has two methods that accepts an object of the class Car as an argument. Within this interface we have the class Car. Live Demointerface CarRentalServices {    void lendCar(Car c);    void collectCar(Car c);    public class Car{       int carId;       String carModel; ... Read More

Can we implement one interface from another in java?

Maruthi Krishna
Updated on 29-Jun-2020 14:35:20

423 Views

No, we cannot implement one interface from another you can just extend it using the extends keyword as −interface ArithmeticCalculations{    public abstract int addition(int a, int b);    public abstract int subtraction(int a, int b); } interface MathCalculations implements ArithmeticCalculations{    public abstract double squareRoot(int a);    public abstract double powerOf(int a, int b); }Still, if you try to implement one interface from another using the implements keyword. The compiler does not recognize the implements keyword after the name of the interface and throws a compile time error saying “'{' expected”.ExampleIn the following Java program, we have two interfaces ... Read More

Advertisements