Class Note 06 - Object Oriented Programming and JDBC
Class Note 06 - Object Oriented Programming and JDBC
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();
a1.b=200; //Illegal
a1.mB(); //Illegal
}
}
02.
Exercise
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
Page 1 of 33
CMJD – Diploma in Comprehensive Master Java Developer
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();
//---------------------------
//call mB()of B
}
}
03 From 02.
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();
//---------------------------
//call mB()of B
B b1=a1; //Illegal
b1.b=200;
b1.mB();
}
}
Page 2 of 33
CMJD – Diploma in Comprehensive Master Java Developer
04.
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new B();
a1.a=100;
a1.mA();
//---------------------------
//call mB()of B
//B b1=a1; //Illegal
B b1=(B)a1; //Object Reference Casting
b1.b=200;
b1.mB();
}
}
05. Case I
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
Page 3 of 33
CMJD – Diploma in Comprehensive Master Java Developer
}
}
class Demo{
public static void main(String args[]){
A a1=new A();
a1.a=100;
a1.mA();
06. Case II
class A{
int a;
public void mA(){
System.out.println("mA of A");
}
}
class B extends A{
int b;
public void mB(){
System.out.println("mB of B");
}
}
class Demo{
public static void main(String args[]){
A a1=new A();
a1.a=100;
a1.mA();
Page 4 of 33
CMJD – Diploma in Comprehensive Master Java Developer
07. Exercise
class A{}
class B extends A{}
class C extends B{}
class Demo{
public static void main(String args[]){
A a1=new A();
A a2=new B();
A a3=new C();
B b1=new B();
B b2=new C();
C c1=new C();
Page 5 of 33
CMJD – Diploma in Comprehensive Master Java Developer
class A{}
class B{}
class Demo{
public static void main(String args[]){
A a1=new A();
B b1=new B();
a1=(A)b1; //Illegal
b1=(B)a1; //Illegal
}
}
09. Exercise
class A{}
class B{}
class Demo{
public static void main(String args[]){
A a1=new A();
B b1=new B();
System.out.println(a1 instanceof A);
System.out.println(a1 instanceof B); //Compile Error
System.out.println(b1 instanceof A); //Compile Error
System.out.println(b1 instanceof B);
}
}
10. Case IV
class A{}
interface P{}
class Demo{
public static void main(String args[]){
A a1=null;
P p1=null;
a1=(A)p1; //Legal
p1=(P)a1; //Legal
}
}
Page 6 of 33
CMJD – Diploma in Comprehensive Master Java Developer
11. Case V
a1=(A)p1; //Illegal
p1=(P)a1; //Illegal
}
}
Enumerations
12.
/*class Color{
public final static String RED="RED";
public final static String BLACK="BLACK";
public final static String BLUE="BLUE";
public final static String GREEN="GREEN";
}*/
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color c1; //Create an enum reference type "Color"
c1=Color.BLACK;
System.out.println(c1); //c1.toString()-->BLACK
}
}
Page 7 of 33
CMJD – Diploma in Comprehensive Master Java Developer
13. Case I
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color c1=new Color(); //Error
}
}
14. Case II
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color[] colors=Color.values();
for(Color c1:colors){
System.out.println(c1);
}
}
}
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color color=Color.BLACK;
switch(color){ //Legal
case RED : //
case BLUE:
case BLACK:
}
}
}
Page 8 of 33
CMJD – Diploma in Comprehensive Master Java Developer
16. Case IV
enum Color{
RED,BLACK,BLUE,GREEN; //enum Objects
}
class Demo{
public static void main(String args[]){
Color color=Color.valueOf("RED");
System.out.println(color);
}
}
17. Case V
enum Color{
RED(),BLACK(),BLUE(),GREEN;
Color(){
System.out.println("Color()");
}
}
class Demo{
public static void main(String args[]){
Color c1=Color.BLACK;
}
}
18. Case VI
enum Color{
RED(1001),BLACK(1002),BLUE(1003),GREEN(1004);
private int code;
Color(int code){
this.code=code;
}
public void setCode(int code){
this.code=code;
}
public int getCode(){
return code;
}
Page 9 of 33
CMJD – Diploma in Comprehensive Master Java Developer
}
class Demo{
public static void main(String args[]){
Color c1=Color.BLACK;
System.out.println(c1.getCode());
c1.setCode(2002);
System.out.println(c1.getCode());
}
}
Class “ArrayList”
Generic type safe class
As a list
Indexing and fast Random accessing Insertion order
19.
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
ArrayList<String> strList = new ArrayList<>();
strList.add(new String("A"));
strList.add(new String("B"));
strList.add(new String("C"));
Page 10 of 33
CMJD – Diploma in Comprehensive Master Java Developer
strList.add(new String("D"));
strList.add(new String("E"));
System.out.println(strList); // strList.toString"(;
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Customer 1"));
custList.add(new Customer(1002, "Customer 2"));
custList.add(new Customer(1003, "Customer 3"));
custList.add(new Customer(1004, "Customer 4"));
custList.add(new Customer(1005, "Customer 5"));
System.out.println(custList);
}
}
20.
import java.util.ArrayList;
class Customer {
private int code;
private String name;
Customer(int code, String name) {
this.code = code;
this.name = name;
}
public String toString() {
return code + "-" + name;
}
}
class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
Customer c1 = custList.get(2); // Indexing, Random access System.out.println("Customer
of index 2 : "+c1);
custList.add(2, new Customer(1111, "Sirisena"));
System.out.println(custList);
for (int i = 0; i < custList.size(); i++) {
Page 11 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.ArrayList;
class Customer {
private int code;
private String name;
Customer(int code, String name) {
this.code = code;
this.name = name;
}
public String toString() {
return code + "-" + name;
}
}
class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
for (Customer cust : custList) {
System.out.println(cust);
}
}
}
Page 12 of 33
CMJD – Diploma in Comprehensive Master Java Developer
22.
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Demo {
}
}
Page 13 of 33
CMJD – Diploma in Comprehensive Master Java Developer
23. Exercise
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
ArrayList<Customer> custList = new ArrayList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
boolean b = custList.contains(new Customer(1003, "Siripala"));
System.out.println("1003-Siripala : " + b); // false
custList.remove(new Customer(1003, "Siripala"));
System.out.println(custList);
}
}
Page 14 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.ArrayList;
class Customer {
private int code;
private String name;
/*
* public boolean equals(Object obj){
* return this==obj;
*}
*/
public String toString() {
return code + "-" + name;
}
}
class Demo {
public static void main(String args[]) {
Customer c1 = new Customer(1001, "Danapala");
Customer c2 = new Customer(1001, "Danapala");
Customer c3 = new Customer(1002, "Ganapala");
Customer c4 = c1;
System.out.println("c1 equals c2 : " + c1.equals(c2));// false
System.out.println("c1 equals c3 : " + c1.equals(c3));// false
System.out.println("c1 equals c4 : " + c1.equals(c4));// true
}
}
Page 15 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.ArrayList;
class Customer {
/*
* public boolean euqlas(Object obj){
* return this==obj;
*}
*/
public boolean equals(Object obj) {
// return this.code==obj.code;
// Customer c1=obj;
Customer c1 = (Customer) obj; // object reference casting
return this.code==c1.code;
}
Page 16 of 33
CMJD – Diploma in Comprehensive Master Java Developer
26 From 23.
import java.util.ArrayList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
Page 17 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
LinkedList<Customer> custList = new LinkedList<>();
custList.add(new Customer(1001, "Danapala"));
custList.add(new Customer(1002, "Gunapala"));
custList.add(new Customer(1003, "Siripala"));
custList.add(new Customer(1004, "Somapala"));
custList.add(new Customer(1005, "Amarapala"));
System.out.println(custList);
}
}
Page 18 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
LinkedList<Customer> custList = new LinkedList<>();
custList.push(new Customer(1001, "Danapala"));
custList.push(new Customer(1002, "Gunapala"));
custList.push(new Customer(1003, "Siripala"));
custList.push(new Customer(1004, "Somapala"));
custList.push(new Customer(1005, "Amarapala"));
System.out.println(custList);
Customer c1 = custList.pop();
System.out.println("Top customer : " + c1);
System.out.println(custList);
c1 = custList.peek();
System.out.println("Top customer : " + c1);
}
}
Page 19 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.util.LinkedList;
class Customer {
private int code;
private String name;
class Demo {
public static void main(String args[]) {
Page 20 of 33
CMJD – Diploma in Comprehensive Master Java Developer
Generics in Java
30.
class List{
private int[] items = new int[10];
private int count;
31.
class List{
Page 21 of 33
CMJD – Diploma in Comprehensive Master Java Developer
Customer Array
class Customer{}
class CustomerList{
private Customer[] customers = new Customer[10];
private int count;
public void add(Customer customer){
customers[count++] = customer;
}
public Customer get(int index){
return customers[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3);
CustomerList customerList = new CustomerList();
customerList.add(new Customer());
}
}
32.
class Customer{}
class List{
private Object[] items = new Object[10];
private int count;
public void add(Object item){
items[count++] = item;
}
public Object get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3); //==> Integer.valueOf(3); Wrapper Class
list.add("3");
list.add(new Customer());}}
Page 22 of 33
CMJD – Diploma in Comprehensive Master Java Developer
33.
class Customer{}
class List{
private Object[] items = new Object[10];
private int count;
public void add(Object item){
items[count++] = item;
}
public Object get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
List list = new List();
list.add(3); //==> Integer.valueOf(3); Wrapper Class
list.add("3");
list.add(new Customer());
34.
class Customer{}
Page 23 of 33
CMJD – Diploma in Comprehensive Master Java Developer
class Example {
public static void main(String[] args) {
List<Integer> list = new List<>();
list.add(1);
list.get(0);
//list.add("1"); //Illegal
35.
class Customer{}
class List<T>{ // Type Parameter for Class
private T[] items = (T[])new Object[10];
private int count;
public void add(T item){
items[count++] = item;
}
public T get(int index){
return items[index];
}
}
class Example {
public static void main(String[] args) {
///// Wrapper Classes //////////
//byte -> Byte
//short -> Short
//int -> Integer
//long -> Long
//float -> Float
//double -> Double
//char -> Character
//boolean -> Boolean
//List<int> list = new List<>(); // Compile Error.
List<Integer> list = new List<>();
list.add(1); // Boxing
int num = list.get(0); // Unboxing
}
}
Page 24 of 33
CMJD – Diploma in Comprehensive Master Java Developer
36.
class Customer{}
JDBC
Page 25 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Page 26 of 33
CMJD – Diploma in Comprehensive Master Java Developer
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
42 From 41
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
Page 27 of 33
CMJD – Diploma in Comprehensive Master Java Developer
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
Page 28 of 33
CMJD – Diploma in Comprehensive Master Java Developer
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Supermarket", "root",
"Xhq8nc3mcj");
Page 29 of 33
CMJD – Diploma in Comprehensive Master Java Developer
statement.setString(9, zip);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
}
}
Page 30 of 33
CMJD – Diploma in Comprehensive Master Java Developer
class A{
public A(){}
public void myMethod(){
System.out.println("myMethod in : " + this);
}
}
public class Main {
public static void main(String[] args){
A a1 = new A();
a1.myMethod();
A a2 = new A();
a2.myMethod();
}
}
45. Step 2
class A{
private static A a;
public A(){}
public static A getInstance(){
if(a == null){
a = new A();
}
return a;
}
public void myMethod(){
System.out.println("myMethod in : " + this);
}
}
public class Main {
public static void main(String[] args){
A a1 = new A();
a1.myMethod();
A a2 = new A();
a2.myMethod(); }}
Page 31 of 33
CMJD – Diploma in Comprehensive Master Java Developer
46. Step 3
class A{
private static A a;
private A(){}
A a2 = A.getInstance();
a2.myMethod();
}
}
DBConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Page 32 of 33
CMJD – Diploma in Comprehensive Master Java Developer
"Xhq8nc3mcj");
}
Main.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
Page 33 of 33