Assignment 4 Oop
Assignment 4 Oop
Characteristics of a Constructor
1. Same Name as Class: The name of the constructor must be exactly the same as the
class name.
2. No Return Type: Constructors do not have any return type—not even void.
3. Automatic Invocation: A constructor is automatically called when you create an
object using new.
4. Can Be Overloaded: A class can have multiple constructors with different parameter
lists (constructor overloading).
5. Cannot Be Inherited: Constructors are not inherited by subclasses, although a
subclass can call a superclass constructor explicitly using super().
Example
public class Car {
private String model;
private int year;
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
// Usage in main:
public class Main {
public static void main(String[] args) {
Car car1 = new Car(); // calls default constructor
Car car2 = new Car("Toyota", 2021); // calls parameterized
constructor
car1.displayInfo();
car2.displayInfo();
}
}
2. Give an example of constructor overloading.
public class Person {
private String name;
private int age;
// 1) Default constructor
public Person() {
this.name = "No name provided";
this.age = 0;
}
1. final Variable
o A final variable cannot be reassigned once it has been assigned a value.
o This is useful for making constants.
o For example:
o final double PI = 3.14159;
2. final Method
o A final method cannot be overridden by subclasses.
o Useful if you want to maintain the same method implementation across all
subclasses.
3. final Class
o A final class cannot be extended (inherited).
o For example, public final class String { ... } in the Java standard
library cannot be subclassed.
1. static Variable
o A static variable (also known as a class variable) is shared among all instances
of the class.
o For example, a static counter field could keep track of the number of instances
created:
o public class Employee {
o private static int count = 0;
o private String name;
o
o public Employee(String name) {
o this.name = name;
o count++;
o }
o }
2. static Method
o A static method can be called without creating an instance of the class.
o For example, Math.random() is a static method in Java’s Math class.
3. static Block
o A static block is used to initialize static variables when the class is loaded into
memory.
o It is executed only once (the first time the class is loaded).
4. static Nested Class
o A static class defined inside another class. It does not require an instance of
the outer class to be instantiated.
1. public
o Accessible from everywhere (inside the class, within the same package,
outside the package, and by subclasses).
2. private
o Accessible only within the same class.
o Not accessible from outside the class or by subclasses.
3. protected
o Accessible within the same package.
o Also accessible in subclasses, even if they are in different packages.
4. default (also known as package-private, when no modifier is specified)
o Accessible within the same package only.
o Not accessible outside the package if there is no inheritance relationship.
6. Design a java class Rectangle which contains following
field and methods:
(i) Field: length, width: int
(ii) Default Constructor: initialize all fields with 0 value
(iii) Method: int getArea() will return area of rectangle.
Implementation
public class Rectangle {
private double length;
private double width;
// Default constructor
public Rectangle() {
this.length = 0.0;
this.width = 0.0;
}
// Default constructor
public Calculator() {
this.num1 = 0.0;
this.num2 = 0.0;
}
// Parameterized constructor
public Calculator(double num1, double num2) {
this.num1 = num1;
this.num2 = num2;
}
Example
public class Point {
private int x;
private int y;