Class & Objects
Class & Objects
We have already created the class named Main, so now we can use this to create objects.
To create an object of Main, specify the class name, followed by the object name, and use the
keyword new:
Example
Create an object called "myObj" and print the value of x:
Main.java Second.java
• These standard libraries come along with the Java Class Library (JCL) in a Java archive
(*.jar) file with JVM and JRE.
• For example,
.
•print() is a method of java.io.PrintSteam
•The print("...") method prints the string inside quotation marks
Output:
Square root of 4 is: 2.0
.
Java Method Overloading
• In Java, two or more methods may have the same name if they differ in parameters (different
number of parameters, different types of parameters, or both).
• These methods are called overloaded methods and this feature is called method overloading.
• For example:
• Here, the func() method is overloaded. These methods have the same name but accept different
arguments.
Private: These methods are only accessible within the class where they are defined and cannot be
2. Method Overloading by changing the data type of parameters
Java Constructors
• A constructor in Java is similar to a method that is invoked when an object of
the class is created.
• Unlike Java methods, a constructor has the same name as that of the class and
does not have any return type. For example,
• Here, Test() is a constructor. It has the same name as that of the class and doesn't
have a return type
Example: Java Constructor
class Main {
public String name; OUTPUT:
Constructor Called:
// constructor The name is Programiz
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}
int i;
class Company {
String name;
// public constructor
public Company() {
name = "Programiz";
}
}
class Main {
public static void main(String[] args) {
String languages;
// constructor accepting single value
Main(String lang) {
languages = lang;
System.out.println(languages + " Programming Language");
}
class Main {
int a;
boolean b;
System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Constructors Overloading in Java
• Similar to Java method overloading, we can also create two or more constructors with different
parameters. This is called constructor overloading.
public void getName() {
class Main { System.out.println("Programming Language: " + this.language);
String language; }
• In the above example, we have declared 2 methods: method1() and method2(). Here,
•method1 is public - This means it can be accessed by other classes.
•method2 is private - This means it can not be accessed by other classes.
Note: the keyword public and private. These are access modifiers in Java. They are also
known as visibility modifiers.
Types of Access Modifier
• There are four access modifiers keywords in Java and they are:
Modifier Description
declarations are visible only within the package
Default (package private)
package defaultPackage;
class Logger {
void message(){
System.out.println("This is a message");
}
}
• Here, the Logger class has the default access modifier. And the class is visible to all the
classes that belong to the defaultPackage package.
• However, if we try to use the Logger class in another class outside of defaultPackage,
we will get a compilation error.
Public Access Modifier
• When methods, variables, classes, and so on are declared public, then we can access them from
anywhere.
• The public access modifier has no scope restriction