Data Hiding
Data Hiding
Example:
Note:
Ex-:
class Test {
public void m1(double d)
{
}
public void m2(int i)
{
}
public static void main(String ar[]) {
Test t=new Test();
t.m1(10.5);
t.m2(10);
t.m3(10.5); //CE
}
}
Note: - Within the same class we can't take 2 methods with the same signature
otherwise we will get compile time error.
Example:
Note:-
Example 1: We can use same abs() method for int type, long type, float type etc.
Example:
1. abs(int)
2. abs(long)
3. abs(float)
Types of Polymorphism:-
1. Compile-time polymorphism
2. Runtime polymorphism.
3-Pillars of OOPS:-
1) Inheritance talks about reusability.
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.
Two methods are said to be overload if and only if both having the same
name but different argument types.
In 'C' language we can't take 2 methods with the same name and different
types.
Example:
Having the same name and different argument types is called method
overloading.
All these methods are considered as overloaded methods.
Having overloading concept in java reduces complexity of the programming.
If we have to perform only one operation, having same name of the methods
increases the readability of the program.
Example:
class Test {
public void m1() {
System.out.println("no-arg method");
}
public void m1(int i) {
System.out.println("int-arg method"); //overloaded methods
}
public void m1(double d) {
System.out.println("double-arg method");
}
public static void main(String[] args) {
Test t=new Test();
t.m1();//no-arg method
t.m1(10);//int-arg method
t.m1(10.5);//double-arg method
}
}
Conclusion: In overloading compiler is responsible to perform method resolution
(decision) based on the reference type (but not based on run time object). Hence
overloading is also considered as compile time polymorphism (or) static
polymorphism (or) early biding.
Note: - In Java, Method Overloading is not possible by changing the return type of
the method only.
We have created two methods, first add() method performs addition of two
numbers and second add method performs addition of three numbers.
Ex:-
We are creating static methods so that we don't need to create instance for calling
methods.
class Adder{
static int add(int a,int b){
return a+b;
}
static int add(int a,int b,int c){
return a+b+c;
}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
Output:
22
33
2) Method Overloading: changing data type of arguments
Ex:-
We have created two methods that differs in data type. The first add method
receives two integer arguments and second add method receives two double
arguments.
class Adder{
static int add(int a, int b){
return a+b;
}
static double add(double a, double b){
return a+b;
}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Output:
22
24.9
Q) Why Method Overloading is not possible by changing the return type of
method only?
In java, method overloading is not possible by changing the return type of the
method only because of ambiguity. Let's see how ambiguity may occur:
class Adder{
static int add(int a,int b){
return a+b;
}
static double add(int a,int b){
return a+b;
}
}
class TestOverloading3{
public static void main(String[] args){
System.out.println(Adder.add(11,11));//ambiguity
}}
Output:
Compile Time Error: method add(int,int) is already defined in class Adder
Yes, by method overloading. You can have any number of main methods in a class
by method overloading. But JVM calls main() method which receives string array
as arguments only.
class TestOverloading4{
}
Output:
byte can be promoted to short, int, long, float or double. The short datatype can be
promoted to int, long, float or double. The char datatype can be promoted to
int,long,float or double and so on.
class OverloadingCalculation1{
void sum(int a,long b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
If there are matching type arguments in the method, type promotion is not
performed.
class OverloadingCalculation2{
void sum(int a,int b){
System.out.println("int arg method invoked");
}
void sum(long a,long b){
System.out.println("long arg method invoked");
}
class OverloadingCalculation3{
obj.sum(20,20);//now ambiguity
Note: One type is not de-promoted implicitly for example double cannot be
depromoted to any type implicitly.
Ex:-
class Parent {
public void property(){
System.out.println("cash+land+gold");
}
public void marry() {
System.out.println("Laxmi"); //overridden method
}
}
class Child extends Parent{ //overriding
public void marry() {
System.out.println("Kruthi/keerthi/Kajal/Pooja"); //overriding method
}
}
class Test {
public static void main(String[] args) {
Parent p=new Parent();
p.marry();//laxmi(parent method)
Child c=new Child();
c.marry();//Kruthi/keerthi/Kajal/Pooja (child method)
Parent p1=new Child();
p1.marry();//Kruthi/keerthi/Kajal/Pooja (child method)
}
}
Note: In overriding method resolution is always takes care by JVM based on
runtime object hence overriding is also considered as runtime polymorphism or
dynamic polymorphism or late binding.
Note: In overriding runtime object will play the role and reference type is dummy.
1. The method must have the same name as in the parent class
2. The method must have the same parameter as in the parent class.
3. There must be an IS-A relationship (inheritance).
Ex:- Understanding the problem without method overriding
class Vehicle{
void run(){
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike extends Vehicle{
public static void main(String args[]){
//creating an instance of child class
Bike obj = new Bike();
//calling the method with child class instance
obj.run();
}
}
Output:
Vehicle is running
Problem is that I have to provide a specific implementation of run() method
in subclass that is why we use method overriding.
Example of with method overriding:-
We have defined the run method in the subclass as defined in the parent class but it
has some specific implementation. The name and parameter of the method are the
same, and there is IS-A relationship between the classes, so there is method
overriding.
Ex:-
class Vehicle{
//defining a method
void run(){
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){
System.out.println("Bike is running safely");
}
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
Output:
Bike is running safely
Ex:-
class Bank{
int getRateOfInterest(){
return 0;
}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){
return 8;
}
}
class ICICI extends Bank{
int getRateOfInterest(){
return 7;
}
}
class AXIS extends Bank{
int getRateOfInterest(){
return 9;
}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Output:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Note: In overriding method names and arguments must be same. That is method
signature must be same.
4. Until 1.4 version the return types must be same but from 1.5 version onwards
covariant return types are allowed.
Note: Co-variant return type concept is applicable only for object types but not for
primitives.
5. According to this Child class method return type need not be same as Parent
class method return type its Child types also allowed.
6. Access modifier of child method must not restrictive than parent class method.
Output:
Note: Private methods are not visible in the Child classes hence overriding concept
is not applicable for private methods. Based on own requirement we can declare
the same Parent class private method in child class also. It is valid but not
overriding.
Ex: -
class Parent{
Private void m1(){
}
}
Class Child extends Parent{
Private void m1(){
}
}
Note: Parent class final methods we can't override in the Child class.
Note: Parent class non final methods we can override as final in child class. We
can override native methods in the child classes.
Example:
class Parent {
public final void m1() {
}
}
class Child extends Parent{
public void m1(){
}
}
}
Output:
Compile time error:
m1() in Child cannot override m1() in Parent; overridden method is final
Note: We should override Parent class abstract methods in Child classes to provide
implementation.
Example:
Ex:-
class Parent {
}
abstract class Child extends Parent {