300 likes | 481 Views
Vocabulary. Key Terms polymorphism - Selecting a method among many methods that have the same name . subclass - A class that inherits variables and methods from a superclass but adds instance variables and methods, or redefines methods .
E N D
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods from a superclass but adds instance variables and methods, or redefines methods. superclass - A general class from which a more specialized class inherits. dynamic binding – The process that determines what sequence of code to follow at runtime. has-a relationship – A relationship when one object belongs to another object. hierarchy – An arrangement of objects or classes in which some they above, below, or at the same level as one another. inheritance – When objects can be defined from other objects without having to define any new classes. is-a relationship – A relationship when one class is a subclass or another class. method overriding – When a subclass implements a superclass method by using the same name and same parameters. super keyword – When a method overrides a superclass method, the overridden method can be invoked by using the keyword super.
Every thing in Java is in a Class - Structure Import Statement Class name Field Data Line 4 and Line 5 Constructor There are 2 constructors One is overloaded constructor. Mutator Methods aka setter method Accessor methods aka getter method
Constructors and creating objects • You can only create an object the way the constructor specifies. • Student s = new Student(); • This students name would be “Student” and the countObjects would store 1. • Student s2 = new Student(“Mary Smith”); • This students name would be assigned when created as Mary Smith and countObjects would become 2. • I could not do this: • Student s3 = new Student(“Mary Smith”, 3); • There is not constructor that looks like this: public Student(String n, int c) { public Student() { name = "Student"; countObjects++; } public Student(String n) { name = n; countObjects++; }
Constructor tips • If you do not create a constructor, java creates one for you. It is called a default constructor. public Student() • Once you create your own constructor that is overloaded, you lose the default constructor. • Therefore, usually classes always create a default constructor and any other overloaded constructors as needed. • public Student(String n) // overloaded
Driver class Classes do not contain the main. They contain the source code for the class. You create a Driver or Tester class to call the methods from the class. OUTPUT
Field Data TYPES OF VARIABLES Global – Can be seen throughout the program Local – used in a method and can only be seen in that method. GLOBAL VARIABLES ARE CREATED IN AN AREA CALLED FIELD • Instance Variable – private and every object created receives its own copy. • BankAccount Class name, balance, acctNum, • Final variable – Uses key word final • Cannot be changed usually created static as well. private final String BANK_TYPE; BANK_TYPE = “BankAccount”; • Static variable: one copy for the entire class. Every object gets a copy but the same copy. • private static int count = 0; Then inside the constructor you would count++; This would add one to count every time an object is created and you will have a count of how many objects there are.
Creating Methods • There are two types of methods: • return methods These return a specific data type. All your coding bat problems are return methods. They must return the data specified in the method header • public StringgetName() { • Void method These methods do not return information. They execute a set of instructions or set information. • public void setName(String name) { • public void eat() {
RETURN METHODS You cannot access field data information directly from another class. Therefore, you must create accessor methods to return the information. Accessor Methods to return the data stored in the instance variables. They are usually named get and then the variable they are returning information from. They are always the return type of the variable. Field Data From Student private String name; private static int countObjects; public StringgetName() { return name; } public static intnumObjects() { return countObjects; } public StringtoString() { return "Name: " + name + " number of Objects " + Student.countObjects; } }
Mutator Methods Methods created to change the data in the instance variables. The information you want to change the variable to is passed through the parameter. They are void. Must use the variable passed through the parameter to set the information. VOID METHODS You need a way to change the information in your instance variables. Called setter methods. Set data in the instance variables. • public void setName(String n) • { • name = n; • }
The Object Class • A class called Object is defined in the java.lang package of the Java standard class library • All classes are derived from the Object class • even if a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class • the Object class is therefore the ultimate root of all class hierarchies • The Object class contains a few useful methods, which are inherited by all classes • toString() • equals() • clone()
toString() method The toString method comes from Object. If you do not include one when you print the object you will receive a reference to a memory location for the object. Student@21048 You always override the toString method From object. It is used to print the information you want from the object such as the instance variable information.
Inheritance • Methods allows a software developer to reuse a sequence of statements • Inheritance allows a software developer to reuse classes by deriving a new class from an existing one • The existing class is called the parent class, or superclass, or base class • The derived class is called the child class or subclass. • As the name implies, the child inherits characteristics of the parent • That is, the child class inherits the methods and data defined for the parent class.
Inheritance Person Inheritance should create an is-a relationship, meaning the child is a more specific version of the parent Employee Student GradStudent Undergrad The arrow points to its superclass. The arrow designates the is-a relationship. is – a relationship has a relationship The Employee is a person The Student is a Person The GradStudentis a Student No!-- Person is a student. // false error A person has a student Person has a GradStudent Person has a Student Student has a GradStudent Student has a Undergrad
Super and Subclasses • Create an SuperHero class that will be the parent (super class or base). • A SuperHero has a name , weapon, and power. All will be Strings. Create static count to keep track of objects created.
Parent Methods Create accessor and mutator for each of the field data items.
Create a subclass of a specific SuperHero called CaptainAmerica This class will inherit the attributes of the SuperHero class and add a speed and boolean canFly. Constructors for the class
Method for subclass. Create methods for additional field data. Speed and canFly. You always create a toString() method particular to the class. I have included an overridden method from parent. This means that the parent class has this same method but the subclass has created it in the class and created new code.
Overriding a method: An overriden method has the same name as the parent class method. It can return the parent method by using the word super and then create additional unique information to return. This method should return Super Hero Captain America
Main Class Driver program SuperHero s is from the default constructor. Will print information from the default constructor.
Create CaptainAmerica Object public String toString() { return "Number of Heros: " + getCount() + " Name: " + name + " Weapon: " + weapon + " Power: " + power; } Number of Heros: 2 Name: Super Hero Weapon: Shield Power: Strength Speed: 15 can fly: false parentMethodOveride: SuperHero Captain America
Overriding a method c is a CaptainAmerica object It has a parentMethodOverRide() It calls the method from the parent class super.parentMethodOverRide() and then adds Captain America To call a method from the parent class use the word super Parent Method: public String parentMethodOverRide() { return "SuperHero"; } Subclass method: public String parentMethodOverRide() { return super.parentMethodOverRide() + " Captain America"; } It returns: SuperHero Captain America
Polymorphism • Polymorphism: selecting a method among many that have the same name. Both classes have parentMethodOverRide() • These methods can refer to objects of their own class or to objects of the classes inherited from their class • Decision is made at run-time called late or dynamic binding
Polymorphism When a method is overwritten in at least on subclass, it is called polymorphic. It is possible for objects to be polymorphic as well. SuperHero s = new SuperHero(); s = new CaptainAmerica();
Early binding and Late Binding CaptainAmerica c = new CaptainAmerica(); c.parentMethodOverRide() When you compile, the Java compiler looks to see how the object was created. Specifically it looks on the left hand side. c was created as a CaptainAmerica object. If the method being called is in that class it will compile. At run time it looks on the right hand side to see the particular type of object created. It is a CaptainAmerica object. It goes to that class and calls that method.
Dynamic Binding • Done at run-time SuperHero s = new CaptainAmerica(); s is a type of Superhero but it is a CaptainAmerica. It will use the appropriate method at run-time.
Static Binding • Compile time • SuperHero s = new CaptainAmerica(); s.setSpeed(10); Compile time error because setSpeed is not in the SuperHero Class. Static Binding looks to see if the method is in the SuperHero class and if not it will not compile.
Casting SuperHero h = new CaptainAmerica("Captain America", "Shield", "Strength", 10 ); I can create an object from the parent. You may want to do this if you want to decide at run time the exact object it is. However, if I called the setSpeed method or getSpeed method from the subclass, it would not compile. h.setSpeed(10); The Compiler looks on the left and sees that it is a SuperHero. The method setSpeed is not in that class. It will not compile unless I cast the object to the correct exact type.
Casting SuperHero h = new CaptainAmerica("Captain America", "Shield", "Strength", 10 ); ((CaptainAmerica)h).setSpeed(10); I have now cast the object to the exact type it really is. Now the compiler will look at the left and see that it is really a CaptainAmerica object. It does have a setspeedmethod so it will compile. At run time it looks to the left and calls the method from the appropriate class.
Assignment • Programming Exercise P10.11 • Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.