0% found this document useful (0 votes)
55 views25 pages

Lect 6 - Closer Look To Classes and Method

This document provides an overview of various object-oriented programming concepts in Java including method overloading, constructor overloading, argument passing, recursion, access modifiers, static members, final keyword, arrays, nested and inner classes, strings, command line arguments, and variable arguments. Key topics covered include determining the method called based on argument type/number, type promotion in overloading, passing arguments by value vs reference, returning objects from methods, using static and final keywords, accessing variables and calling methods on nested classes, string immutability, string methods, and how varargs must be the last parameter.

Uploaded by

Sonu Sachdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views25 pages

Lect 6 - Closer Look To Classes and Method

This document provides an overview of various object-oriented programming concepts in Java including method overloading, constructor overloading, argument passing, recursion, access modifiers, static members, final keyword, arrays, nested and inner classes, strings, command line arguments, and variable arguments. Key topics covered include determining the method called based on argument type/number, type promotion in overloading, passing arguments by value vs reference, returning objects from methods, using static and final keywords, accessing variables and calling methods on nested classes, string immutability, string methods, and how varargs must be the last parameter.

Uploaded by

Sonu Sachdev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

A Closer Look at Methods and Classes

(Chapter 7 of Schilit)
Object Oriented Programming BS (CS/SE) II
By
Dr. Sher Muhammad Dadupota
Overloading Methods
• Concept
• Type or number of argument determine the method to be called
• Return type alone is not sufficient
Overloading Methods
• Type promotion in overloading
• Example of abs() method
Overloading Constructors
Using Object as Parameters
A closer look at Argument passing
• Pass by value
• Pass by reference
Returning objects from a method as return
type
Recursion
• The factorial example
Access Controls in Java
• public
• private
• protected
• default

• Stack example
Understanding static member variables
• static member variable of class
• Associated with class
• One copy for all objects
• Example
Understanding static member methods
• Can only call other static methods
• Can access only static member variables of class
• Can be called on class name as well as object reference
• Can’t refer this and super keywords
• Example
Understanding static code block
• Gets executed exactly once

• Example (static member variable, method and block)


Introducing keyword final
• Usage with variable
• Usage with methods
Arrays Revisiting
• Length variable
• Example (varying size of arrays with static non-static use)
• Example (dynamic size of Stack array)
Nested and Inner Classes
• Nested class
• Class within a class
• Scope of nested in bound with outer class
• If B is inside A, B will not exist until A is created
• Nested has access to member variables including private of outer
• Vice versa is not true
• Two types of nested class, static and non-static
• non-static are common
• Example
Nested and Inner Classes
• Use of static inside inner class
• Inner static class
Inner class inside loop
String class
• Class in java.lang
• Even string constants inside System.out are string objects
• String objects are immutable
• StringBuffer and StringBuilder are mutatable strings
String class
• Example: Concatenation with String
String class
• String methods
• equals()
• length()
• charAt()
String class
• equals() method vs == operator
• equals() checks value
• == checks reference
Command-line arguments
Varargs: Variable Length Arguments
• Support started with JDK 5
• Example: Old style variable arguments through array
• … syntax
• Varargs must be last parameter (why?)
• Must be only one
Varargs in method overloading
Varargs and Ambiguity

You might also like