Java Intro to Inheritance

Add to Favourites
Post to:
Comments
Presentation Transcript Presentation Transcript

Slide 1 : Part 1 Introduction to Enterprise Application Development using Java

Course Objectives (1/3) : 2 Course Objectives (1/3) Trainees should be able to explain the role of Java EE in developing Enterprise Applications Trainees should be able to explain the features of Object Oriented Programming Trainees should be able to design the classes required for solving a given problem Trainees should be able to write classes in Java implementing the features of OOP Trainees should be able to write Java programs that can handle the exceptions

Course Objectives (2/3) : Course Objectives (2/3) Trainees should be able to identify and use the classes from the core Java library for solving a given problem Trainees should be able to select the appropriate class from the Collection Framework for solving a given problem Trainees should be able to create Java Beans and explain their use Trainees should be able to explain the use of Reflection API 3

Course Objectives (3/3) : Course Objectives (3/3) Trainees should be able to explain and use the basic Annotations in Java Trainees should be able to explain the basic measurement parameters of Project Quality Metrics (PQM) Trainees should be able to use Quality Analyst for Java (QA4J) for capturing the basic PQM Trainees should be able to write, compile, run and debug Java programs using the Eclipse IDE 4

Course Pre-requisites : Course Pre-requisites The participants should have knowledge of C as a programming language 5

Course Concept Map : Course Concept Map 6

Relevance of Java in the industry : 7 Relevance of Java in the industry Java applications have platform interoperability Platform independent byte code JVM is platform dependant, it has open specification Most of the Java technologies are Open Source Java is used for developing: Desktop applications Web applications Mobile and Embedded applications Network computing applications Computer games

Expectations : Expectations To Understand the different layers of Java EA Gain knowledge on using and exploring Java libraries To convert a given class to a Java program using Java programming constructs To learn Java coding standards and practices 8

Course Agenda (1/2) : 9 Course Agenda (1/2) Part 1 Introduction to Enterprise Applications Introduction to Java EE Object Oriented Programming concepts Overview of Java language fundamentals Classes and Objects Part 2 Inheritance Method Overriding and Dynamic Binding Abstract Classes Interfaces Packages

Course Agenda (2/2) : 10 Course Agenda (2/2) Part 3 Exception Handling Java Library Part 4 Java Beans Reflection API Annotations Collection Classes QA4J

Session Plan Day1 : Session Plan Day1 Introduction to Enterprise Applications Introduction to Java EE Object Oriented Programming concepts Overview of Java language fundamentals Classes and Objects 11

Slide 12 : Enterprise Applications

Enterprise Application (EA) : Enterprise Application (EA) EA is a software system built to solve the business problems of an enterprise Banking Insurance Healthcare It is deployed on server(s) to serve large number of users simultaneously 13

Enterprise Application – The big picture : Enterprise Application – The big picture 14

Example for EA : Example for EA 15 Request Response

Evolution of an Enterprise Application : 16 Dumb Terminal Data Access Presentation Business Logic Smart Terminal Presentation Business Logic Single Tier Advantages Easy to manage - client side management is NOT required Data consistency is easy and simple to achieve Disadvantages Difficult to maintain and reuse Two Tier Example Order fulfillment Application accessing Customer Information Advantages DB product independence Disadvantages Difficult to maintain and update Three Tier: Example DART Application Advantages Business logic can change more easily Disadvantages Complexity introduced in the middle tier N-Tier Example- Wep Apps on Sparsh, like Leave System / Performagic Advantages More loosely coupled More reusable Zero client management Disadvantages Complexity in the middle tier Evolution of an Enterprise Application Database Thin Client

Slide 17 : Object Oriented Programming – A Quick Revision

Object Oriented Programming : 18 Object Oriented Programming Object Oriented Programming The entire program is visualized as a number of objects interacting with each other An object is a self-contained entity that contains attributes (data) and behaviors (methods) For using an object one needs to just invoke a method on the object

What is a Class? (1/2) : 19 What is a Class? (1/2) A Class Is a blue print used to create objects Is a software template that defines the methods and data to be included in a particular kind of Object Contains state and behavior Class Diagram is pictorial representation of a class. It is a part of UML diagram. Class name Property Behavior

What is a Class? (2/2) : 20 What is a Class? (2/2) State (Member Data) Data declared inside the class Usually not exposed to external world Behavior (Member Methods) Functions defined inside the class Behavior exhibited by the class Usually exposed to external world

