0% found this document useful (0 votes)
5 views

JAVAia

The document outlines the differences between method overloading and method overriding in Java, highlighting aspects such as definition, polymorphism, inheritance, parameters, return types, access modifiers, static methods, and final methods. It also explains Java's access modifiers (public, protected, default, and private) with examples, detailing their visibility and accessibility. Additionally, it introduces inter-thread communication in Java, emphasizing its importance for synchronized threads to collaborate using methods like wait(), notify(), and notifyAll().

Uploaded by

drivemaheshs
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)
5 views

JAVAia

The document outlines the differences between method overloading and method overriding in Java, highlighting aspects such as definition, polymorphism, inheritance, parameters, return types, access modifiers, static methods, and final methods. It also explains Java's access modifiers (public, protected, default, and private) with examples, detailing their visibility and accessibility. Additionally, it introduces inter-thread communication in Java, emphasizing its importance for synchronized threads to collaborate using methods like wait(), notify(), and notifyAll().

Uploaded by

drivemaheshs
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
You are on page 1/ 6

Differences Between Method Overloading and Method Overriding

Aspect Method Overloading Method Overriding


Same method name with Same method signature (name and
Definition different parameter lists in the parameters) in parent and child
same class. classes.
Polymorphism
Compile-time polymorphism. Runtime polymorphism.
Type
Requires inheritance; occurs
Not required; happens within
Inheritance between a superclass and a
the same class.
subclass.
Methods must have different The method must have the same
Parameters numbers or types of parameter list in both superclass
parameters. and subclass.
Can have the same or different Must have the same return type or a
Return Type
return types. covariant type.
No restriction on access Cannot reduce the visibility (access
Access Modifier
modifiers. level) of the overridden method.
Static methods cannot be
Static Methods Can overload static methods.
overridden; they are hidden instead.
Final methods can be Final methods cannot be
Final Methods
overloaded. overridden.
In Java, access modifiers control the visibility of classes, methods, and variables. When
working with packages, the following levels of access are used:
Implementation of Access Protection with Examples
1. Public Modifier
A member with public access is visible everywhere in all packages.
// File: pack1/PublicExample.java
package pack1;

public class PublicExample {


public void showMessage() {
System.out.println("This is a public method.");
}
}

// File: pack2/TestPublic.java
package pack2;

import pack1.PublicExample;

public class TestPublic {


public static void main(String[] args) {
PublicExample obj = new PublicExample();
obj.showMessage(); // Accessible because it's public
}
}
2. Protected Modifier
A protected member is accessible within the same package and by subclasses in other
packages.
// File: pack1/ProtectedExample.java
package pack1;

public class ProtectedExample {


protected void display() {
System.out.println("This is a protected method.");
}
}

// File: pack2/TestProtected.java
package pack2;

import pack1.ProtectedExample;

public class TestProtected extends ProtectedExample {


public static void main(String[] args) {
TestProtected obj = new TestProtected();
obj.display(); // Accessible because it's inherited
}
}
3. Default (Package-Private) Modifier
A member with default access is accessible only within the same package.
// File: pack1/DefaultExample.java
package pack1;

class DefaultExample {
void showMessage() {
System.out.println("This is a default method.");
}
}

// File: pack1/TestDefault.java
package pack1;

public class TestDefault {


public static void main(String[] args) {
DefaultExample obj = new DefaultExample();
obj.showMessage(); // Accessible because it's in the same package
}
}
4. Private Modifier
A private member is accessible only within its own class.
// File: pack1/PrivateExample.java
package pack1;

public class PrivateExample {


private void display() {
System.out.println("This is a private method.");
}

public void accessPrivate() {


display(); // Can be accessed within the same class
}
}

// File: pack1/TestPrivate.java
package pack1;

public class TestPrivate {


public static void main(String[] args) {
PrivateExample obj = new PrivateExample();
obj.accessPrivate(); // Indirectly accessing private method
}
}

Key Points
Public: Accessible everywhere.
Protected: Accessible in the same package and through inheritance in other packages.
Default: Accessible only in the same package.
Private: Accessible only within the defining class.
These levels help in securing and encapsulating data efficiently in Java applications.
Inter-Thread Communication in Java
Inter-thread communication is a mechanism that allows synchronized threads to
communicate with each other. This is particularly useful when multiple threads need to
collaborate to complete a task. Java provides methods like wait(), notify(), and notifyAll()
in the Object class to facilitate this communication

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy