Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Design & Development Lecture 5

Similar presentations


Presentation on theme: "Web Design & Development Lecture 5"— Presentation transcript:

1 Web Design & Development Lecture 5

2 Common Errors

3 Common Errors NoClassDefFoundError NoSuchMethodError: main
Cannot resolve symbol Local variables must be initialized ArrayIndexOutofBounds & NumberFormat exception

4 Error: Exception in thread “main” NoClassDefFoundError
public class CommonErrors{ public static void main (String args[]) { System.out.println(“Hello World”); }

5 Error: Exception in thread “main” NoSuchMethodError: main
public class CommonErrors{ public static void display () { System.out.println(“hello world”); } public static void main (String args[]) { CommonErrors.display(); }

6 Error: cannot resolve symbol
public class CommonErrors{ public static void main (String args[]) { int number; System.out.println(number1); } number);

7 Error: Local variable not initialized
public class CommonErrors{ public static void main (String args[]) { int number ; System.out.println(number); } = 10 ;

8 Error: ArrayIndexOutofBoundsException
public class CommonErrors{ public static void main (String args[]) { System.out.println(args[0]); }

9 Error: NumberFormatException
public class CommonErrors{ public static void main (String args[]) { String s = “20s”; int number = Integer.parseInt(s); } = “20” ;

10 Error: NullPointerException
public class CommonErrors{ public static void main (String args[ ]) { Student s = null; s.print( ); }

11 How to Use Java Doc

12 How to use Java Doc public class DocDemo{
public static void main (String args[]) { String s = “Hello World”; // I want to convert this string into Upper case? } String upper = s.toUpperCase(); //upper contains “HELLO WORLD” System.out.println(upper); //will print HELLO WORLD

13 More OOP

14 Inheritance

15 Employee class hierarchy
Teacher Manager Person

16 Employee. java (super class)
public class Employee{ protected int id; protected String name; //parameterized constructor public Employee(int id, String name){ this.id = id; this.name = name; } //default constructor public Employee(){ this (10, “not set”); //continue

17 Employee. java (super class)
public void setId (int id) { this.id = id; } public int getId () { return id; public void setName (String name) { this.name = name; public String getName () { return name; //continue ….

18 Employee. java (super class)
public void display(){ System.out.println(“in employee display method”); System.out.println("Employee id:" + id + " name:" + name); } //overriding object class toString method public String toString() { System.out.println(“in employee toString method”); return "id:" + id + "name:" + name;

19 Teacher. java (sub class)
public class Teacher extends Employee{ private String qual; //default constructor public Teacher () { //implicit call to superclass default construct qual = ""; } //parameterized constructor public Teacher(int i, String n, String q){ super(i,n); //call to superclass const must be first line qual = q; //continue

20 Teacher. java (sub class)
//accessors for qual public void setQual (String qual){ this.qual = qual; } public String getQual(){ return qual; //continue

21 Teacher. java (sub class)
//overriding dispaly method of Employee class public void display(){ System.out.println("in teacher's display method"); super.display(); //call to superclass display method System.out.println("Teacher qualification:" + qual); } //overriding toString method of Employee class public String toString() { System.out.println("in teacher's toString method"); String emp = super.toString(); return emp +" qualification:" + qual; }// end of class

22 Test. java (Driver class)
public class Test{ public static void main (String args[]){ System.out.println("making object of employee"); Employee e = new Employee(89, "khurram ahmad"); System.out.println("making object of teacher"); Teacher t = new Teacher (91, "ali raza", "phd"); e.display(); //call to Employee class display method t.display(); //call to Teacher class display method System.out.println("Employee: " +e.toString()); System.out.println("Teacher: " + t); } //end of main }

23 Compile & Execute

24 Polymorphism

25 Polymorphism “Polymorphic” literally means “of multiple shapes” and in the context of OOP, polymorphic means “having multiple behavior” A parent class reference can point to the subclass objects. For example Employee reference Can point to the Employee Object Can point to the Teacher Object Can point to the Manager Object A polymorphic method results in different actions depending on the object being referenced Also known as late binding or run-time binding

26 Modify Test. java (of Inheritance Example)
public class Test { public static void main (String args[]){ // Make employee references Employee ref1, ref2; ref1 = new Employee(89, "khurram ahmad"); //upcasting, is-a relationship ref2 = new Teacher (91, "ali raza", "phd"); ref1.display(); //call to Employee class display method ref2.display(); //call to Teacher class display method System.out.println("Employee: " +ref1.toString()); System.out.println("Teacher: " + ref2.toString()); } //end of main }

27 Type Casting

28 Type Casting Java is strongly typed language Up-casting Implicit
No loss of information Example of Primitives int a = 10; double b = a; Classes Employee e = new Teacher();

29 Type Casting Downcasting Explicit Possible loss of information
Example of Primitives double a = 7.65; int b = (int) a; Classes Employee e = new Teacher(); //upcasting Teacher t = (Teacher) e; //downcasting


Download ppt "Web Design & Development Lecture 5"

Similar presentations


Ads by Google