0% found this document useful (0 votes)
16 views21 pages

Lecture 10

The document outlines the syllabus for a Java Programming course, covering key concepts such as method overloading, garbage collection, inheritance, and access control. It includes objectives, examples of code, and explanations of Java features like static variables and the String class. Additionally, it provides references for further learning and concludes with a thank you note.

Uploaded by

Deepak Raj
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)
16 views21 pages

Lecture 10

The document outlines the syllabus for a Java Programming course, covering key concepts such as method overloading, garbage collection, inheritance, and access control. It includes objectives, examples of code, and explanations of Java features like static variables and the String class. Additionally, it provides references for further learning and concludes with a thank you note.

Uploaded by

Deepak Raj
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/ 21

UNIVERSITY INSTITUTE COMPUTING

BSc (CS)/ BCA


Subject Name: Java Programming
Code: 22SCT-351/22CAT-351

JAVA PROGRAMMING DISCOVER . LEARN . EMPOWER


1
Syllabus

Unit-I
Method Overloading

• Method Overloading, Garbage Collection, Final, Finalize and Finally Method,


Comparison between Java and C++. Inheritance, Interface, Abstract Class, Interfaces

2
Content

(Unit I)
Method Overlaoding
Chapter 1.3:
Lecture 1.3.1.-
1. Method Overloading
2. G

3
Objectives/Outcomes

CO1: Identify the ways to implement the inheritance concept in


the Java programming language.

4
Method with Parameters
class Rectangle {
public class WithParaMain {
double width;
double height; public static void main(String args[]) {
double area() { Rectangle mybox1 = new Rectangle();
return width * height;
double vol;
}
void setDim(double w, mybox1.setDim(10, 20);
double h) { vol = mybox1.area(); System.out.println("Area is " + vol);
width = w;
height = h; }
} }
}

