java review i. what is a stream? what is a byte stream? what is a character stream? what types of...

69
Java Review I

Upload: lora-poole

Post on 02-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Java Review I

What is a stream? What is a byte stream? what is a character stream?

What types of files does a program will typically handle?

What is a stream? What is a byte stream? what is a character stream?

What types of files does a program will typically handle?

A stream is an object that allows for the flow of data between your program and some I/O device or some file

Byte stream inputs and outputs 8-bit bytes

ASCII text files and binary files

How to write to a text file?

How to write to a text file?

Use PrintWriter class

Import.import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;

How to write to a text file?

Use PrintWriter class

Import.import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;

PrintWriter outputStreamName;outputStreamName = new PrintWriter( new FileOutputStream (FileName));

PrintWriter outputStreamName;outputStreamName = new PrintWriter(FileName);

Create a PrintWriter object

How to write to a text file?

Use PrintWriter class

Import.import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;

PrintWriter outputStreamName;outputStreamName = new PrintWriter( new FileOutputStream (FileName));

PrintWriter outputStreamName;outputStreamName = new PrintWriter(FileName);

Create a PrintWriter object

If the file is open successfully, the methods, print(…)and println(…),can be used to output text information into the file.

How to write to a text file?

Use PrintWriter class

Import.import java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;

PrintWriter outputStreamName;outputStreamName = new PrintWriter( new FileOutputStream (FileName));

PrintWriter outputStreamName;outputStreamName = new PrintWriter(FileName);

Create a PrintWriter object

If the file is open successfully, the methods, print(…)and println(…),can be used to output text information into the file.

Remember to put th

is in an Exception handling block

How to close a file?

writer_or_reader_obj.close();

Exampleimport java.io.PrintWriter;import java.io.FileOutputStream;import java.io.FileNotFoundException;

