0% found this document useful (0 votes)
4 views9 pages

Java Method Types and Constructors Explained

The document provides examples of various Java programming concepts, including static methods, instance methods, abstract classes, method overriding, method overloading, constructors, instance and static variables, and encapsulation. Each concept is illustrated with code snippets demonstrating how they are implemented in Java. The examples cover both calling methods with and without objects, as well as the use of access modifiers and constructors.

Uploaded by

Shivam Singhal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Java Method Types and Constructors Explained

The document provides examples of various Java programming concepts, including static methods, instance methods, abstract classes, method overriding, method overloading, constructors, instance and static variables, and encapsulation. Each concept is illustrated with code snippets demonstrating how they are implemented in Java. The examples cover both calling methods with and without objects, as well as the use of access modifiers and constructors.

Uploaded by

Shivam Singhal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Static method can be called without object and with object.

[Link] method calling using object

//static method

public class Main {

static void myMethod() {

[Link]("Hello World!");

public static void main(String[] args) {

Main abc=new Main();

[Link]();

[Link] method calling without object

//static method

public class Main {

static void myMethod() {

[Link]("Hello World!");

public static void main(String[] args) {

myMethod();

}
[Link] method

//instance method

public class Main {

void myMethod() {

[Link]("Hello World!");

public static void main(String[] args) {

Main c1=new Main();

[Link]();

4. abstract class and method

abstract class Student{

abstract void myMethod();

class Boy extends Student{

void myMethod() {

[Link]("Hello World!");

}
public class Main {

public static void main(String[] args) {

Boy c1=new Boy();

[Link]();

5. method overriding

class Student{

void myMethod() {

[Link]("base class");

class Boy extends Student{

void myMethod() {

[Link]("derived");

public class Main {

public static void main(String[] args) {

Boy c1=new Boy();

[Link]();

6. method overriding with super keyword

class Student{
void myMethod() {

[Link]("base class");

class Boy extends Student{

void myMethod() {

[Link]();

[Link]("derived");

public class Main {

public static void main(String[] args) {

Boy c1=new Boy();

[Link]();

7. method overloading in 2 class

class Student{

void myMethod() {

[Link]("base class");

class Boy extends Student{

void myMethod(int x) {
[Link](x);

public class Main {

public static void main(String[] args) {

Boy c1=new Boy();

[Link]();

[Link](40);

[Link] overloading in single class

class Student{

void myMethod() {

[Link]("method overloading");

void myMethod(int x) {

[Link](x);

public class Main {

public static void main(String[] args) {

Student c1=new Student();

[Link]();

[Link](40);
}

9. method overloading in main class

public class Main {

void myMethod() {

[Link]("method overloading");

void myMethod(int x) {

[Link](x);

public static void main(String[] args) {

Main c1=new Main();

[Link]();

[Link](40);

[Link] constructor

public class Main {

Main() {

[Link]("default constructor");

// void myMethod(int x) {

// [Link](x);
//}

public static void main(String[] args) {

Main c1=new Main();

[Link] constructor

public class Main {

Main(int x) {

//parameterized constructor

[Link](x);

// void myMethod(int x) {

// [Link](x);

//}

public static void main(String[] args) {

Main c1=new Main(34);

12. instance variable

public class Main {

int x=20;//instance variable

public static void main(String[] args) {


Main ob1=new Main();

[Link](ob1.x);

[Link] variable with object

public class Main {

static int x=20;//static variable

public static void main(String[] args) {

Main ob1=new Main();

[Link](ob1.x);

14. static variable without object

public class Main {

static int x=20;//static variable

public static void main(String[] args) {

//Main ob1=new Main();

[Link](x);

15. encapsulation

public class Person {


private String name;

// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
[Link] = newName;
}
}
public class Main {
public static void main(String[] args) {
Person myObj = new Person();
[Link]("John");
[Link]([Link]());
}
}

Common questions

Powered by AI

A default constructor automatically initializes an object with default states when no arguments are provided, whereas a parameterized constructor allows specific fields to be set using passed parameters. As illustrated, the default constructor in 'Main' simply prints a message, while the parameterized constructor in 'Main(int x)' uses a parameter to initialize and print its value, providing more control over object initialization .

Encapsulation is the principle of restricting access to certain components of an object and is implemented by using private fields and public getter and setter methods. In the 'Person' class, the name of the person is kept private to prevent direct access from outside the class, while public methods 'getName' and 'setName' are used to access and modify the name indirectly, ensuring control over how the name is manipulated .

No, a static method in Java cannot access instance variables directly because static methods belong to the class rather than any object instance. Therefore, they do not have access to 'this', which refers to the current object, making it impossible to directly reference instance variables without an object context .

In method overriding with the 'super' keyword, the derived class method can call the base class method of the same name. This allows extending or modifying the functionality of the base class method. For example, in the given sources, the 'Boy' class overrides the 'myMethod' from the 'Student' class. Using 'super.myMethod()', the base class method is called first, followed by executing the overridden method in the 'Boy' class, which prints 'base class' and then 'derived' .

Method overloading occurs when multiple methods in the same class have the same name but different parameter lists (i.e., different type, number, or both). The provided examples demonstrate method overloading by defining methods with the same name 'myMethod' but with different parameters, such as one method with no parameters and another with an integer parameter in both single and multi-class settings .

Using public access modifiers in getter and setter methods ensures that fields in a class can be safely accessed and modified while maintaining controlled access to the private variables. This setup in the encapsulation example allows external classes to interact with the 'name' field of the 'Person' object via 'setName' and 'getName', while the direct modification of 'name' is not possible, enhancing encapsulation and protecting the integrity of the data .

A static method in Java can be called using the class name or by an object of the class, while an instance method requires an object of the class for it to be called. Static methods do not depend on object state and can be invoked without creating an object. In contrast, instance methods operate on objects and can access instance variables .

Method overriding provides polymorphism by allowing a subclass to define a specific behavior for a method already defined in its superclass. For example, in the source, the 'Boy' class overrides the 'myMethod' from the 'Student' base class. When 'myMethod' is called on a 'Boy' instance, the overridden version is executed, allowing different behaviors depending on the object type, even if treated as a superclass type .

Abstract classes in Java are used to provide a base class with abstract methods that concrete classes must implement. For instance, 'Student' is an abstract class with 'myMethod' as an abstract method, which the concrete class 'Boy' implements. This enforces that any concrete subclass must provide its own specific implementation of 'myMethod', differentiating it from a regular class, which can be instantiated directly and may contain complete methods .

Static variables are used to share common data among all instances of a class because they belong to the class, not the objects. When accessed using an object, the static variable is retrieved through a class instance which is unconventional; directly accessing it via the class is more typical and reflects that it is shared by all instances .

You might also like