Beruflich Dokumente
Kultur Dokumente
FaaDoOEngineers.com
1 FaaDoOEngineers.com
Day 2 Coverage
Inheritance
Method Overriding Abstract Class Interface Overview of predefined packages Creating User Defined Packages
FaaDoOEngineers.com
FaaDoOEngineers.com
FaaDoOEngineers.com
Objectives
Inheritance
Method Overriding Use of super keyword Interface Abstract Class
FaaDoOEngineers.com
public class Person { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } }
6 FaaDoOEngineers.com
public class Employee extends Person { private String eid; public void setEid(String eid) { this.eid=eid; } public String getEid() { return eid; } }
Example
Class Person
Class Employee
7 FaaDoOEngineers.com
Class Student
Example
Class Person
Class Employee
Class Regular
8 FaaDoOEngineers.com
name
number
sequence
type
of arguments in a method
9 FaaDoOEngineers.com
You can override (most) methods in a parent class by defining a method with the same name and parameter list in the child class. This is useful for changing the behavior of a class without changing its interface. You cannot override the static and final methods of a superclass. A subclass must override the abstract methods of a superclass.
10
FaaDoOEngineers.com
public void setID(String empID) { this.empID=empID; } public String getId() { return empID; } }
public class Person { private String name; public Person(String name) { this.name = name; } public String getName() { return name; } }
12
FaaDoOEngineers.com
Restricting Inheritance
Parent
Using final modifier with a class will restrict the inheritance capability of that class
Restricting Inherited capability
Child
13
FaaDoOEngineers.com
{ // members
} final class Employee extends Person { // members }
14
FaaDoOEngineers.com
This ensures that functionality defined in this method cannot be altered any. Similarly, the value of a final variable cannot be altered.
15
FaaDoOEngineers.com
A programmers tool for specifying certain behaviors that an object must have in order to be useful for some particular task.
Interface is a conceptual entity. Can contain only constants (final variables) and abstract method (no implementation). Use when a number of classes share a common interface. Each class should implement the interface.
16
FaaDoOEngineers.com
For example, you might specify Driver interface as part of a Vehicle object hierarchy. A human can be a Driver, but so can a Robot.
17
FaaDoOEngineers.com
Example Declaration
18
FaaDoOEngineers.com
Politician speak()
Priest speak()
Lecturer speak()
20
FaaDoOEngineers.com
Lecturer
that implements the Speaker interface and defines the speak method.
21
FaaDoOEngineers.com
22
FaaDoOEngineers.com
23
FaaDoOEngineers.com
24
FaaDoOEngineers.com
Shape
Circle
Rectangle
25
FaaDoOEngineers.com
Example Syntax
26
FaaDoOEngineers.com
27
FaaDoOEngineers.com
Exercise
Declare the Train class with the following list of methods: OrgName() TrainNo() TrainName() FromTo()
DreamExpress
LiveExpress TimeExpress
that extends from Train class and define the TrainNo(), TrainName and FromTo() method.
29
FaaDoOEngineers.com
Summary
Inheritance is the concept of extending data members and methods of a superclass in a subclass. You can derive data members and methods from a single superclass that is a subclass of another superclass. Java does not support multiple inheritance directly. You can use the concept of method overriding to override the superclass method with the subclass method having same names. Interface is a concept of creating data members and methods that can be derived by multiple classes in Java. Interfaces also allow you to declare set of constants that can be imported into multiple classes.
30
FaaDoOEngineers.com
Summary (Continued)
The methods declared in the interface are defined in the class implementing that interface. The methods in an interface are only abstract methods. Abstract classes provides a common root for a group of classes, nicely tied together in a package.
31
FaaDoOEngineers.com
Packages
FaaDoOEngineers.com
FaaDoOEngineers.com
Objectives
Packages in Java Overview of java.lang package Overview of java.util package Creating User Defined Package
35
FaaDoOEngineers.com
Description
Includes various classes, such as Object, System, Thread etc. Includes all Input-Output Stream related classes. Provides various classes that support Date, Collection. Provides API for Database operation
37
FaaDoOEngineers.com
Description
Providing Supporting classes for Graphic User Interface components. Provides the Applet class to create web based application. Includes classes that support network programming such as Socket, DatagramSocket.
java.net
38
FaaDoOEngineers.com
Java
java.lang
java.io
java.util
java.awt
java.applet
java.net
java.sql
39
FaaDoOEngineers.com
The java.lang package provides various classes and interfaces that are fundamental to Java programming. The java.lang package contains various classes that represent primitive data types, such as int, char, long, and double. Classes provided by the java.lang package:
Class
Object
Description
All the classes in the java.lang package are the subclasses of the Object class. It provides a standard interface to input output and error devices, such as Keyboard & VDU.
System
40
FaaDoOEngineers.com
Description
Supports runtime processing of the class information of an object.
Provides functionality for String manipulation. Provides methods to convert an Integer object to a String object. Provides functions for statistical, exponential operations.
41
FaaDoOEngineers.com
Description
Returns the value of an object as the primitive data type int. Returns the value of an object as the primitive data type float. Returns the value of an object as the primitive data type double.
42
FaaDoOEngineers.com
Description
43
FaaDoOEngineers.com
Description
Returns the integer value closest to the parameter. Returns the next highest integer of the supplied parameter. Returns the next smallest integer of the supplied parameter.
44
FaaDoOEngineers.com
Description
Returns the absolute value of that parameter.
Accepts no argument and returns a positive, double, value randomly generated, greater than or equal to 0.0 and less than 1.0.
45
FaaDoOEngineers.com
Description
Returns the length of a String object. Converts all the characters in the string object in uppercase. Converts all the characters in the string object in lowercase. Returns the String representation of the object.
46
FaaDoOEngineers.com
Description
Returns the length of a String object. Compares current String object with another String object. If the Strings are same the return value is 0 else the return value is non zero. If str1 > str2 then return value is a positive number If str1<str2 then return value is a negative number
47
FaaDoOEngineers.com
public int indexOf (String str) public int indexOf (String str, int startindex)
public int lastIndexOf(String str) public int lastIndexOf(String str, int startindex)
48
FaaDoOEngineers.com
Description
Returns a copy of the string, with leading and trailing whitespace omitted. Returns the char value at the specified index. Concatenates the specified String to the end of the String. Returns a new String that is a substring of a String.
Description
Tests if the String starts with the specified prefix. Tests if the String end with the specified suffix. Converts this String to a new character array. Copies characters from this string into the destination character array.
public char[] toCharArray() public void getChars(int scrBegin,int srcEnd,char[] destination,int dstBegin)
50
FaaDoOEngineers.com
The java.util package provides various utility classes and interfaces that support date and calendar operations, String manipulations and Collections manipulations. Classes provided by the java.util package:
Class
Date Calendar
Description
Encapsulates date and time information. Provides support for date conversion. It is a subclass of Calendar class, provides support for standard calendar used worldwide.
GregorianCalendar
51
FaaDoOEngineers.com
Description
Generated a stream of random numbers. An indexed sequence that grows and shrinks dynamically. An ordered sequence that allows efficient insertions and deletions at any location An unordered collection that rejects duplicates. A data structure that stores key/value associations.
LinkedList
HashSet
HashMap
52
FaaDoOEngineers.com
Description
An ordered list of elements that may consist of duplicate elements.
A group of objects with no duplication. A sorted group of objects with no duplication. Provides an abstract mechanism for visiting elements in an arbitrary container Provides an object that maps keys to values.
53
FaaDoOEngineers.com
54
FaaDoOEngineers.com
Set
List
HashMap
TreeMap
HashSet SortedSet
ArrayList
LinkedList
Interface Class
TreeSet
55 FaaDoOEngineers.com
The classes of the java.util package were updated to support the concept of collection framework. These classes are referred to as the legacy classes. The various legacy classes defined by the java.util package are: Vector Stack Hashtable Properties
56
FaaDoOEngineers.com
When you write a Java program, you create many classes. You can organize these classes by creating your own packages.
The packages that you create are called user-defined packages. A user-defined package contains one or more classes that can be imported in a Java program.
57
FaaDoOEngineers.com
Creating a user-defined package package <package_name> // Class definition package land.vehicle; public class Car {
String brand;
String color; int wheels; }
58
FaaDoOEngineers.com
59
FaaDoOEngineers.com
60
FaaDoOEngineers.com
CLASSPATH Variable
The CLASSPATH variable points to the directories in which the classes that are available to import are present. CLASSPATH enables you to put the class files in various directories and notifies the JDK tools of the region of these classes.
61
FaaDoOEngineers.com
Summary
Packages enable you to organize the class files provide by Java. The various built-in packages of the Java programming language are: java.lang java.util java.io java.applet java.awt java.net The packages created by users are called user-defined packages. The import statement with the package name enables you to inform the compiler about the region of classes. The java.lang package provides a number of classes and interfaces that are fundamental to Java programming.
62
FaaDoOEngineers.com
Summary (Continued)
Some of the classes defined in the java.lang package are : The various built-in packages of the Java programming language are: Object Class System Wrapper Character Integer Math String The java.util package provides various utility classes and interfaces that support date/calendar operations, String manipulation.
63
FaaDoOEngineers.com
Summary (Continued)
Some of the classes defined in the java.util package are : Date Calendar GregorianCalendar Random ArrayList LinkedList HashSet TreeSet The various interfaces defined in the java.util package are: Collection List Set SortedSet
FaaDoOEngineers.com
64
Group A
a. Character class b. Integer class c. Math class d. String class e. StringBuffer class
65 FaaDoOEngineers.com
Group B
i. Collection of useful numeric constants.
ii. Supports operations on strings of characters. iii. Used for manipulating strings. iv. Wrapper class for the primitive data type char. v. Wrapper class for the primitive data type int.