Can you answer these Questions? : Can you answer these Questions? 21 Penguin is an aquatic flightless bird. They have round eyes and orange colored beak. Large penguins are 1.1 meters in height and can dive deep into the sea. Small penguins are 40 cm tall and cannot dive deep into the sea. Penguins can walk on ice with great ease. To catch fish penguins have to dive into the sea and they flap their wings to swim in the water. Life span of penguin is from 20-25 years. Behaviors: Properties: Q2. List out the behaviors and properties of Penguin from the above statement: Q1. If you have to represent the above detail in an object oriented programming language, what could be a possible class name? Answer: Penguin Penguin can walk, Penguin can flap its wings, Dive into the sea, Catch fish Beak color, Shape of eyes, Height, Age

Object Oriented Characteristics : 22 Object Oriented Characteristics Abstraction The process of exposing the relevant details and hiding the irrelevant details Member data is accessed using a method and not directly Encapsulation Process of hiding the members from the external world Data members are usually made private preventing any direct access to them Polymorphism Refers to an object’s ability to behave differently depending on the context Method Overloading is a way to achieve polymorphism Inheritance A technique to create specialized classes from a general class Defines a class hierarchy

Can you answer these questions? : Can you answer these questions? 23 Yes Inheritance Abstraction Encapsulation Polymorphism Car and Bus belong to the same category Automobile Can Automobile become a parent class For Bus and Car? (Yes/No)

Relationships : 24 Relationships Different types of relationships can exist between classes Identifying relationships helps design the classes better Analogous to relations between entities in RDBMS design There are 3 types of relationships Has-A (or Part-Of) Car has an Engine Uses-A Driver uses a Car Is-A (or Kind-Of) Car is a FourWheeler

Object Oriented Programming – A Quick Revision(1/2) : Object Oriented Programming – A Quick Revision(1/2) 25 Write the skeleton class code for the following class diagram

Object Oriented Programming – A Quick Revision (2/2) : Object Oriented Programming – A Quick Revision (2/2) 26 Q. ‘name’ member variable cannot be accessed from outside the Instruments class because the access specified is private. What is this concept called in Object Oriented Concepts? Q. Which Object Oriented concept is shown by the (a) playSound() method (b) Relationship between StringInstrument class and Instruments class [Is-A, Has-A or Uses-A]? (c) Relationship between Musician class and Instruments class Ans. A. Polymorphism (Overloading) B. Is-A C. Uses-A Q. List the class member variable(s) of stringInstrument class. Ans. name, sound[ ] Ans. Encapsulation

Slide 27 : Java Programming

Salient Features of Java : 28 Salient Features of Java Object Oriented Simpler language Compared to earlier OO languages like C++, it is simple Architecture Neutral/Portable Example: Java code compiled on Windows can be run on Unix without recompilation Write Once, Run Anywhere Secure Built -in security features like absence of pointers

Java Architecture : Java Architecture

Slide 30 : 30 John and his teammate, Paul are working on a project. John is working on a Windows machine where as Paul is working on a Mac machine. To increase the interoperability of the application they chose Java as their programming language. Did they make a correct choice? (Yes/No) Yes John creates a .java file and sends it to Paul. Is this the correct way to exploit interoperability feature given by Java? (Yes/No) No John then uses java compiler, ______[1], to generate ______ [2] from .java file. The extension of the new file is _____ [3] Now John sends this file to Paul. Is this the correct way to exploit interoperability feature given by Java? (Yes/No) 1. javac 2. bytecode 3. .class Yes Paul uses interpreter to use the byte code. The interpreters are platform ______ independent/dependant? Dependant Can you answer these questions?

Java Fundamentals – A Quick Recap (1/2) : Java Fundamentals – A Quick Recap (1/2) Primitive Data Types Comments Variables Local Variables Instance Variables Typecasting of Primitive Data Types Operators Control Statements 31

Java Fundamentals – A Quick Recap (2/2) : Java Fundamentals – A Quick Recap (2/2) Classes Data Members Methods Objects Constructors 32

Can you answer these questions? : Can you answer these questions? 33 Object of a class Automobile is created as follows Automobile automobile = new Automobile(); Answer the following questions (TRUE or FALSE) Automobile() is the default constructor of Automobile class. automobile is a reference of Automobile class. Automobile() initializes all the member variables to its initial values even if the user has not written any code for it. Memory is allocated by the keyword ‘new’ . automobile can never have the value of null. TRUE FALSE TRUE TRUE TRUE

Type Casting (1/2) : Type Casting (1/2) 34 The following snippet demonstrates the concept of type casting. int number=10; float weight=number; The above snippet is an example of ________[implicit/explicit] type conversion. Fill the boxes (marked as 1,2,3) in the following diagram with the data types in the order of implicit type conversion i.e a data type can be type casted along the flow. Ans. Implicit 1 short 3 2 float Ans. 1:byte ; 2:int ; 3:double Where does the boolean data type fits in the above sequence ? Ans. It cannot be type casted

