Unit Ii-Apj
Unit Ii-Apj
OVERLOADING METHODS
To define two or more methods within the same class that share the same name, as long as their
parameter declarations are different. When this is the case, the methods are said to be
overloaded, and the process is referred to as method overloading.
Method overloading is one of the ways that Java supports polymorphism.
When an overloaded method is invoked, Java uses the type and/or number of arguments as its
guide to determine which version of the overloaded method to actually call.
Thus, overloaded methods must differ in the type and/or number of their parameters.
While overloaded methods may have different return types, the return type alone is insufficient to
distinguish two versions of a method.
When Java encounters a call to an overloaded method, it simply executes the version of the method
whose parameters match the arguments used in the call.
Example:
class OverloadExample {
void show(int a) {
System.out.println("Integer: " + a);
}
void show(double a) {
System.out.println("Double: " + a);
}
Integer: 10
Double: 10.5
3. Changing the Order of Parameters
Overloading can also be done by changing the order of different types of parameters.
Example:
class OverloadExample {
void print(String str, int num) {
System.out.println("String: " + str + ", Integer: " + num);
}
void print(int num, String str) {
System.out.println("Integer: " + num + ", String: " + str);
}
OBJECTS AS PARAMETER
In Java, objects can be passed as parameters to methods, just like primitive data types.
When an object is passed as a parameter, it is actually the reference to the object that is passed, not
the actual object itself.
This means that changes made to the object's fields inside the method will affect the original object
outside the method
Example: Passing an Object as a Parameter
This Java program demonstrates passing an object as a parameter to a method and
modifying its properties.
1. Class Definition (Person)
class Person {
String name;
Person(String name) {
this.name = name;
}
void changeName(String newName) {
this.name = newName;
}
}
The Person class has a field name (a String).
A constructor initializes the name when an object is created.
A method changeName(String newName) allows changing the name of the object.
2. Method to Modify Object (modifyPerson)
static void modifyPerson(Person p) {
p.name = "Updated Name"; // Modifies the original object
}
This method accepts a Person object (p) as a parameter.
It directly modifies the name field of the Person object.
RETURNING OBJECTS
A method can return an object instead of primitive data types like int, char, boolean, etc.
This allows us to return complex data structures, making programs more modular and efficient.
Why Return an Object
Returning objects is useful in many situations, such as:
1. Encapsulation of Data – Objects store multiple related data fields, making them ideal
return types.
2. Reusability – Methods can return objects that can be used elsewhere in the program.
3. Method Chaining – Returning this allows method calls to be chained together.
4. Factory Methods – Methods that create and return objects dynamically.
INHERITANCE IN JAVA
Inheritance in java is a mechanism in which one object acquires all the properties and
behaviors of parent object. Inheritance represents the IS-A relationship, also known as
parentchild relationship.
Why use inheritance in java
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
Syntax of Java Inheritance
1. class Subclass-name extends Superclass-name
2. {
3. //methods and fields
4. }
The extends keyword is used for class inheritance to indicate that a class is inheriting from a parent
(superclass). The basic syntax is:
Syntax:
class ParentClass {
// Parent class members (fields, methods, constructors)
}
class ChildClass extends ParentClass {
// Child class members (fields, methods, constructors)
}
TYPES OF INHERITANCE
Single Inheritance
In this type of inheritance, a subclass (child class) inherits from a single superclass (parent
class).
It enables code reuse from a single base class.
syntax
class Parent {
// Parent class members
}
interface Interface2 {
// Methods
}
interface Interface2 {
// Methods
}
class Parent {
// Parent class members
}
}}
Output:
weeping... barking... eating...
Hierarchical Inheritance Example
File: TestInheritance3.java
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){ Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing... eating...
The final keyword in Java is used to apply restrictions on variables, methods, and classes.
It prevents modification of values, method overriding, and inheritance
1. Final Variable (Constant)
A final variable can be assigned only once. Once assigned, its value cannot be changed.
class Example {
final int MAX_VALUE = 100;
class Parent {
final void show() {
System.out.println("Final method in Parent class");
}
}
Example program:
// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Output:
Animal makes a sound
Dog barks
Explanation:
The makeSound() method is defined in the Animal class.
The Dog class overrides makeSound() to provide its own implementation.
When we call makeSound() on an Animal object, it calls the Animal class method.
When we call makeSound() on a Dog object, it calls the overridden method in the Dog
class.
This is method overriding, where a subclass provides a specific implementation of a
method already defined in its superclass.
Example :
// Parent class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Output:
Dog barks
Cat meows
Explanation:
1. Upcasting: We use a parent class reference (Animal myAnimal) to refer to different
child class objects (Dog, Cat).
2. Method Overriding: Each subclass (Dog, Cat) provides its own implementation of the
makeSound() method.
3. Runtime Resolution: The method that gets executed is determined at runtime based on
the actual object type (Dog or Cat), not the reference type (Animal).
Example
abstract class
1. abstract class A{}
abstract method
1. abstract void printStatus();//no body and abstract
Example of abstract class that has abstract method
abstract class Bike
{
abstract void run();
}
class Honda4 extends Bike
{
void run()
{
System.out.println("running safely..");
}
public static void main(String args[])
{
Bike obj = new Honda4();
obj.run();
}
} output: running safely..
PACKAGES:
Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form,
built-in package and
user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Example:
An interface in Java is a blueprint for a class that contains only abstract methods (before Java 8) and
default/static methods (after Java 8).
interface B {
void methodB();
}
Method A implemented
Method B implemented