JavaRush /Java Blog /Random EN /Coffee break #107. Constructor Method in Java I Construct...

Coffee break #107. Constructor Method in Java I Constructor Overload. Abstract Java Classes and Interfaces

Published in the Random EN group

Constructor Method in Java I Constructor Overload

Source: Objectorientedprogramming77 What is Constructor ? This is a special type of method whose name is the same as the name of the class, which determines how the object is initialized when it is created. Coffee break #107.  Constructor Method in Java I Constructor Overload.  Abstract Java Classes and Interfaces - 1Like other methods, we can also define a Constructor method in our Java program, but unlike other methods, we cannot call the Constructor directly; Java calls the constructor automatically when an object is created. When we use the new keyword to create an object of a class, Java does three things:
  • Allocates memory for an object.
  • Initialize this object instance variable with their initial or default value.
  • Calls the Method constructor of the class.
If a class does not define any Constructor method , we will still create an object of that class, but we must set an instance variable or call other methods, which must subsequently initialize the object with that object. By defining a Constructor method in our own classes, we can set the initial values ​​of an instance variable, call a method based on that variable, or call methods on other objects, or calculate the initial properties of our object. We can also overload Constructor like normal methods to create an object that has certain properties based on the argument we pass to new .

Basic Constructor

By definition, Constructor looks like a regular method with two main differences.
  • Constructor and class name are always the same.
  • It has no return type
For example, the table below shows a simple Person class with a constructor that initializes its instance variable based on the new argument . The class also includes a method by which an object can introduce itself, and a main() method for testing each of these classes.
class Person
{
    String name;
    int age;

    Person (String n, int a)
{
    name = n;
    age = a;
}

void printPerson ()
{
System.out.print("Hi, I am " +name);
System.out.println(" I am "+ age + " years old.");
}

public static void main(String args[])
{

    Person p;

    p = new Person ("Ajab", 20);
    p.printPerson();

    p = new Person ("Rizwan", 30);
    p.printPerson();
We get the output:
Hi, I am Ajab. I am 20 years old. Hi, I am Rizwan. I am 30 years old

Constructor overload

Like other methods, Constructor can also accept different numbers and types of parameters, allowing objects to be created with specified properties, or allowing Constructor to calculate properties based on different types of input. For example, the MyRectone class in a given table creates a MyRectone constructor and passes a different parameter instead of creating different methods for the given arguments.
class MyRectone
 {

    int x1 = 0;
    int y1 = 0;
    int x2 = 0;
    int y2 = 0;

MyRectone ( int x1, int x2, int x2, int y2)
 {

    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;

}

MyRectone (Point topLeft, Point bottomRight)
 {

    x1 = topLeft.x;
    y1 = topLeft.y;
    x2 = bottomRight.x;
    y2 = bottomRight.y;

}

MyRectone ( Point topLeft, int w, int h)
{

    x1 = topLeft.x;
    y1 = top left.y;
    x2 = ( x1 + w);
    y2 = (y1 + h);

}

void printRect ()
{

    System.out.print ("MyRectone: <" + x1 + ", " + y1);
    system.out.println (", " + x2 + " ,"+ y2 + ">");

}

public static void main (String args [] )
{

    MyRectone rect;
    System.out.println ("Calling MyRectone with coordinates 35,35 70,70");

    rect = new MyRectone (35,35,70,70);
    rect.printRect();

    System.out.println ("Calling MyRectone with coordinates (15,15) (30,30)");
    rect = new MyRectone (15,15,30,30);
    rect.printRect();

    System.out.print (" Calling buildRect w/1 point (10,10),");
    System.out.println ("width (50) and height (50)");
    rect = new MyRectone ( new Point (10,10), 50, 50);
    rect.printRect();
Conclusion:
Calling MyRectone with coordinates 35,35 70,70: MyRectone: <35,35,70,70> Calling buildRect w/1 points (15,15), (30,30): MyRectone: <15,15,30,30 > Calling buildRect w/1 point (10,10), width (50) and height (50): MyRectone:<10,10,50,50>

Calling another Constructor

A constructor can be a superset of another constructor defined in your class; that is, they can behave the same plus a little more. Rather than duplicating identical behavior across multiple constructor methods in our class, it makes sense to be able to simply call that first constructor from the body of the second constructor. Java provides a special syntax for this. To call a constructor defined in the current class, use this form:
this (arg1, arg2, arg3… ..);
The arguments to this are, of course, the arguments to the constructor.

Abstract Java Classes and Interfaces

Source: Dev.to An abstract class is a class that has some methods without a full definition and has the abstract modifier . Coffee break #107.  Constructor Method in Java I Constructor Overload.  Abstract Java Classes and Interfaces - 2
  • You cannot create an object using an abstract class's constructor.
  • You can use an abstract class as a base class to define a derived class.
  • An abstract class has at least one abstract method.
  • Abstract method : has a header like a regular method, but without a body, and requires the abstract modifier and a semicolon.
  • An abstract method cannot be private.
  • An abstract class can be a type.
Example:
public abstract class Example{
  private String data1;
  private double data2;

public abstract double getPay();
}
Interface : Defines a set of methods that any class that implements this interface must have.
  • An interface is a type.
  • It contains method headers with no definition and no instance variables:
public interface Interface1{
public void method1();
public int method2();
}
To implement an interface, a class must do two things:
  • Include implements InterfaceName .
  • The class must implement all method headers listed in the interface.
public class Implementer implements Interface1 {

    @Override
    public void method1() {
    //definition
    }

    @Override
    public int method2() {
    //definition
    }

}
  • Method headers are declared public .
  • An abstract class can also implement an interface, this class provides definitions for some of the method headers in the interface.
  • Java interfaces can also contain constant , for example:
public interface Constant {

    public static final int JANUARY = 1, FEBRUARY = 2, MARCH = 3;
}
  • Any class that implements the Constant interface will automatically have these constants, for example:
public class Constants implements Constant {

    public static void main(String[] args) {
        System.out.println(JANUARY);
    }

}
  • You can mix the use of interfaces by including constants and method headers into a single interface.
  • Java does not support multiple inheritance, so a class can only extend one base class. However, using interfaces, a class can implement multiple interfaces:
public class Implementer implements Interface1, Interface2, .. InterfaceN{

}
  • The reason why a Java class can only extend one base class is because if Java allows two base classes, the two classes may have the same method header with a different definition, resulting in inconsistency.
  • Two interfaces can be incompatible if you define two constants with the same name and different values:
public interface Interface1{
public static final int ANSWEAR = 0;
}
public interface Interface1{
public static final int ANSWEAR = 3;
}
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION