OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java...

Post on 24-Jun-2020

12 views 0 download

Transcript of OBJECT-ORIENTED PROGRAMMINGMethod Overloading Method overloading is one of the ways that Java...

OBJECT-ORIENTED

PROGRAMMINGLECTURE # 18-22

Content

Method Overloading

Constructor Overloading

Encapsulation and Access Specifiers

static Keyword

static block

Introducing Math class

final keyword

Nested and Inner Classes

Method Overloading

Method overloading is one of the ways that Java supports

polymorphism.

Defining two or more methods with the same name in a class is

called method overloading.

The compiler is able to distinguish between the methods

because of their method signatures.

The name of a method together with the types and sequence

of the parameters form the signature of the method.

Method Overloading

Examples:

System.out.println(“hello”);

System.out.println(23);

System.out.println(58.25);

System.out.println(‘a’);

Method Overloading

class OverloadDemo {

void test() { System.out.println("No parameters");

}

// Overload test for one integer parameter. void test(int a) { System.out.println("a: " + a);

}

// Overload test for two integer parameters. void test(int a, int b) { System.out.println("a and b: " + a + " " + b);

}

// overload test for a double parameter double test(double a) { System.out.println("double a: " + a);

return a*a; }

}

Method Overloading

class Overload {

public static void main(String args[]) {

OverloadDemo ob = new OverloadDemo(); double result; ob.test();

ob.test(10);

ob.test(10, 20); result = ob.test(123.2);

System.out.println("Result of ob.test(123.2): " + result);

}

}

Constructor Overloading

Constructors are methods that can be overloaded, just like

any other method in a class.

When you want to generate objects of a class from different

sets of initial defining data.

Constructor Overloading

class Student { String name;String rollno;int age;

Student(){System.out.println(“no information provided”);name = “”;rollno = “”; age = 0;}

Student(String n, String r, int a){name = n;rollno = r; age = a;}

void getStudentInfo(){System.out.println(“name = ”+name+”\n roll no. = rollno+”\n age =

”+age);

}}

Constructor Overloading

class UseStudentClass{

public static void main(){

Student std1 = new Student();Student std2 = new Student(“Al”, “19SW09”, 21);

std1.getStudentInfo();std2.getStudentsInfo();

}}

Encapsulation and Access Specifiers

Java’s access specifiers are public, private, and protected.

Java also defines a default access level.

Encapsulation, you can control what parts of a program can

access the members of a class.

By controlling access, you can prevent misuse.

Encapsulation and Access Specifiers

Java provides the following access specifiers:

Encapsulation and Access Specifiers

class Test {

int a; // default accesspublic int b; // public access

private int c;

void setc(int i) { c = i;

}

int getc() { return c;

}

}

Encapsulation and Access Specifiers

class AccessTest {

public static void main(String args[]) {

Test ob = new Test();ob.a = 10;ob.b = 20;

// ob.c = 100; // Error!

ob.setc(100); // OKSystem.out.println("a, b, and c: " + ob.a + " " +ob.b + " " + ob.getc());

}

}

static Keyword

A static class member can be accessed without creating an

object.

You can declare both methods and variables to be static.

main() is a common example.

static Keyword

Methods declared as static have several restrictions:

They can only call other static methods.

They must only access static data.

They cannot refer to this or super in any way.

static Keyword

class StaticDemo {

static String name;static int age;

static void getInfo(){System.out.println(“name ”+name);System.out.println(“age ”+age);}

}class UseStaticDemo{public static void main(String args[]){

StaticDemo.getInfo();

}}

static block

Static blocks are also called Static initialization

blocks .

A static initialization block is a normal block of code

enclosed in braces, { }, and preceded by the static

keyword.

static{

//whatever code is needed for initialization goes here

}

Introducing Math Class

The java.lang.Math class contains methods for performing basic

numeric operations such as the elementary exponential, logarithm,

square root, and trigonometric functions.

Access these functions without making the object as they are all

declared static.

E.g. Math.method(arg)

Code it now!

Consider a right-angle triangle like this one. Write a

java class with three instance variables hypotenuse,

perpendicular and base. Create two overloaded

constructor for initializing these lengths including a

default constructor that gives default values.

Create three functions,

getHyp(double , double) returns hyp after calculation,

getPerp(double, double) returns perp after calculation,

getBase(double, double) returns base after calculation.

Code it now!

Overload the functions in the previous slides:

getHyp(double length, double angle)

getBase(double length, double angle)

getPerp(double length, double angle)

Each function must return the side after taking

length and angle

final Keyword

Doing so prevents its contents from being modified.

This means that you must initialize a final variable

when it is declared.

It is a common coding convention to choose all

uppercase identifiers for final variables

final double PI = 3.142;

Nested and Inner Classes

You can put the definition of one class inside the definition of another class.

The inside class is called a nested class.

Syntax:

public class Outside {

// Nested class

public class Inside {

// Details of Inside class...

}

// More members of Outside class...

}

Nested and Inner Classes

A top-level class is a class that contains a nested

class but is not itself a nested class.

Nested and Inner Classes

class Outer {int outer_x = 100;void test() {Inner inner = new Inner();inner.display();}// this is an inner classclass Inner {void display() {System.out.println("display: outer_x = " + outer_x);}}}class InnerClassDemo { public static void main(String args[]) {Outer outer = new Outer();outer.test();

} }