Basics, OOP, and Interfaces
Basics, OOP, and Interfaces
• Course outline
• Programming basics
• Variables
• If, if-else, nested if, and if else ladder
Today
Syntax
switch(variable){
case <value1>:{Statement 1; break;}
case <value 2> {Statement2; break;}
default: {statement 3; break;}
}
Switch case example
public static void main(String args[]){
char letter = 'B';
switch(letter){
case 'A': {
System.out.println("Excellent"); break;
}
case 'B': {
System.out.println("Good"); break;
}
case 'C': {
System.out.println("Average"); break;
}
case 'D': {
System.out.println("Pass"); break;
}
default:{
System.out.println("Failed"); break;
}
}
}
Conditional Expression
Example:
int discount = price>=10000 ? 10 : 5 ;
• For loop
– if the number of iterations is known
• While loop
– until a condition is met
• Do-while loop
– run the code at least once before checking the condition
For loop
output: 1 2 3 4 5 6 7 8 9 10
For loop example
for (int rounds=1; rounds<=10; rounds++){
//print only the odd numbers
if(rounds%2 == 0){
continue;
}
System.out.print(round+” “);
}
output: 1 3 5 7 9
While loop
initialization;
while (condition){
statement
}
While loop
int steps = 1;
while(steps<=10){
System.out.print(steps+” “);
steps++;
}
output: 1 2 3 4 5 6 7 8 9 10
Do while loop
initialization;
do{
statement;
}while();
Do while loop
import java.util.Scanner;
Example:
Student studentOne = new Student();
Access members
• Syntax
– <Object Name>.<member variable>
– <Object Name>.<member function>()
Put all to gather
class Student{
....
}
public static void main(String args[]){
Student studentOne = new Student();
studentOne.SetName(“Peter”);
studentOne.GetName();
}
Visibility Controllers / Access Modifiers
public protected private
• Visible to its own class • Visible to its own class • Visible to its own class
• Can be inherited by sub class • Can be inherited by sub class • Cannot inherited by sub class
• Visible to outside classes • Not visible to outside classes • Not visible to outside classes
Constructor
• A method
• Name is equalent to the class name
• Do not return, no need to mention void
Constructor
class Student{
private String name;
public Student(){
this.name = “John”;
}
}
Constructor - overload
class Student{
private String name;
public Student(){
this.name = “John”;
}
public Student(String name){
this.name = name;
}
}
Constructor - overload
class SampleTwo{
public static void main(String args[]){
Student studentOne = new Student();
Student studentTwo = new Student(“David”);
System.out.println(studentOne.GetName());
System.out.println(studentTwo.GetName());
}
}
Static members
Example
class Employee{
private static int noOfLeaves = 10;
public static void SetNoOfLeaves(int leaves){
this.noOfLeaves = leaves;
}
}
Inheritance
• This means that the object can take many different forms.
• Overloading
– Functions with same name but different input parameters
• Overriding
– Inherited class / Sub class redefine the function
Final methods, variables, and classes
– Declare it as final
• Can be applied on
– Classes
– Methods
Also
}
Extending interface
interface <interface1> extends <interface2>{
}
Example
interface itemConstant{
int code = 1234;
String name = “Fan”;
}
interface item extends itemConstant{
void Display();
}
Combine interfaces
interface itemConstant{
int code = 1234;
String name = “Fan”;
}
interface itemMethod{
void Display();
}
class B implements A{
int count = value;
void methodX(){
}
}
Example: Create multiple inheriance class
}
InterfaceExample.java
public class InterfaceExample {
public static void main(String[] args) {
Circle circleOne = new Circle(10,30,20);
circleOne.DisplayParameters();
}
}