CS3391 OOPS Unit 2
CS3391 OOPS Unit 2
PROGRAMMING
UNIT 2
INHERITANCE, PACKAGES & INTERFACES
by
S.Sakkaravarthi, AP/IT
Ramco Institute of Technology, Rajapalayam
UNIT 2-TOPICS
Overloading Methods
Objects as Parameters
Returning Objects
Static, Nested and Inner Classes
Inheritance: Basics-Types of Inheritance
Super keyword
UNIT 2-TOPICS
When a class has more than one method having the same
name but with different parameter lists, this feature is
called method overloading in Java.
This means that any modifications made to the object within the
method will have an impact on the original object.
Object as Parameter
public class Student
{
private int rollno;
private String name;
public MyClass(int r, String n)
{
rollno = r;
name = n;
}
public void display(Student obj) // Method with object as parameter
{
System.out.println(“Roll no is:"+ obj.rollno);
System.out.println(“Name is:" + obj.name);
}
Object as Parameter-Contd…
class
class BOI
BOI extends
extends Bank{
Bank{ BOI
BOI INTEREST:4
INTEREST:4
int
int getRateOfInterest(){
getRateOfInterest(){
return
return 4;
4;
}}
}}
Method Overloading Vs Method Overriding
Method Overloading Method Overriding
Method overloading is performed within Method overriding occurs in two classes
class. that have IS-A relationship.
In Method Overloading, parameter must be In Method Overriding, parameter must
different. be same.
Return type can be same or different in Return type must be same in method
method overloading overriding
Method overloading is used to increase the Method overriding is used to provide the
readability of the program. specific implementation of the method that
is already provided by its superclass.
Method overloading is the example of Method overriding is the example of run
compile time polymorphism time polymorphism
Method Overloading Method Overriding
} {
System.out.println(“flush eating animal”);
}}
Dynamic Method Dispatch
Dynamic Method Dispatch is the process by which a call to
an overridden method is resolved at run time (during code
execution).
The concept of method overriding is the way to attain
runtime polymorphism in java.
During the code execution, JVM decides which
implementation of the same method should be called.
Dynamic Method Dispatch is another name for Runtime
Polymorphism.
Abstract Class
A class that is declared with the abstract keyword is known
as abstract class in java.
It can have abstract and non-abstract methods.
An abstract method is a method that is declared without any
implementation.
Abstract class needs to be extended and its method
implemented.
Abstract class cannot be instantiated (i.e., Object cannot be
created for an abstract class).
Abstract Class-Syntax
abstract class <class name>
{
//abstract method and non-abstract methods
}
Abstract Method: A method which is declared as abstract and
does not have implementation is known as an abstract method
Abstract Method Syntax:
import java.util.*;
Here, util is a sub-package created inside java package.
Packages and Member Access
Members private default protected public
of Java
class N Y N Y
variable Y Y Y Y
method Y Y Y Y
constructor Y Y Y Y
interface N Y N Y
Packages and Member Access
Access within within outside package outside
Modifier class package by subclass only package
private Y N N N
default Y Y N N
protected Y Y Y N
public Y Y Y Y
Private-Access Specifier
//save by B.java
//save by A.java
package mypack;
package pack;
import pack.*;
class A
class B
{
{
void msg()
public static void main(String args[])
{
{
System.out.println("Hello");
A obj=new A(); //Compile Time Error
}
obj.msg(); //Compile Time Error
}
}
}
Protected -Access Specifier
The protected access specifier is accessible within package and
outside the package but through inheritance only.
//save by B.java
//save by A.java
package mypack;
package pack;
import pack.*;
public class A
class B extends A
{
{
protected void msg()
public static void main(String args[])
{
{
System.out.println("Hello");
B obj=new B();
}
obj.msg(); Output: Hello
}
}
}
Public -Access Specifier
The public access specifier is accessible everywhere. It has
the widest scope among all other modifiers.
//save by B.java
//save by A.java
package mypack;
package pack;
import pack.*;
public class A
class B
{
{
public void msg()
public static void main(String args[])
{
{
System.out.println("Hello");
A obj=new A();
}
obj.msg(); Output: Hello
}
}
}
Advantages of Packages
1. import package.*
2. import package.classname
3. fully qualified name
Interfaces
An interface in java is a blueprint of a class. It has static
constants and abstract methods. The interface in java is a
mechanism to achieve abstraction.