Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon 1. Overriding Methods A subclass can modify behavior inherited from a parent class. • A subclass can create a method with different functionality than the parent’s method but with the same: • Name • Return type • Argument list Then the new method is said to override the old one. So, what is the objective of subclass? Example Public class Employee { protected String name; protected double salary; protected Date birthDate; public String getDetails() { return “Name: “ + name + “\n” + “Salary: “ + salary; } } public class Manager extends Employee { protected String department; Public String getDetails() { return “Name: “ + name + “\n” + “Salary: “ + salary + "\n" + “Manager of: “ + department; } } Page 1 Date: September 19, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon The Manager class has a getDetails method by definition because it inherits one from the “Employee class”. However, the original has been replaced, or overridden by the child class ‘version. 2. Overridden Methods Cannot Be Less Accessible Public class Parent { Public void doSomething() { } } Public class Child extends Parent { Private void doSomething() {} // illegal } public class UseBoth { public void doOtherThing() { Parent p1 = new Parent(); Parent p2 = new Child(); p1.doSomething(); p2.doSomething(); } } The Java programming language semantics dictate that p2.method () results in the child version of the method being executed, because the method is declared private, p2 ( declared as parent ) cannot access it , this the semantics language is violated 3. Invoking Overridden Methods A subclass method may invoke a superclass method using the super keyword: • The keyword super is used in a class to refer to its superclass. Page 2 Date: September 19, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon • The keyword super is used to refer to the members of superclass, both data attributes and methods. • Behavior invoked does not have to be in the superclass; it can be further up in the hierarchy. public class Employee { private String name; private double salary; private Date birthDate; public String getDetails() { return "Name: " + name + "\nSalary: " + salary; } } public class Manager extends Employee { private String department; public String getDetails() { // call parent method return super.getDetails() “\nDepartment: " + department; } } The Object Class The object class is the root of all classes in the Java technology programming language. If a class is declared with no extends clause, then the compiler adds implicitly the code extend Object to the declaration for example Public class Employee { // code here } Is equivalent to Public class Employee extends Object { Page 3 // code goes here } Date: September 19, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon Two important methods are: • equals • toString 1. The equals Method The == operator determines if two references are identical to each other (that is, refer to the same object). The equals method determines if objects are equal but not necessarily identical. The Object implementation of the equals method uses the == operator User classes can override the equals method to implement a domain-specific test for equality. Note: You should override the hashCode method if you override the equals method. Example public class MyDate1 { private int day; private int month; private int year; // constructor public MyDate1(int day2, int month2, int year2) { this.day = day2; this.month = month2; this.year = year2; } Page 4 Date: September 19, 2013 3rd class – Department of Network College of IT- University of Babylon Java Programming Mehdi Ebady Manaa public boolean equals(Object o) { boolean result = false; if ( (o != null) && (o instanceof MyDate1) ) { MyDate1 d = (MyDate1) o; if ( (day == d.day) && (month == d.month)&& (year == d.year) ) { result = true; } // end if }// end if return result; } // end method public int hashCode() { return (day ^ month ^ year); } // end method hashCode } public class TestEquals { public static void main(String[] args) { MyDate1 date1 = new MyDate1(10, 10, 1976); MyDate1 date2 = new MyDate1(10, 10, 1976); if ( date1 == date2 ) { System.out.println("date1 is identical to date2"); } else { System.out.println("date1 is not identical to date2"); Page 5 Date: September 19, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon } if ( date1.equals(date2) ) { System.out.println("date1 is equal to date2"); } else { System.out.println("date1 is not equal to date2"); System.out.println("set date2 = date1;"); } date2 = date1; if ( date1 == date2 ) { System.out.println("date1 is identical to date2"); } else { } System.out.println("date1 is not identical to date2"); } } This example generates the following output: date1 is not identical to date2 date1 is equal to date2 set date2 = date1; date1 is identical to date2 2. The toString method It has the following characteristics: • This method converts an object to a String. • Use this method during string concatenation. Page 6 Date: September 19, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon • Override this method to provide information about a userdefined object in readable format. class Bank { String n; String add; int an; int bal; int dep; public Bank(String n,String add,int an,int bal) { this.add=add;this.bal=bal; this.an=an;this.n=n; } public String toString() { return "Name of the customer.:" + this.n+",, "+"Address of the customer.:"+this.add +",, "+"A/c no..:"+this.an+",, " +"Balance in A/c..:"+this.bal; } } Q/ how we convert primitive types to a String using toSting method?? Page 7 Date: September 19, 2013