public class WriteTextFileDemo { public static void main (String[] args) { PrintWriter outputStreamName; String Filename = "test.txt"; try { outputStreamName = new PrintWriter(new FileOutputStream (Filename)); outputStreamName.println("test text file!"); outputStreamName.println("succeeded!"); // Do other fancy output … outputStreamName.close(); } catch(FileNotFoundException e) { System.out.println("File "+Filename+"cannot be found."); System.exit(0); } }}

How to append to an existing text file?

Flushing to a text file

In some situations, you do not want to overwrite what has been recorded in the existing file. The new output will be appended to the previous output. To address that, we need to use the FileWriter and BufferedWriter classes

PrintWriter outputStreamName = new PrintWriter( new FileOutputStream(FileName, true));

Use the following format to open the file to force flushing the buffer to the file!

PrintWriter outputStreamName = new PrintWriter( new BufferedWriter( new FileWriter(FileName, true)));

Note that the slides from the textbook were the 4th edition, which are not correct about the appending to the text file!!

How to read from a text file using a Scanner object?

How to read from a text file using a Scanner object?

Using Scannar class

Scanner StreamObject = new Scanner(new FileInputStream(FileName));

Import.import java.util.Scannar;import java.io.FileInputStream;import java.io.FileNotFoundException;

Create a Scanner object

How to read from a text file using a Scanner object?

Using Scannar class

Scanner StreamObject = new Scanner(new FileInputStream(FileName));

Import.import java.util.Scanner;import java.io.FileInputStream;import java.io.FileNotFoundException;

Create a Scanner object

If successful, use the following to read from filenextInt()nextShort()nextLong()nextByte() nextDouble()nextLine()……

How to determine the end of the file using a Scanner object?

How to determine the end of the file using a Scanner object?

hasNextInt()hasNextShort()hasNextLong()hasNextByte() hasNextDouble()hasNextLine()……

Remember to put file I/O in an Exception handling block.

Do not forget to close the file!

Example

Read one integer from the file at a time. Integers are separated by empty space.

Read one line of string.

Testing the end of the text fileExample: adding line number to the file

Given input file:

The output file will be

How to read from a text file using a BufferedReader object?

How to read from a text file using a BufferedReader object?

import java.io.BufferedReader;import java.io.FileReader;import java.io.FileNotFoundException;import java.io.IOException;

BufferedReader readerObject;readerObject = new BufferedReader(new FileReader(FileName));

How to read from a text file using a BufferedReader object?

import java.io.BufferedReader;import java.io.FileReader;import java.io.FileNotFoundException;import java.io.IOException;

BufferedReader readerObject;readerObject = new BufferedReader(new FileReader(FileName));

After the file is successfully opened, the readLine() and read() methods can be used to read from the file.

• The readLine method is the same method used to read from the keyboard, but in this case it would read from a file.

• The read method reads a single character, and returns a value (of type int) that corresponds to the character read.

How to determine the end of a text file using a BufferedReader object?

How to determine the end of a text file using a BufferedReader object?

• When using the readLine() method, it will return null if reaching or trying to read beyond the end of the file.

• When using the read() method, it will return -1 if reaching or trying to read beyond the end of the file.

Remember to put file I/O in an Exception handling block.

Do not forget to close the file!

Using BufferReader class to read from a text file

Given input file

Output will be

EARLIER CONTENT

Class in Java• Classes are the most important language feature that make object-

oriented programming (OOP) possible.– Combination of both attributes and behaviors– Can be used to generate many objects with the same behaviors– Information hidden and encapsulation

• Programming in Java consists of defining a number of classes– Every program is a class– All helping software consists of classes– All programmer-defined types are classes

• Classes are central to Java• All classes inherit from the Object class

How to define a class in Java?

How did we do that in C++?

class class_name{public: //member func.…private: // attribute list};

How should we do that in Java?

public class class_name{public member func1public member func2…protected member var1private member var2…}

No semi-column!

How to define a class in Java?

How did we do that in C++?

class class_name{public: //member func.…private: // attribute list};

How should we do that in Java?

public class class_name{public member func1public member func2…protected member var1private member var2…}

No semi-column!

How do different access modifiers affect the access of members?

One Examplepackage One;

public class DayOfYear{ public DayOfYear() {month = day = 1;} public DayOfYear(int monthVal, int dayVal) {month = monthVal; day = dayVal; } void input(int m, int d) {month = m; day = d;} public void output() { System.out.println(“month=“ +month+ ”, day=“ +day); } private int month, day;}

package Two;

public class TestDemo{ public static void main() {

DayOfYear date1(3,18), examdate();

System.out.print(“date1.month=“ +date1.month+ ”, date1.day=“

+date1.day); examdate.input(3,20); examdate.output(); }}

What will be the output of this code?

One Examplepackage One;

public class DayOfYear{ public DayOfYear() {month = day = 1;} public DayOfYear(int monthVal, int dayVal) {month = monthVal; day = dayVal; } void input(int m, int d) {month = m; day = d;} public void output() { System.out.println(“month=“ +month+ ”, day=“ +day); } private int month, day;}

package Two;

public class TestDemo{ public static void main() {

DayOfYear date1(3,18), examdate();

System.out.print(“date1.month=“ +date1.month+ ”, date1.day=“

+date1.day); examdate.input(3,20); examdate.output(); }}

Compiling error! Why?

One Examplepackage One;

public class DayOfYear{ public DayOfYear() {month = day = 1;} public DayOfYear(int monthVal, int dayVal) {month = monthVal; day = dayVal; } void input(int m, int d) {month = m; day = d;} public void output() { System.out.println(“month=“ +month+ ”, day=“ +day); } int month, day;}

package Two;

public class TestDemo{ public static void main() {

DayOfYear date1(3,18), examdate();

System.out.print(“date1.month=“ +date1.month+ ”, date1.day=“

+date1.day); examdate.input(3,20); examdate.output(); }}

Compiling error! Why?

Default access is “package access!” How to create objects?

One Examplepublic class DayOfYear{ public DayOfYear() {} public DayOfYear(int monthVal, int dayVal) {month = monthVal; day = dayVal; } void input(int m, int d) {month = m; day = d;} public void output() { System.out.println(“month=“ +month+ ”, day=“ +day); } private int month=1, day=1;}

public class TestDemo{ public static void main() { DayOfYear date1, examdate; date1=new DayOfYear(); examdate=new DayOfYear(); date1.output(); examdate.input(3,20); examdate.output(); }}

How about this example?

Constructor

Do you know the role of constructor?

When will a constructor be called?

When will an object be created?

How many constructors can a class have?

Do you need a destructor in Java?

Exercise

YourClass anObject = new YourClass(42, 'A'); YourClass anotherObject = new YourClass(); anotherObject.doStuff();YourClass oneMoreObject; oneMoreObject.doStuff();oneMoreObject.YourClass(99, 'B');

public class YourClass { private int information = 0; private char moreInformation = ‘’; public YourClass( int newInfo, char moreNewInfo) { <Details not shown.> } public void doStuff() { <Details not shown.> } }

Given the following definition

Which of the following are legal?

Exercise

YourClass anObject = new YourClass(42, 'A'); YourClass anotherObject = new YourClass(); anotherObject.doStuff();YourClass oneMoreObject; oneMoreObject.doStuff();oneMoreObject.YourClass(99, 'B');

public class YourClass { private int information = 0; private char moreInformation = ‘’; public YourClass( int newInfo, char moreNewInfo) { <Details not shown.> } public void doStuff() { <Details not shown.> } }

Given the following definition

Which of the following are legal?

Java Inheritance• Inheritance is one of the main techniques of object-

oriented programming (OOP).• It enables potentially infinite reuse of resources.• It can be used to enhance and improve the previous

programs for the changing requirements

In one sentence, inheritance allows us to create new class from the existing classes without doing much repeated work.

Java Inheritance

public class Employee{public // constructor listpublic // member functionsprivate string name, ssn;private double netPay;}

public class HourlyEmployee extends Employee{public // constructor list // new member functionsprivate double wageRate;private double hours;}

Java Inheritance

• The phrase extends BaseClass must be added to the derived class definition.

• The derived class inherits all the public methods, all the public and private instance variables, and all the public and private static variables from the base class.• The code is reused without having to explicitly copy it, unless the

creator of the derived class redefines one or more of the base class methods

• The derived class can add more instance variables, static variables, and/or methods

Overriding a method

Can the return type of the overriding method be modified?

public class Base{public Employee func(){ … return Employee();}private int val1;private string val2;}

public class Derived extends Base{public HourlyEmployee func(){……return HourlyEmployee();}private int val3;private double val4;}

Can we change the access modifier of a method?

Need to have the same function signatures!

Overriding a method

Can the return type of the overriding method be modified?

public class Base{public Employee func(){ … return Employee();}private int val1;private string val2;}

public class Derived extends Base{public HourlyEmployee func(){……return HourlyEmployee();}private int val3;private double val4;}

Typically no! But it is allowed if the derived class has the return type which is the derived class of the return type of the original function!

Can we change the access modifier of a method?

Need to have the same function signatures!

Yes, but can only weaken the control.

Constructor in derived classesWhat do we need to pay attentions to ?

We need to properly initialize the member variables inherited from the base class

Constructor in derived classesWhat do we need to pay attentions to ?

We need to properly initialize the member variables inherited from the base class

How?Use key word super(…) to call the corresponding constructor of the base class.super(…) should always be the first line.

public class Base{public Base(){ val1= 0; val2 = “”;}public int val1;private string val2;}

public class Derived extends Base{public Derived(){ super(); val1 = 0; val4 = 0.0;}public int val1;private double val4;}

An example on super constructorpublic class Base{

private int x, y = 0;public Base(int x_val, int y_val){ x = x_val; y = y_val;}public Base(int x_val){ this (x_val); }……

}

The base class

public class Derived extends Base{

private double x, y = 0;public Derived(double x_val, double y_val){ x = x_val; y = y_val;}public Derived(double x_val){ this (x_val, 0.0); x = x_val; super (x_val);}……

}

The derived class

Any issues?

An example on super constructorpublic class Base{

private int x, y = 0;public Base(int x_val, int y_val){ x = x_val; y = y_val;}public Base(int x_val){ this (x_val); }……

}

The base class

public class Derived extends Base{

private double x, y = 0;public Derived(double x_val, double y_val){ // Base class does not have a constructor with empty argument list! x = x_val; y = y_val;}public Derived(double x_val){ this (x_val, 0.0); x = x_val; super (x_val); // super needs to be in the first line!}……

}

The derived class

Any issues?

An example on super constructorpublic class Base{

private int x = 0, y = 0;public Base() {}public Base(int x_val, int y_val){ x = x_val; y = y_val;}public Base(int x_val){ this (x_val); }……

}

The base class

public class Derived extends Base{

private double x, y = 0;public Derived(double x_val, double y_val){ super (x_val); // super needs to be in the first line! x = x_val; y = y_val;}public Derived(double x_val){ this (x_val, 0.0); x = x_val; }……

}

The derived class

Any issues?

Constructor in derived classesWhat do we need to pay attentions to ?

We need to properly initialize the member variables inherited from the base class

How?Use key word super(…) to call the corresponding constructor of the base class.super(…) should always be the first line.

Do you know the difference between super(…) and this(…)?

Do you know how to use super. to invoke the overridden function from the base class?

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

what will be the output?

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Base class.Derived class.

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Base class.Derived class.

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Base class.Derived class.Base class.Derived class.Derived class 2.

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Base class.Derived class.Base class.Derived class.Derived class 2.

Examplepublic class Base{

protected int x=0, y=0;public Base(int x_val, int y_val){ x = x_val; y = y_val; System.out.println(“Base class.”);}public Base(){ this (0, 0); }public void print(){ System.out.println(“x=“+x); System.out.println(“y=“+y);}

}

public class Derived extends Base{

protected double x=0, y=0;public Derived(){ this (0.0, 1.0);}public Derived(double x_val, double y_val){ super ((int)x_val, (int)y_val); x = x_val; y = y_val; System.out.println(“Derived class.”);}

}

public class Derived2 extends Derived{

boolean setx_or_y = false;public Derived2(){}public Derived2(boolean flag, double val){ if (flag==true) x=val; else y=val; setx_or_y = flag; System.out.println("Derived class 2.");}public static void main(String[] args){ Base obj1; Derived obj2 = new Derived (4, 5); Derived2 obj3 = new Derived2(false, 3); obj2.print(); obj3.print();}

}

Base

Derived

Derived2

Base class.Derived class.Base class.Derived class.Derived class 2.x=4y=5x=0y=1

Take Home Examplepublic class Base1{

public Base1(int val){ System.out.println(“Base1: “+val);}

}

public class Derived11 extends Base1{

public Derived11 (int val){ super(val); System.out.println(“Derived11: “+val);}

}

public class Derived12 extends Derived11 {

public Derived12 (int val){ super(val); System.out.println(“Derived12: “+val);}

}

public class Base2{

public Base2(int val){ System.out.println(“Base2: “+val);}

}

public class Derived21 extends Base2{

public Derived21 (int val){ super(val); System.out.println(“Derived21: “+val);}

}

public class Derived31 extends Base3{

private Derived11 comp1;private Derived21 comp2;public Derived31(int val){ super(val+1); comp1 = new Derived12(val+2); comp2 = new Derived21(val+4);}public static void main(String[] args){ Derived31 obj = new Derived31(1);}

}

public class Base3{

public Base3(int val){ System.out.println(“Base3: “+val);}

}

Java Polymorphism

• Why do we need polymorphism.– Further improve the reuse and maintenance of software

• What does polymorphism mean?– It refers to the ability to associate many meanings to one

method name by means of a special mechanism known as late binding or dynamic binding.

– What does late binding mean?• determining the instance function based on the type of the

calling object during run time!

Java Polymorphism

• How to enable late binding in Java?

• What are the three ingredients to enable Java polymorphism?

Java Polymorphism

• How to enable late binding in Java?– Most functions are late binding

• What are the three ingredients to enable Java polymorphism?– Inheritance– method overriding– upcasting

Examplepublic class Animal{ public Animal() { System.out.println(“Animal.”);} public void eat() { System.out.println(“Animal needs to eat.”); }}

public class Bird extends Animal{ public Bird () { System.out.println(“Bird.”);} public void eat() { System.out.println(“Bird eats bugs.”); }}

public class Human extends Primate{ public Human() { System.out.println(“Human.”);} public void eat() { System.out.println(“Human eats whatever we can eat.”); }}

public class Primate extends Animal{ public Primate() { System.out.println(“Primate.”);} public void eat() { System.out.println(“Primate eats both vegs and meat.”); }}

public class WhatAnimalEat{ public static void whatdoyoueat(Animal a) { a.eat(); } public static void main(String[] args) { Animal a, b, c; a = new Animal(); b = new Bird(); c = new Human(); whatdoyoueat(a); whatdoyoueat(b); whatdoyoueat(c); }}

What will be the output?

The final Modifier

• If the modifier final is placed before the definition of a method, then that method may not be redefined in a derived class

• If the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes

Examplepublic class Animal{ public Animal() { System.out.println(“Animal.”);} public void eat() { System.out.println(“Animal needs to eat.”); }}

public class Bird extends Animal{ public Bird () { System.out.println(“Bird.”);} public void eat() { System.out.println(“Bird eats bugs.”); }}

public class Human extends Primate{ public Human() { System.out.println(“Human.”);}}

public class Primate extends Animal{ public Primate() { System.out.println(“Primate.”);} public final void eat() { System.out.println(“Primate eats both vegs and meat.”); }}

public class WhatAnimalEat{ public static void whatdoyoueat(Animal a) { a.eat(); } public static void main(String[] args) { Animal a, b, c; a = new Animal(); b = new Bird(); c = new Human(); whatdoyoueat(a); whatdoyoueat(b); whatdoyoueat(c); }}

What will be the output?

Abstract ClassTo postpone the definition of a method, it is specified as an abstract method.

public abstract class Figure{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public abstract void draw(); public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;};

Abstract methods define the interface of the base class and all its derived classes, because…?

An abstract class must have the “abstract” modifier in its class heading

A class containing at least one abstract method needs to be specified as an abstract class.

Abstract ClassTo postpone the definition of a method, it is specified as an abstract method.

public abstract class Figure{ public Figure (double cx, double cy) { center_x=cx; center_y=cy;} public Figure() { this (0,0); } public abstract void draw(); public abstract void area(); public void reset_center(double x, double y) { center_x=x; center_y=y;} private double center_x=0, center_y=0;};

Abstract methods define the interface of the base class and all its derived classes, because…?

An abstract class must have the “abstract” modifier in its class heading

A class containing at least one abstract method needs to be specified as an abstract class.

Can an abstract class be used to create an object? Why or why not?

Examplepublic abstract class Base{ protected int type = 0;

public Base(int type){ type = type; System.out.println(“Base class.”);}public Base(){ this (0); }public abstract int getType(){ return type;}public String toString(){ return Integer.toString(getType());}

}

public class Derived extends Base{private int derived_type = 1;

public Derived (int type){ super (type); derived_type=type; System.out.println(“Derived class.”); }public Derived(){ this(1); }public int getType(){ System.out.print(“Derived class type ”); return derived_type ;}

}

public class TestDemo{ public static void main(String[] args) { Base obj1 = new Derived(1); Base obj2 = new Base(); Base obj3 = new Derived2(3) System.out.println(obj1); System.out.println(obj2); System.out.println(obj3); }}

public class Derived2 extends Derived{private int derived_type = 2;

public Derived2 (int type){ super (type); derived_type=type; System.out.println(“Derived class 2.”); }public Derived2(){ this(2);}public int getType(){ System.out.print(“Derived class 2 type ”); return type;}

}

What will be the output of this code?

Java Interface A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implements the interface.

Java Interface A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implement the interface.

public interface Interface_Name{public return_type func1(<argument list>);public void func2();public String getInfo();public boolean compareTwoObjs(Object obj1, Object obj2);}

public for the interface methods!

Java Interface A Java interface specifies a set of methods that any class that implements the interface must have.

One way to view an interface is as an extreme form of an abstract class.An interface is NOT a class, but rather a type!

Purpose: define methods with parameters of an interface type, and have the code apply to all classes that implement the interface.

public interface Interface_Name{public return_type func1(…)public void func2();public String getInfo();}

Other classes that use the interface need to use the key word implements.

public ConcreteClass implements Interface_Name{public Object func1(…){//provide func body!}public void func2(){ // func2 body}public String getInfo(){ // getInfo body}}