Inheritance in Java Mechanism of Deriving New Class

Inheritance in Java Mechanism of Deriving New Class
Slide Note
Embed
Share

Inheritance in Java allows the creation of new classes based on existing ones. The process involves deriving new classes from old classes, where the old class serves as the base, super, or parent class, and the new class as the derived, subclass, or child class. Types of inheritance include Single Inheritance, Multilevel Inheritance, Multiple Inheritance, and Hierarchical Inheritance. The mechanism involves constructing classes through subclass constructors using the super keyword to invoke superclass methods. However, Multiple Inheritance can lead to complexities due to dependencies on multiple base classes.

  • Java
  • Inheritance
  • Programming
  • Class
  • Mechanism

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. JavaScript: Control Statements

  2. CONTROL STRUCTURES JavaScript provides three selection structures. The if statement either performs (selects) an action if a condition is true or skips the action if the condition is false. Called a single-selection structure because it selects or ignores a single action or group of actions. The if else statement performs an action if a condition is true and performs a different action if the condition is false. Double-selection structure because it selects between two different actions or group of actions. The switch statement performs one of many different actions, depending on the value of an expression. Multiple-selection structure because it selects among many different actions or groups of actions.

  3. CONTROL STRUCTURES CONT: JavaScript provides four repetition statements, namely, while, do while, for and for in. Keywords cannot be used as identifiers (e.g., for variable names).

  4. JavaScript keywords break delete for new throw void Keywords that are reserved but not used by JavaScript abstract boolean const debugger extends final import int package private static super volatile case do function null true while catch else if return try with continue false in switch typeof default finally instanceof this var byte double float interface protected synchronized throws char enum goto long public class export implements native short transient

  5. IF...ELSE SELECTION STATEMENT Conditional operator (?:) Closely related to the if else statement JavaScript s only ternary operator it takes three operands The operands together with the ?: operator form a conditional expression The first operand is a boolean expression The second is the value for the conditional expression if the boolean expression evaluates to true Third is the value for the conditional expression if the boolean expression evaluates to false

  6. IF...ELSE SELECTION STATEMENT (CONT.) Nested if else statements Test for multiple cases by placing if else statements inside other if else structures The JavaScript interpreter always associates an else with the previous if, unless told to do otherwise by the placement of braces ({}) The if selection statement expects only one statement in its body To include several statements, enclose the statements in braces ({ and }) A set of statements contained within a pair of braces is called a block

  7. IF...ELSE SELECTION STATEMENT (CONT.) A logic error has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but the program produces incorrect results.

  8. WHILE REPETITION STATEMENT while Allows the programmer to specify that an action is to be repeated while some condition remains true The body of a loop may be a single statement or a block Eventually, the condition becomes false and repetition terminates

  9. FORMULATING ALGORITHMS: COUNTER-CONTROLLED REPETITION Counter-controlled repetition Often called definite repetition, because the number of repetitions is known before the loop begins executing A total is a variable in which a script accumulates the sum of a series of values Variables that store totals should normally be initialized to zero before they are used in a program A counter is a variable a script uses to count typically in a repetition statement

  10. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 7.7: average.html 7.7: average.html -- 6 <! <!-- -- Counter Counter- -controlled repetition to calcul controlled repetition to calculate a class average. 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Class Average Program Class Average Program</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 var var total; total; // sum of grades // sum of grades 13 var var gradeCounter gradeCounter; ; // number of grades entered // number of grades entered 14 var var grade; grade; // grade typed by user (as a string) // grade typed by user (as a string) 15 var var gradeValue; gradeValue; // grade value (converted to integer) // grade value (converted to integer) 16 var var average; average; // average of all grades // average of all grades 17 18 // Ini // Initialization Phase tialization Phase 19 total = total = 0 0; ; // clear total // clear total 20 gradeCounter = gradeCounter =1 1; // prepare to loop // prepare to loop 21 22 // Processing Phase // Processing Phase 23 while while ( gradeCounter <= gradeCounter <=10 24 { { 25 26 // pr // prompt for input and read grade from user ompt for input and read grade from user 27 grade = window.prompt( grade = window.prompt( "Enter integer grade:" 28 "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > ate a class average. -- --> > Stores the sum of grades </title> Sets total to 0 Sets gradeCounter to 1 in preparation for the loop 10 ) // loop 10 times // loop 10 times Continues the cycle until gradeCounter is greater than 10 "Enter integer grade:", , "0" "0" ); );

  11. 29 // convert grade from a string to an integer // convert grade from a string to an integer 30 gradeValue = parseInt( grade ); gradeValue = parseInt( grade ); 31 32 // add gradeValue to total // add gradeValue to total 33 total = total + gradeValue; total = total + gradeValue; 34 35 // add 1 to gradeCounter // add 1 to gradeCounter 36 gradeCounter = gradeCounter + gradeCounter = gradeCounter +1 1; 37 } } // end while // end while 38 39 // Termination Phase // Termination Phase 40 average = total / average = total / 10 10; ; // calculate the average 41 42 // display average of exam grades // display average of exam grades 43 docum document.writeln( ent.writeln( 44 "<h1>Class average is " "<h1>Class average is " + average + 45 // // -- --> > 46 </script> </script> 47 </head> </head> 48 <body> <body> 49 <p> <p>Click Refresh (or Reload) to run the script again Click Refresh (or Reload) to run the script again<p> 50 </body> </body> 51 </html> </html> Increments gradeCounter by 1 after each iteration of the loop // calculate the average + average + "</h1>" "</h1>" ); ); <p>

  12. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 7.11: analysis.html 7.11: analysis.html -- 6 <! <!-- -- Examination Examination- -results calculation. results calculation. -- 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Analysis of Examination Results Analysis of Examination Results</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 // initializing variables in declarations // initializing variables in declarations 13 v var arpasses = passes =0 0; // number of passes // number of passes 14 var varfailures = failures =0 0; // number of failures // number of failures 15 var varstudent = student =1 1; // student counter // student counter 16 var varresult; result; // one exam result // one exam result 17 18 // process 10 students; counter // process 10 students; counter- -con 19 while while( student <= ( student <=10 20 { { 21 result = window.prompt( result = window.prompt( "Enter result (1=pass,2=fail)", "0" 22 23 if if( result == "1" ) ( result == "1" ) 24 passes = passes + 1; passes = passes + 1; 25 else else 26 failures = failures + 1; failures = failures + 1; 27 28 student = student + 1; student = student + 1; 29 } } // end while // end while 30 "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > --> > </title> controlled loop trolled loop 10 ) Outer control structure "Enter result (1=pass,2=fail)", "0" ); ); Start nested control structure End nested control structure Increment for while loop

  13. 31 // termination phase // termination phase 32 document.writeln( document.writeln( "<h1>Examination Results</h1>" "<h1>Examination Results</h1>" ); 33 document.writeln( document.writeln( 34 "Passed: " "Passed: " + passes + + passes + "<br />Failed: " 35 36 if if( passes > ( passes >8 8 ) 37 document.writeln document.writeln( "<br />Raise Tuition" 38 // // -- --> > 39 </script> </script> 40 </head> </head> 41 <body> <body> 42 <p> <p>Click Refresh (or Reload) to run the script again Click Refresh (or Reload) to run the script again</p> 43 </body> </body> 44 </html> </html> ); "<br />Failed: " + failures ); + failures ); Additional control structure "<br />Raise Tuition" ); </p>

  14. ESSENTIALS OF COUNTER- CONTROLLED REPETITION Counter-controlled repetition requires name of a control variable initial value of the control variable the increment (or decrement) by which the control variable is modified each time through the loop the condition that tests for the final value of the control variable to determine whether looping should continue

  15. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 8.1: WhileCounter.html 8.1: WhileCounter.html -- 6 <! <!-- -- Counter Counter- -controlled repetition. controlled repetition. -- 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Counter Counter- -Controlled Repetition Controlled Repetition</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 var var counter = counter = 1 1; ; // initialization // initialization 13 14 w while hile ( counter <= ( counter <= 7 7 ) 15 { { 16 document.writeln( document.writeln( "<p style = 17 counter + counter + "ex "ex\ \">XHTML font size " 18 "ex</p>" "ex</p>" ); ); 19 ++counter; ++counter; // // increment increment 20 } } //end while //end while 21 // // -- --> > 22 </script> </script> 23 </head><body></body> </head><body></body> 24 </html> </html> "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > --> > </title> Initializes counter Precedes the with a \ to create an escape sequence so that it can be used in the string ) // repetition condition // repetition condition "<p style = \ \"font "font- -size: " size: " + + ">XHTML font size " + counter + + counter + Condition to be fulfilled with every iteration Incrementing statement

  16. FOR REPETITION STATEMENT for statement Specifies each of the items needed for counter-controlled repetition with a control variable Can use a block to put multiple statements into the body If the loop s condition uses a < or > instead of a <= or >=, or vice-versa, it can result in an off-by-one error for statement takes three expressions Initialization Condition Increment Expression The increment expression in the for statement acts like a stand-alone statement at the end of the body of the for statement Place only expressions involving the control variable in the initialization and increment sections of a for statement

  17. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 8.2: ForCounter.html 8.2: ForCounter.html -- 6 <! <!-- -- Counter Counter- -controlled repetition with t controlled repetition with the for statement. 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Counter Counter- -Controlled Repetition Controlled Repetition</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 // Initialization, repetition conditi // Initialization, repetition condition and 13 // incrementing are all included in the for // incrementing are all included in the for 14 // // statement header. statement header. 15 for for ( ( var var counter = counter = 1 1; counter <= 16 document.writeln( document.writeln( "<p style = 17 coun counter + ter + "ex "ex\ \">XHTML font size " 18 "ex</p>" "ex</p>" ); ); 19 // // -- --> > 20 </script> </script> 21 </head><body></body> </head><body></body> 22 </html> </html> "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > he for statement. -- --> > </title> Initial value of the control variable on and Condition to test whether looping should continue ; counter <= 7 7; ++counter ) ; ++counter ) "<p style = \ \"font "font- -size: " size: " + + ">XHTML font size " + counter + + counter + Increment to occur after each iteration of the loop Statement inside the for loop

  18. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 8.6: Interest.html 8.6: Interest.html -- 6 <! <!-- -- Compound interest calculation with a f Compound interest calculation with a for loop. 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Calculating Compound Interest Calculating Compound Interest</title> 10 <style type = <style type = "text/css" "text/css"> > 11 table table { { width: width: 100% 100% } 12 th th { { text text- -align: align: left 13 </style> </style> 14 <script type = <script type = "text/javascript" "text/javascript"> > 15 <! <!-- -- 16 var var amount; amount; // current amount of money // current amount of money 17 var var principal = principal = 1000.0 1000.0; 18 var var rate = rate = .05 .05; ; // interest rate // interest rate 19 20 docu document.writeln( ment.writeln( 21 "<table border = "<table border = \ \"1 22 document.writeln( document.writeln( 23 "<caption>Calculating Compound Interest</caption>" "<caption>Calculating Compound Interest</caption>" ); 24 document.writeln( document.writeln( 25 "<thead><tr><th>Year</th "<thead><tr><th>Year</th>" 26 document.writeln( document.writeln( 27 "<th>Amount on deposit</th>" "<th>Amount on deposit</th>" ); 28 document.writeln( document.writeln( "</tr></thead><tbody>" "</tr></thead><tbody>" ); 29 "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > or loop. -- --> > </title> } left } } ; // principal amount // principal amount "1\ \">" ">" ); ); // begin the table // begin the table ); >" ); ); // year column heading // year column heading ); // amount column heading // amount column heading );

  19. 30 // output a table row for each year // output a table row for each year 31 for for ( ( var var year = year = 1 1; year <= 32 { { 33 amount = principal * Math.pow( amount = principal * Math.pow( 1.0 34 document.writeln( document.writeln( "<tr><td>" 35 "</td><td>" "</td><td>" + amount.toFixed( 36 "</td></tr>" "</td></tr>" ); 37 } } //end for //end for 38 39 document.writeln( document.writeln( "</tbody></table>" 40 // // -- --> > 41 </script> </script> 42 </head><body></body> </head><body></body> 43 </html> </html> ; year <= 10 10; ++year ) ; ++year ) Control variable year begins with a value of 1 1.0 + rate, year ); + rate, year ); "<tr><td>" + year + + year + + amount.toFixed(2 2) + ) + ); Continue to execute the loop while year is less than or equal to 10 "</tbody></table>" ); ); After each loop iteration, increase the value of year by 1

  20. SWITCH MULTIPLE-SELECTION STATEMENT switch statement Consists of a series of case labels and an optional default case When control reaches a switch statement The script evaluates the controlling expression in the parentheses Compares this value with the value in each of the case labels If the comparison evaluates to true, the statements after the case label are executed in order until a break statement is reached The break statement is used as the last statement in each case to exit the switch statement immediately The default case allows you to specify a set of statements to execute if no other case is satisfied Usually the last case in the switch statement

  21. SWITCH MULTIPLE-SELECTION STATEMENT (CONT.) Each case can have multiple actions (statements) Braces are not required around multiple actions in a case of a switch The break statement is not required for the last case because program control automatically continues with the next statement after the switch Having several case labels listed together (e.g., case 1: case 2: with no statements between the cases) executes the same set of actions for each case

  22. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 8.7: SwitchTest.html 8.7: SwitchTest.html -- 6 <! <!-- -- Using the switch multiple Using the switch multiple- -selection 7 <html xmlns = <html xmlns ="http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Switching between XHTML List Formats Switching between XHTML List Formats</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 var var choice; choice; // user s choice // user s choice 13 var var startTag; startTag; // starting list item tag // starting list item tag 14 var var endTag; endTag; // ending list item tag // ending list item tag 15 var var validInput = validInput = true true; 16 var var listType; listType; // type of list as a string // type of list as a string 17 18 choice = window. choice = window.prompt( prompt( "Select a list style: 19 "1 (numbered), 2 (lettered), 3 (roman)" "1 (numbered), 2 (lettered), 3 (roman)", 20 21 switch switch ( choice ) ( choice ) 22 { { 23 case case "1" "1": : 24 startTag = startTag = "<ol>" "<ol>"; ; 25 endTag = endTag = "</ol>" "</ol>"; ; 26 listType = listType = "<h1>Numbered List</h1>" "<h1>Numbered List</h1>"; ; 27 break break; ; 28 case case "2" "2": : 29 startTag = startTag = "<ol style = "<ol style = \ \"list 30 endTag = endTag = "</ol>" "</ol>"; ; 31 listType = listType = "<h1>Lettered List</h1>" "<h1>Lettered List</h1>"; ; 32 break break; ; "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > selection statement. statement. -- --> > </title> ; // indicates if input is valid // indicates if input is valid Beginning of switch statement "Select a list style:\ \n" n" + + , "1" "1" ); ); Beginning of statements to be executed if choiceequals 1 Beginning of statements to be executed if choiceequals 2 Statements Break out of switch statement "list- -style style- -type: upper type: upper- -alpha alpha\ \">" ">"; ;

  23. 33 case 34 startTag = 35 endTag = 36 listType = 37 break 38 default 39 validInput = 40 } } //end switch //end switch 41 42 if if ( validInput == ( validInput == true 43 { { 44 document.writeln( listType + startTag ); document.writeln( listType + startTag ); 45 46 for for ( ( var var i = i = 1 1; i <= 47 document.writeln( document.writeln( "<li>List item " 48 49 document.writeln( endTag ); document.writeln( endTag ); 50 } } //end if //end if 51 else else 52 document.writeln( document.writeln( "Invalid choice: " 53 // // -- --> > 54 </script> </script> 55 </head> </head> 56 <body> <body> 57 <p> <p>Click Refresh (or Reload) to run the script again Click Refresh (or Reload) to run the script again</p> 58 </body> </body> 59 </html> </html> case "3" "3": : startTag = "<ol style = "<ol style = \ \"list "list- -style style- -type: upper type: upper- -roman roman\ \">" ">"; ; endTag = "</ol>" "</ol>"; ; listType = "<h1>Roman Numbered List</h1>" "<h1>Roman Numbered List</h1>"; ; break; ; default: : Beginning of statements to be executed if choice is anything other than 1 , 2 or 3 validInput = false false; ; true ) ) No break is necessary, since we ve come to the end of the switch anyway ; i <= 3 3; ++i ) ; ++i ) "<li>List item " + i + + i + "</li>" "</li>" ); ); "Invalid choice: " + cho + choice ); ice ); </p>

  24. DOWHILE REPETITION STATEMENT do while statement tests the loop-continuation condition after the loop body executes The loop body always executes at least once

  25. 1 <?xml version = <?xml version = "1.0" 2 <!DOCTYPE html PUBLIC <!DOCTYPE html PUBLIC " "- -//W3C//DTD XHTML 1.0 Strict//EN" //W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1 "http://www.w3.org/TR/xhtml1/DTD/xhtml1- -strict.dtd" 4 5 <! <!-- -- Fig. Fig. 8.9: DoWhileTest.html 8.9: DoWhileTest.html -- 6 <! <!-- -- Using the do...while repetition sta Using the do...while repetition statement. 7 <html xmlns = <html xmlns = "http://www.w3.org/1999/xhtml" "http://www.w3.org/1999/xhtml"> > 8 <head> <head> 9 <title> <title>Using the do...while Repetition Statement Using the do...while Repetition Statement</title> 10 <script type = <script type = "text/javascript" "text/javascript"> > 11 <! <!-- -- 12 var var counter = counter = 1 1; ; 13 14 do do { { 15 document.writeln( document.writeln( "<h" 16 "an h" "an h" + counter + + counter + " level head" 17 counter + counter + ">" ">" ); ); 18 ++counter; ++counter; 19 } } while while ( coun ( counter <= ter <= 6 6 ); 20 // // -- --> > 21 </script> </script> 22 23 </head><body></body> </head><body></body> 24 </html> </html> "1.0" encoding = encoding = "utf "utf- -8" 8"?> ?> strict.dtd"> > --> > tement. -- --> > </title> Perform the following actions "<h" + counter + + counter + ">This is " ">This is " + + " level head" + + "</h" "</h" + + Then check to see if counter <= 6. If it is, iterate through the loop again. );

More Related Content