Type Casting (2/2) : Type Casting (2/2) 35 System.out.println(“ \n\tValue of integer:" + num); System.out.println(“ \n\tValue of double:" + average); System.out.println(“ \n\n "); } } average=num; Output: ERROR: possible loss of precision found : double required: int num=average; num = (int)average; public class TypeConversionDemo{ public static void main(String args[]) { int num = 10; double average = 1.9;

Can you answer these questions? : Can you answer these questions? 36 public class Sample { public static void main(String args[]) { int count; int data= 1223; System.out.println(“Demo on local variable."); } } Output: ERROR: variable count might not have been initialized System.out.println(“Count=“+count); count=data;

Arrays in Java : 37 Arrays in Java Are created dynamically The operator new is used for dynamic memory allocation The following statement creates an array of 5 integers new int[5] The above statement returns a reference to the newly created array References in Java are very similar to pointers in C

Reference variables in Java (1/4) : 38 Reference variables in Java (1/4) Reference variables are used in Java to store the references of the objects created by the operator new Any one of the following syntax can be used to create a reference to an int array The reference x can be used for referring to any int array int x[]; int [] x; //Declare a reference to an int array int [] x; //Create a new int array and make x refer to it x = new int[5];

Reference variables in Java (2/4) : 39 Reference variables in Java (2/4) The following statement also creates a new int array and assigns its reference to x int [] x = new int[5]; In simple terms, reference can be seen as the name of the array

Reference variables in Java (3/4) : 40 Reference variables in Java (3/4) Even though we can think of Java references as C pointers, their usages are different

Reference variables in Java (4/4) : 41 Reference variables in Java (4/4) A reference type can be assigned ‘null’ to show that it is not referring to any object ‘null’ is a keyword in Java int [] x = null;

Initializing an array in Java : 42 Initializing an array in Java An array can be initialized while it is created as follows: int [] x = {1, 2, 3, 4}; char [] c = {‘a’, ‘b’, ‘c’};

The length of an array : 43 The length of an array Unlike C, Java checks the boundary of an array while accessing an element in it Java will not allow the programmer to exceed its boundary If x is a reference to an array, x.length will give you the length of the array The for loops can be set up as follows for(int i = 0; i < x.length; ++i){ x[i] = 5; }

Can you answer these questions? : Can you answer these questions? 44 public class PrimitiveArrayDemo { public static void main(String [] args) { int [] array = new int[5]; //Line 4 for(int i=0; i < array.length; i++) { System.out.println("\n\t" + array[i]); } } } What does Line 4 do? Output: 0 0 0 0 0 Why are the array elements initialized to 0? Allocates memory to array elements. As soon as the array is created in heap, JVM initializes the array members to their default values.

Multidimensional Arrays : 45 Multidimensional Arrays Multidimensional arrays are array of arrays. To declare a multidimensional array, specify each additional index using another set of square brackets. int [][] x; //x is a reference to an array of int arrays x = new int[3][4]; /* * Create 3 new int arrays, each having 4 elements * x[0] refers to the first int array, x[1] to the second etc * x[0][0] is the first element of the first array * x.length will be 3 * x[0].length, x[1].length and x[2].length will be 4 */

Can you answer these questions? : Can you answer these questions? 46 public class PrimitiveArrayDemo { public static void main(String [] args) { int [][] matrix = new int[3][3]; for(int i = 0; i < matrix.length; i++) { System.out.println("\n"); for(int j = 0; j < matrix[i].length; j++){ System.out.print("\t" + matrix[i][j]); } } } } Output:

Sharpen your skills : Sharpen your skills 47 public class AdvancedArrayDemo { public static void main(String [] args) { int matrix [][] = new int[3][]; //Line 3 for(int i = 0; i < matrix.length; i++) { matrix[i] = new int[i+1]; //Line 5 System.out.println("\n"); for(int j = 0; j < matrix[i].length; j++) { matrix[i][j] = i + j; System.out.print("\t" + matrix[i][j]); } } System.out.println("\n\n\t Length of Matrix: " + matrix.length); } } Output: What does Line 3 do? Allocates memory to one row at a time. What does Line5 do? Allocate memory to Corresponding column

Array of Objects (1/2) : null null null Array of Objects (1/2) 48 Collection of similar type of objects Creation of array of objects in Java: Step1: Allocate the memory for Array Bus [] arrayBus = new Bus[3]; //array of Objects of size 3 arrayBus[0] arrayBus[1] arrayBus[2]

Array of Objects (2/2) : Array of Objects (2/2) 49 public class Trainee { private String empName; public Trainee(String empName) { this.empName = empName; } public void display() { System.out.println("\n\t" + empName); } public static void main(String [] args) { Trainee [] objTrainee = new Trainee[2]; //Line 12 objTrainee[0] = new Trainee(“John"); //Line 13 objTrainee[1] = new Trainee(“Paul"); //Line 14 for(int i = 0;i < objTrainee.length;i = i + 1) { objTrainee[i].display(); } } } What does Line 12 do? Allocates memory to one row at a time. What does Line13,14,do? Allocates memory to the objects in the array Output:

Constructors – A recap : Constructors – A recap A constructor (default or user defined) is a special method that is called to create a new object A constructor method will have the same name as that of the class will not have any return type, not even void If no user defined constructor is provided for a class, the implicit constructor initializes the member variables to its default values numeric data types are set to 0 char data types are set to null character(‘\0’) boolean data types are set to false reference variables are set to null 50

Can you answer this question? : Can you answer this question? 51 public class Automobile { int regNumber; String name; boolean status; public static void main (String [] args) { Automobile automobile= new Automobile(); System.out.println(automobile. regNumber); System.out.println(automobile.name); System.out.println(automobile.status); } } Output: 0 null false

Constructor and Access Specifiers (1/3) : Constructor and Access Specifiers (1/3) 52 public class Automobile { Automobile() { System.out.print("Inside Automobile class constructor"); } public static void main(String [] args) { Automobile automobile = new Automobile(); } } public private Output:

Constructor and Access Specifiers (2/3) : 53 class Automobile { Automobile() { System.out.print(“Inside Automobile class constructor"); } } public class Car { public static void main(String [] args) { Automobile automobile = new Automobile(); } } public private Output: Error: Automobile() has private access in Automobile Constructor and Access Specifiers (2/3)

Constructor and Access Specifiers (3/3) : 54 class Automobile { Automobile() { System.out.print("Inside Automobile class constructor"); } } public class Car extends Automobile { public static void main(String [] args) { Automobile car = new Automobile(); } } public Output: private Error: Automobile() has private access in Automobile Constructor and Access Specifiers (3/3)

Method Overloading – A Quick Recap (1/2) : Method Overloading – A Quick Recap (1/2) Two or more methods in a Java class can have the same name, if their argument lists are different Argument list could differ in No of parameters Data type of parameters Sequence of parameters This feature is known as Method Overloading or Static Polymorphism 55

Method Overloading – A Quick Recap (2/2) : Method Overloading – A Quick Recap (2/2) 56 class DisplayUtility { public void display(int num) { System.out.println("\n\t Integer value: " + num); } public void display(char value) { System.out.println("\n\t Character value: " + value); } public void display(double data) { System.out.println("\n\t Double value: " + data); } } public class MethodOverloadingDemo { public static void main(String [] args) { DisplayUtility object = new DisplayUtility(); object.display(123); object.display('A'); object.display(123.23); } } Will call display(int) Will call display(char) Will call display(double) object.display("JEE Batch"); ERROR: Cannot find symbol

Ambiguous Error : Ambiguous Error 57 public class Adder { static void add(int var1, double var2) { System.out.println("Inside add() which returns double"); System.out.println(var1 + var2); } static void add(double var1, int var2) { System.out.println("Inside add() which returns float"); System.out.println(var1+var2); } public static void main(String [] args) { add(2,3); } } Output: Error: reference to add is ambiguous, both method add(int,double) in Addition and method add(double,int) in Addition match add(2,3)

Constructor Overloading : Constructor Overloading 58

Java Coding Standards (1/3) : Java Coding Standards (1/3) 59 class School{int x=1000;String name;School(String name) {this.name=name;} void enrolStudent(Student obj2) {obj2.x=this.x+1;}} public class Student {String name;int x,standard; Student(String name,int standard){this.name=name;this.standard=standard; } public static void main (String args[]){ School obj1= new School ("IEMS");Student obj2= new Student(“Paul",12); obj1.enrolStudent(obj2);System.out.println("Roll number="+ obj2.x); }} Find the output of the following code:

Java Coding Standards (2/2) : Java Coding Standards (2/2) Try to find the output by doing the following changes Replace variable x with rollNo Replace variable name in class School with schoolName Replace variable name in class Student with studentName Replace obj1 with school Replace obj2 with student Give proper commenting to the code Provide proper indentation to the code 60

Summary: : 61 Summary: Introduction to Enterprise Applications Evolution of EA Introduction to Java EE Object Oriented Programming Concepts Class and Class diagrams Overview of Java language fundamentals Local variables Creating references and objects 1D and 2D Arrays Method overloading Constructors Java Coding Standards

Slide 62 : Contracts (Contd…)

Slide 63 : 63 63 Thank You

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect