Web Design & Development Lecture 5

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Web Application Development Slides Credit Umair Javed LUMS.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
09 Inheritance. 2 Contents Defining Inheritance Relationships of Inheritance Rules of Inheritance super and this references super() and this() methods.
1. 2 Introduction to Inheritance  Access Modifiers  Methods in Subclasses  Method Overriding  Converting Class Types  Why up-cast?  Why down-cast?
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Programming With Java ICS Chapter 8 Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture 4: Extending Classes. Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Design issues for Object-Oriented Languages
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Lecture 17: Polymorphism (Part II)
Introduction to Programming with Java
Advanced Programming in Java
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
CS1316: Representing Structure and Behavior
Extending Classes.
Java Programming Language
CS1316: Representing Structure and Behavior
Everything the light touches, Simba, will be yours
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Lecture 18: Polymorphism (Part II)
Web Design & Development Lecture 4
Chapter 11 Inheritance and Polymorphism Part 1
Advanced Programming in Java
Presentation transcript:

Web Design & Development Lecture 5

Common Errors

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

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

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(); }

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

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

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

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

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

How to Use Java Doc

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

More OOP

Inheritance

Employee class hierarchy Teacher Manager Person

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

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 ….

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;

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

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

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

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 }

Compile & Execute

Polymorphism

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

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 }

Type Casting

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();

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