320 likes | 444 Views
Classes, Objects Inheritance. Week 12. Classes and Objects. A Java program consists of one or more classes A class is an abstract description of objects Here is an example class: class Dog { ...description of a dog goes here... } Here are some objects of that class:. More Objects.
E N D
Classes, ObjectsInheritance Week 12
Classes and Objects • A Java program consists of one or more classes • A class is an abstract description of objects • Here is an example class: • class Dog { ...description of a dog goes here...} • Here are some objects of that class:
More Objects • Here is another example of a class: • class Window { ... } • Here are some examples of Windows:
Data usually goes first in a class Classes contain data definitions • Classes describe the data held by each of its objects • Example: • class Dog { String name;int age;...rest of the class...} • A class may describe any number of objects • Examples: "Fido", 3; "Rover", 5; "Spot", 3; • A class may describe a single object, or even no objects at all
Methods usually go after the data Classes contain methods • A class may contain methods that describe the behavior of objects • Example: • class Dog { ... void bark() { System.out.println("Woof!"); }} • When we ask a particular Dog to bark, it says “Woof!” • Only Dog objects can bark; the classDog cannot bark
Methods contain statements • A statement causes the object to do something • (A better word would be “command”—but it isn’t) • Example: • System.out.println("Woof!"); • This causes the particular Dog to “print” (actually, display on the screen) the characters Woof!
Methods may contain temporary data • Data described in a class exists in all objects of that class • Example: Every Dog has its own name and age • A method may contain local temporary data that exists only until the method finishes • Example: • void wakeTheNeighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); i = i – 1; }}
(This part is the constructor) Classes always contain constructors • A constructor is a piece of code that “constructs,” or creates, a new object of that class • If you don’t write a constructor, Java defines one for you (behind the scenes) • You can write your own constructors • Example: • class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; }}
Program File File File Class Variables Statements Constructors Variables File Statements Methods Variables Diagram of program structure • A program consists of one or more classes • Typically, each class is in a separate .java file
Summary • A program consists of one or more classes • A class is a description of a kind of object • In most cases, it is the objects that do the actual work • A class describes data, constructors, and methods • An object’s data is information about that object • An object’s methods describe how the object behaves • A constructor is used to create objects of the class • Methods (and constructors) may contain temporary data and statements (commands)
Writing and running programs • When you write a program, you are writing classes and all the things that go into classes • Your program typically contains commands to create objects (that is, “calls” to constructors) • Analogy: A class is like a cookie cutter, objects are like cookies. • When you run a program, it creates objects, and those objects interact with one another and do whatever they do to cause something to happen • Analogy:Writing a program is like writing the rules to a game; running a program is like actually playing the game • You never know how well the rules are going to work until you try them out
Getting started • Question: Where do objects come from? • Answer: They are created by classes. • Question: Where does the first object come from? • Answer: Programs have a special main method, not part of any object, that is executed in order to get things started • public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog} • The special keyword static says that the main method belongs to the class itself, not to objects of the class • Hence, the main method can be “called” before any objects are created • Usually, the main method gets things started by creating one or more objects and telling them what to do
A bad program • public class Dog { String name; int age; Dog(String name, int age) { this.name = name; this.age = age; } public static void main(String[] args) { bark(); } void bark() { System.out.println("Woof!"); }} • non-static method bark() cannot be referenced from a static context --------- new Dog("Fido", 5).bark();
A complete program • class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); } • void wakeTheNeighbors( ) { int i = 50; while (i > 0) { bark( ); i = i – 1; }}public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.wakeTheNeighbors();} • } // ends the class
Inheritance • Inheritance • Reserved word protected • Reserved word super • Overriding methods • Class Hierarchies • Abstract class
Inheritance • Inheritanceallows a software developer to derive a new class from an existing one • The existing class is called the parent class,superclass, or base class • The new class is called the child class, subclass or derived class • As the name implies, the child inherits characteristics of the parent • That is, the child class inherits the methods and data defined by the parent class
Vehicle Car Inheritance • Inheritance is based on an is-a relationship • The child is a more specific version of the parent • Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class
Inheritance • Software reuseis a fundamental benefit of inheritance • By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software • However, a programmer can tailor a derived class as needed by adding new variables or by “overriding” some of the inherited methods
Deriving Subclasses • In Java, we use the reserved word extendsto establish an inheritance relationship public class Car extends Vehicle { // class contents }
The protected Modifier • Visibility modifiers affect the way that class members can be used in a child class • Variables and methods declared with private visibility cannot be referenced by name in a child class • They can be referenced in the child class if they are declared with public visibility -- but public variables violate the principle of encapsulation • There is a third visibility modifier that helps in inheritance situations: protected
The protected Modifier • The protectedmodifier allows a child class to reference a variable or method directly in the child class • It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility • A protected variable is visible in any class that is a child of the class where it is defined • A protected variable is also visible in any class that is in the same package as the class where it is defined (we don’t use packages in this course)
The super Reference • Constructors are not inherited even though they have public visibility • Yet we often want to use the parent's constructor to set up the "parent's part" of the object • The superreference can be used to refer to the parent class and invoke the parent's constructor public class Child extends Parent { public Child() { super(); // a call to Parent() // plus whatever code we need for Child } }
The super Reference • A child’s constructor is responsible for calling the parent’s constructor • The first line of a child’s constructor should use the super reference to call the parent’s constructor • The super reference can also be used to reference (with a dot .) other variables and methods defined in the parent’s class
Multiple Inheritance • Multiple inheritance allows a class to be derived from two or more classes inheriting members of all parents • Collisions (such as the use of the same variable name in two or more parents) must be resolved • Java does not support multiple inheritance (Some other languages such as C++ do) • In Java, a class can have only one direct parent • The use of interfaces in Java gives us most of the benefits of multiple inheritance without the problems
Overriding Methods • A child class can override the definition of an inherited method in favor of its own • The new method must have the same signature as the parent's method, but can have a different body • The class of the object used to execute an overridden method determines which version of the method is invoked • In the child, the method in the parent class can be invoked explicitly using the super reference • If a method is declared in the parent class with the final modifier, it cannot be overridden
Overriding Variables • The concept of overriding can be applied to data and is called shadowing variables • Shadowing variables should be avoided because it tends to cause unnecessarily confusing code
Overloading vs. Overriding • Overloading deals with multiple methods with the same name in the same class that have different signatures (different parameter lists) • Overriding deals with two methods (one in a parent class and one in a child class) that have the same signature (same parameter list) • Overloading lets you define a similar operation in different ways (using different input parameters) • Overriding lets you redefine a parent’s method in a child (using the same input parameters)
Business RetailBusiness ServiceBusiness KMart Macys Kinkos Class Hierarchies • A child class of one parent can be the parent of another child, forming a class hierarchy • Two children of the same parent are called siblings
Class Hierarchies • A child class inherits from all its ancestor classes • An inherited variable, constant, or method is passed continually down the line (unless it is declared private) • Common features should be put as high in the hierarchy as is reasonable • There is no single class hierarchy that is appropriate for all situations
The Object Class • A class called Objectis defined in the java.lang package of the Java standard class library • All classes are derived from the Object class • 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 • Therefore, the Object class is the ultimate root of all class hierarchies
The Object Class • The Object class contains a few useful methods which are inherited by all classes • For example, the toStringmethod is defined in the Object class • The toString method in the Object class is defined to return a string that contains the name of the object’s class along with other information (e.g. the address of its location in memory) System.out.println(new Object()); java.lang.Object@952905 • Every time we define the toString method, we are actually overriding the inherited definition
The Object Class • The equalsmethod of the Object class returns true if the two references are aliases • We can override equals in any class to define equality in some more appropriate way • As we've seen, the String class defines the equals method to return true if the two String objects contain the same characters • The designers of the String class have overridden the equals method inherited from Object in favor of a more useful version