JavaProgramming_Lecture02-6
JavaProgramming_Lecture02-6
00
Object Oriented
Programming
Lecture -2/3
Sept, 15th 2022 (4pm-7pm)
Sept, 22nd 2022 (4pm – 4:25pm)
Learning outcomes:
• Important Java facts
• Implement a class
• Implement a class with arguments
• Understand the purpose and use of constructors
• Encapsulation
• Understand how Java implements Classes in Byte Code – Next week
Java Facts
• Every Java application begins with a class name
• class name MUST match the file name
• Every line of code must be in a class
• A class name should start with a capital letter
• In Java functions are called methods
• Every Java application MUST have a main() method
• execution start in the main() method – Entry point
• methods MUST start with lower case
• JAVA is case sensitive
• In Java every line MUST terminate with a semi-colon (;)
Objects Class = Cell Nucleus Instance Variables Methods
// args is an array of strings -- first we need to know how many args we have
// String is a class
// We can use the join method of the String class to join strings together
System.out.println(String.join(" ","joining","text","is","as simple as abc"));
// if user forgets to provide an arg then we create one ourselves for him
// otherwise we would get an array index error
if (args.length == 0) {args = new String[1]; args[0] = new String("You forgot your Arg");}
System.out.println(args[0]);
args = null; // we create objects with new and flag them for garbage collection by setting to
null
}
}
Operator Overloading
• Classes can define their own functions for operators
• operators are +, -. /, * etc
• Java String uses + for catenation of strings
• eg “Hello World: ” + args.length + “ items”
• this is called operator overloading
• Most object oriented languages allow classes to define their own
operators
• Java does not allow operator overloading
• … but they make an exception for String
• Why? Because the creator of Java, James Gosling thought people abused
operator overloading.
Creating your own Classes
• Classes are easy to define
• but they need to be in a different file
• Classes are constructed with a constructor
• constructors are just methods that have the same name as the
class
• constructors are optional, but Java secretly provides one for you if
you don’t
Class Example: Fractions
// if user forgets to provide an arg then we create one ourselves for him
// otherwise we would get an array index error
if (args.length == 0) {args = new String[1]; args[0] = new String("You forgot your Arg");}
System.out.println(args[0]);
}
Constructing a Class
• Constructors are methods which run automatically when the object is
created
• they have the same name as the Class itself
• Constructors can be overloaded
• multiple methods with the same name but different arguments
Constructors of Class – Extending Frac class
/* Hello World with Args */ /* The Fraction Class */
ublic class HelloFrac { class Frac {
public static void main(String[] args) { int n, d;
String name = "Programming in Java";
// args is an array of strings -- first we need to know how many args we have
System.out.println("Hello Args: your number of args is"+" "+args.length);
// We can use the join method of the String class to join strings together
public Frac() {
System.out.println(String.join(" ","joining","text","is","as simple as abc")); n = 0;
// String join also works for arrays of strings
d=0;
System.out.println("All args: "+String.join(" ",args)); System.out.println("Empty Constructor
// if user forgets to provide an arg then we create one ourselves for him Called:");
// otherwise we would get an array index error }
if (args.length == 0) {args = new String[1]; args[0] = new String("You forgot your Arg");}
// Class Frac with an argument int i
System.out.println(args[0]); public Frac(int i) {
n = i;
Frac a = new Frac(); d = i;
Frac b = new Frac(3); System.out.println("Int Constructor Called: " + i);
System.out.println("Type of Class: " + a.name); }
System.out.println("Your Frac is: " + a.n + " over " + a.d); }
args = null; // we create objects with new and flag them for garbage collection by setting to null
}
Encapsulation
• Encapsulating means hiding sensitive data (mainly object’s
variables)from other objects in the program within a class from
external access.
• Meaning no code outside of the object can see or manipulate them.
• Hence, they can’t introduce bugs by accidentally modifying the
object’s state.
• In Java Classes components are either public or private
• Elements of a Java Class can be made private
• this means it can only be accessed from inside the class
Encapsulation
/* Hello World with Args */ /* The Fraction Class */
ublic class HelloFrac { class Frac {
public static void main(String[] args) { int n, d;
private String name = "To infinity and
// args is an array of strings -- first we need to know how many args we have
System.out.println("Hello Args: your number of args is"+" "+args.length);
beyond with Rational Numbers";
// We can use the join method of the String class to join strings together public String getName() { return name; }
System.out.println(String.join(" ","joining","text","is","as simple as abc"));
public Frac(int i) {
Frac a = new Frac(); n = i; d=i;
Frac b = new Frac(3); System.out.println("Int Constructor
Called: " + i);
Frac c = new Frac(3,5); }
System.out.println("Type of Class: " + a.getName()); public Frac(int i, int j) {
System.out.println("Your Frac is: " + a.n + " over " + a.d); n = i; d=j;
System.out.println("Frac
args = null; // we create objects with new and flag them for garbage collection by setting to null
Constructor Called: " + i + " / " + j);
} }
}
Java Console I/O
• You can interact with a program directly through the Console
• the Console is also called the Command Line Interface (CLI) or Shell
• Consoles are text based interfaces
• Consoles display (print) text
• messages to the console are important for debugging
• Consoles also can read text from your keyboard
• input from the Arguments is the most basic
• input can also be read directly from the keyboard by the program
Java Console I/O Example Code:
/* Hello World with Console Input */
* Hello World with console */
ublic class HelloConsole {
if (args.length == 0) {args = new String[1]; args[0] = new String("Sorry, You forgot your Arg");}
System.console().writer().println(in);
}
JavaP - The javap tool is used to get the information of any class or
interface. The javap command (also known as the Java Disassembler) disassembles one or
more class files.
Do it yourself drill
Homework:
Online reading
• Java OOP (Object-Oriented Programming)
https://www.w3schools.com/java/java_oop.asp
• Java Classes and Objects
https://www.w3schools.com/java/java_classes.asp
• Java Class Methods
https://www.w3schools.com/java/java_class_methods.asp
• Java Constructors
https://www.w3schools.com/java/java_constructors.asp
• Java Encapsulation and Getters and Setters
https://www.w3schools.com/java/java_encapsulation.asp