5
Method Overloading
• Overloaded methods have the same name but different parameters.
• Overloaded methods must differ in the type and/or number of their parameters.
• Overloaded methods may have different return types.
• return type alone is insufficient to distinguish two methods.
class OverloadDemo { public class MeOverloadingMain
void test() { { public static void main(String args[])
System.out.println(" {
No parameters"); OverloadDemo ob = new OverloadDemo();
} ob.test();
void test(int a) ob.test(10);
{ System.out.println("a: " + ob.test(10, 20);
a); double result = ob.test(123.25);
} System.out.println("Result of ob.test(123.25): " +
void test(int a, int b) { result);
System.out.println("a and b: " }
+ a + " " + b); }
}
double test(double a) {
System.out.println("double a: " + a); 6
Garbage collection
• Java handles deallocation automatically.
• The automated deallocation is called garbage collection.
• When no references to an object exist, and that object is eligible for garbage collection.
The finalize( ) Method
• The Java calls finalize( ) method whenever it is about to recycle an object.
• finalize() is called just prior to garbage collection.
The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
•The keyword protected prevents access to finalize( ) by code defined outside its class.

7
Inner Classes
 A class within another class is called nested classes.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
p
u
b
l
i
c
8
Nested Classes
We can define a nested class within the block defined by a method.
public class NestedMain {
int outer_x = 100;
void test() {
for (int i = 0; i < 10; i++) {
class Inner {
void display()
{
System.
out.println("display
: outer_x = " +
outer_x);
}
}
Inner inner =
new Inner();
inner.display(
);
}
9
}
Access control
 Java's access specifiers are public, private, protected and a default access level.
1. A public class member can be accessed by any other code.
2. A private class member can only be accessed within its class.

3. Default (without an access modifier).


 A class's fields, methods and the class itself may be default.
 A class's default features are accessible to any class in the same package.
 A default method may be overridden by any subclass that is in the same package as the
superclass.

4. Protected
 protected features are more accessible than default features.
 Only variables and methods may be declared protected.
 A protected feature of a class is available to all classes in the same package(like a default).
 protected feature of a class can be available to its subclasses.

10
Program for access control
class Test { public class AccessMain {
int a; // default access public static void main(String args[]) {
public int b; // public access Test ob = new Test();
private int c; // private access ob.a = 1;
ob.b = 2;
// methods to access c
void setc(int i) { // This is not OK and will cause an error
c = i; // ob.cAccess
= 100; control// Error!
}
// You must access ‘c’ through its methods
int getc() {
return c; ob.setc(100); // OK
} System.out.println("a, b, and c: " + ob.a
+ " " + ob.b + " " + ob.getc());
}
}
}

11
What is Static ?
When a member is declared static, it can be accessed before any objects of its class
are created, and without reference to any object.
 You can declare both methods and variables to be static.
 The most common example of a static member is main( ). main( ) is declared as
static because it must be called before any objects exist.
 Instance variables declared as static are, essentially, global variables.
 When objects of its class are declared, no copy of a static variable is made. Instead, all
instances of the class share the same static variable.
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.

12
Program on static variables,
What is Static ? methods, and blocks.
class UseStatic {
static int a = 3;
static int b;
static void meth(int x)
{ System.out.println("x = " +
x); System.out.println("a = " +
a); System.out.println("b = " +
b);
}
static {
System
.out.pri
ntln("St
atic
block
initializ
ed.");
b=a* 13
4;
Program on static method and the
static variable are accessed outside
Program on static variables, methods, and blocks.

of their class.
class StaticDemo
{ static int a = 42;
static int b = 99;
static void callme() {
S
y
s
t
e
m
.
o
u
t
.
p
r
i 14
n
Final
 A variable can be declared as final.
• Doing so prevents its contents from being modified.
•This means that you must initialize a final variable when it is declared.
For example:
1. final int FILE_NEW = 1;
2. final int FILE_OPEN = 2;
3. final int FILE_SAVE = 3;
4. final int FILE_SAVEAS = 4;
5. final int FILE_QUIT = 5;

• It is a common coding convention to choose all uppercase identifiers for final


variables.
• Variables declared as final do not occupy memory on a per-instance basis.
• a final variable is essentially a constant.
• The keyword final can also be applied to methods, but its meaning is substantially
different than when it is applied to variables.

15
Difference between Final, Finally, Finalize
final finally finalize

finally block is used in Java Exception Handling to


final is a keyword and access modifier in Java. execute the mandatory piece of code after try-catch finalize() is the method in Java.
blocks.

final access modifier is used to apply restrictions on finally block executes whether an exception occurs or finalize() method is used to perform clean-up
the variables, methods, and classes. not. It is primarily used to close resources. processing just before an object is garbage collected.

It is used with variables, methods, and classes. It is with the try-catch block in exception handling. It is used with objects.

Once declared, the final variable becomes constant finally block cleans up all the resources used in finalize method performs the cleaning with respect to
and can't be modified. the try block. the object before its destruction.

finally block executes as soon as the execution of try-


finalize method is executed just before the object is
final is executed only when we call it. catch block is completed without depending on the
destroyed.
exception.

16
String class
 String is probably the most commonly used class in Java’s class library.
 The reason for this is that strings are a very important part of programming.
 The first thing to understand about strings is that every string you create is actually an
object of type String.
 Even string constants are actually String objects.
For example, in the statement
System.out.println("This is a String, too");
the string “This is a String, too” is a String constant.
• objects of type String are immutable; once a
String object is created, its contents cannot
be altered.
While this may seem like a serious restriction, it is
not, for two reasons:
• If you need to change a string, you can always create a new one that contains the
modifications.
• Java defines a peer class of String, called StringBuffer, which allows strings to be altered,
so all of the normal string manipulations are still available in Java.
17
Strings can be constructed a variety of ways. The easiest is to use a statement like this:
Program on String class
class StringDemo {
public static void main(String args[])
{ String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1 + " and " + strOb2;
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}

18
Program on String Methods
class StringDemo2 {
public static void main(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " + strOb1.length());
System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}
19
References
• https://www.geeksforgeeks.org/loops-in-java/#:~:text=ArrayList%20in%20Java-,L
oops%20in%20Java,some%20condition%20evaluates%20to%20true.&text=while
%20loop%3A%20A%20while%20loop,on%20a%20given%20Boolean%20conditi
on.
• https://www.javatpoint.com/java-do-while-loop

20
THANK YOU

21

You might also like