0% found this document useful (0 votes)
3 views12 pages

Module 2

The document provides an overview of key concepts in Java programming, including the structure and significance of the main method, access modifiers, method overloading, and inheritance. It explains the roles of keywords like 'this', 'super', and the rules for constructor chaining and method overriding. Additionally, it highlights the importance of public and static methods in Java's object-oriented programming paradigm.

Uploaded by

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

Module 2

The document provides an overview of key concepts in Java programming, including the structure and significance of the main method, access modifiers, method overloading, and inheritance. It explains the roles of keywords like 'this', 'super', and the rules for constructor chaining and method overriding. Additionally, it highlights the importance of public and static methods in Java's object-oriented programming paradigm.

Uploaded by

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

PCCCS405 -Advanced Programming (OOP)

JAVA main
public static void main(String[] args)
public:
• Makes the method accessible from anywhere
• The JVM (Java Virtual Machine) must be able to call main()
• If it is not public, the program will not run
• Without public → JVM cannot access main()
Static
• Allows JVM to call main() without creating an object
• JVM does not instantiate the class before calling main()
void
• Indicates that main() returns nothing
• JVM does not expect any value back
main
• A special method name recognized by the JVM
• JVM always starts execution from main()
String[] args
• Stores command-line arguments
• args is just a variable name (you can rename it)
Public Vs. Static Class Methods
…..
public class Main { // The main() method
// Creating a static method public static void
static void fun1() { main(String[] args) {
[Link]("fun1: This // Accessing static method
is a static method.");
through the class
}
fun1();

// Creating an object of the


// Creating a public method
Main class
public void fun2() {
Main obj = new Main();
[Link]("fun2: This
is a public method."); // Accessing public method
} through the object
……. obj.fun2();
}
}
Java Method
public class test2 {
static void submethod() {
[Link]("Hello World");
}

public static void main(String[] args) {


submethod();
}
}
public class test {
static int myMethod(int x) {
return 5 + x;
}

public static void main(String[] args) {


[Link](myMethod(3));
}
}
this Keyword inside Java Methods
this is a keyword in Java which is used as a reference to the object of the
current class, with in an instance method or a constructor. Using this you can
refer the members of a class such as constructors, variables and methods.
Constructor Chaining In Java
The constructor chaining is a specific sequence
of calling constructors when a user initializes an
object in a certain way. This technique is used
when multiple constructors are invoked one
after another, based on the instance class. It is
also closely related to inheritance, where the
role of a subclass constructor is to call the
constructor of its superclass.
Constructor chaining can be done in two ways:

Within same class: It can be done using this() keyword for


constructors in the same class
From base class: by using super() keyword to call the
constructor from the base class.
Rules of constructor chaining
• There should be at-least be one constructor
without the this() keyword (constructor 3 in
above example).

• Constructor chaining can be achieved in any


order.
Java - Access Modifiers
• Default (No keyword required)
• Private
• Protected
• Public
Method Overloading
• int myMethod(int x)
• float myMethod(float x)
• double myMethod(double x, double y)

The primary rules for method overloading are:

• Same Method Name: All overloaded methods must have the same name within the
same class or hierarchy.
• Different Parameter List: The methods must have different parameter lists. The
difference can be achieved by:
– Changing the number of parameters (e.g., one method with two
integers, another with three integers).
– Changing the data types of parameters (e.g., one method with two
integers, another with two doubles).
– Changing the order of parameters (e.g., one method with (String name,
int id), another with (int id, String name)).

• Return Type Does Not Matter: You cannot overload a method by changing its return type
alone. The parameter list must still be different; otherwise, a compile-time error will
occur.
• Access Modifiers and Exceptions: Overloaded methods can have different access
modifiers (public, private, etc.) and can throw different exceptions.
• Static/Instance Methods: Both static and non-static methods can be overloaded.
Java - Inheritance
• Code Reusability: The basic need of an inheritance
is to reuse the features. If you have defined some
functionality once, by using the inheritance you can
easily use them in other classes and packages.
• Extensibility: The inheritance helps to extend the
functionalities of a class. If you have a base class
with some functionalities, you can extend them by
using the inheritance in the derived class.
• Implantation of Method Overriding: Inheritance is
required to achieve one of the concepts of
Polymorphism which is Method overriding.
• Achieving Abstraction: Another concept of OOPs
that is abstraction also needs inheritance.
Method Overriding
Rules for Method Overriding
• Name, parameters, and return type must
match the parent method.
• Java picks which method to run at run time,
based on the actual object type, not just the
reference variable type.
• Static methods cannot be overridden.

You might also like