Inheritance allows you to reuse methods that you've So if you had a Java class for each, which class would Recall: overriding a method is when a subclass
Previous PDF | Next PDF |
[PDF] Method Overriding in Java - Pragjyotish College
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding
[PDF] Encapsulation, Inheritance, Types, Overloading, Overriding
Overloading ▫ Class-based, typically Java – The equals() method in Point takes a Point instead of an Object as an argument – It does not override equals in
[PDF] Inheritance, overloading and overriding
Inheritance, overloading and overriding ○ Recall a method that is inherited by the parent, and the child class wants to mapped in java as – abstract
[PDF] Java inheritance
methods and fields of the class it inherited from plus any methods and fields Overriding ▫ Class Vector{ and methods 12 Inheritance in Java: extends Car
[PDF] Chapter 9: Inheritance and Interfaces - CSULB
be overloading the inherited method, not overriding it • ChoiceQuestion setText("In which country was the inventor of Java born?"); cq presentQuestion();
[PDF] Inheritance and Polymorphism - Fas Harvard - Harvard University
Overriding an Inherited Method it overrides the toString method inherited from Automobile A class in Java inherits directly from at most one class • However
[PDF] Inheritance and Polymorphism - Rose-Hulman
Inheritance allows you to reuse methods that you've So if you had a Java class for each, which class would Recall: overriding a method is when a subclass
[PDF] Java Inheritance Interview Questions
The superclass and subclass have “is-a” relationship between them Inheritance is used For Method Overriding and For Code Reusability Question: Types of
[PDF] Overloading and Overriding Methods Java permits a class to replace
Overloading and Overriding Methods Java permits a class to replace (or override ) a method that it has it has inherited A subclass can define a method with the
[PDF] Java Class And Inheritance - NYU
Methods and constructors ❑ definition and passing ❑ method overloading ▫ Class variables, constants and methods ▫ Inheritance ❑ inheritance hierarchy
[PDF] method that calls itself repeatedly
[PDF] methode apprendre a lire a 4 ans
[PDF] méthode de gauss
[PDF] methode facile pour apprendre la division
[PDF] méthode pour apprendre à compter cp
[PDF] méthode pour apprendre à lire à 3 ans
[PDF] methode pour apprendre l'hebreu
[PDF] méthode pour apprendre l'histoire géographie
[PDF] methode pour apprendre la division
[PDF] methode pour apprendre les divisions
[PDF] methode pour apprendre les divisions en ce2
[PDF] méthode rapport de stage droit
[PDF] methode simple pour apprendre la division
[PDF] méthodologie commentaire composé pdf
Inheritance and Polymorphism
CSSE 221
Fundamentals of Software Development Honors Rose-Hulman Institute of TechnologyAnnouncements
¥ Capsules:
Ð Summary, quiz, and key each in a separate
document Ð Quiz has place for students' names, questions are numbered Ð Quiz: max of 1 side Ð Key is marked as such¥ Look for email about my BigRational unit
tests¥ Questions?
This week: BallWorlds assignment
¥ Last class:
Ð Intro to UML as a communication tool Ð Writing methods you don't call Ð Using this¥ Today:
Ð Inheritance Ð Polymorphism
¥ Friday:
Ð Introducing next weekÕs assignment Ð Arrays and ArrayLists Ð (Using the debugger)Inheritance
¥ Some slides inspired by Fall 2006-2007 CSSE221 students: Ð Michael Auchter Ð Michael Boland Ð Andrew HettlingerInheritance ¥ Objects are unique
¥ But they often
share similar behavior Student Professor Software Engineer Chemical Engineer Physicist Guitarist DrummerWhy not just copy-and-paste?
¥ Say I have an Employee class and want to create an HourlyEmployee class that adds info about wages. Why not copy-and-paste, then modify?
The Basics of Inheritance
¥ Inheritance allows you to reuse methods that youÕve already written to create more specialized versions of a class.
¥ Syntax:
public class HourlyEmployee extends EmployeeSubclass Superclass HourlyEmployee IS-A Employee
1-1, 2-1
Your turn
¥ Question: What is the relationship between a parrot and a bird?Your turn
¥ What is the relationship between a parrot and a bird? Ð Every parrot is a bird, but not every bird is a parrot.Ð So if you had a Java class for each, which
class would extend which?Some Key Ideas in Inheritance
¥ Code reuse
¥ Overriding methods ¥ Protected visibility ¥ The ÒsuperÓ keywordCode re-use
¥ The subclass inherits all the public and protected methods and elds of the superclass. Ð Constructors are not inherited Ð Constructors can be invoked by the subclass¥ Subclass can add new methods and elds.
Overriding Methods
¥ DudThatMoves extends Dud
¥ DudThatMoves will de ne an act() method
with the same signature that overrides DudÕs methodItÕs exactly the same as in the superclass What do you think happens if our child class doesnÕt override a method in the superclass?
Visibility ModiÞers
¥ Public Ð Accessible by any other class in any package.¥ Private Ð Accessible only within the class; for elds. ¥ Protected Ð Accessible only by classes within the same
package and any subclasses in other packages. Ð We won't use protected elds, but use private with protected accessors.Ð Private elds are encapsulated
¥ Default (No Modi er) Ð Accessible by classes in the same package but not by classes in other packages.Ð Use sparingly
1-2The ÒsuperÓ Keyword
¥ ItÕs like the word Òthis,Ó only ÒsuperÓ:¥ Two uses:
Ð To call a superclass' method, use
super.methodName(É)Ð To call a superclass' constructor, use
super(some parameter) from the child classÕ constructor¥ Don't use super for elds (they're private
anyway).1-3, 2-6
The ÒsuperÓ Keyword
¥ Methods can call super.methodName(É)
Ð To do the work of the parent class method,
plusÉÐ Additional work for the child class
public class Workaholic extends Worker { public void doWork() { super.doWork(); drinkCoffee(); super.doWork(); } }
The ÒsuperÓ Keyword
¥ Methods can call super.methodName(É)
Ð To do the work of the parent class method,
plusÉÐ Additional work for the child class
public class Workaholic extends Worker { // If a Workaholic just worked // like a worker, it would inherit doWork // NEVER write code like this: public void doWork() { super.doWork(); } }
The ÒsuperÓ Keyword
¥ A common experience?
public class RoseStudent extends Worker { public void doWork() { while ( isCollapsed) { super.doWork(); drinkCoffee(); } super.doWork(); } }
Rules of using super in constructors
¥ A super(É) call must be the Þrst line of the code of a classÕs constructor if it is to be used.
The this Keyword
1. this.someField and this.someMethod(): nice style
2. this alone is used to represent the whole
object: environment.addBall(this)The this Keyword
3. this calls another constructor
this must be the rst thing called in a constructor.Therefore, super(É) and
this(É) cannot be used in the same constructor. public class Foo {private String message; public Foo(){ this(ÒThis is sad.Ó); } public Foo(String s){ this.message = s; } }
Overriding vs. Overloading
¥ Recall: overriding a method is when a subclass has method with the same signature (name and parameter list) as its superclass
Ð MoverÕs act() and BouncerÕs act()
¥ Overloading a method is when two methods
have the same name, but different parameter lists Arrays.sort(array) and Arrays.sort(array, new ReverseSort())2-2,2-3
More notes
¥ Every object in Java extends java.lang.Object Ð DonÕt have to say it explicitly Ð This is why every class has a basic toString() and a basic clone() method.¥ Abstract classes contain abstract
(unimplemented) methods.Ð Abstract classes can't be instantiated, just
extended1-4, 2-5
Final notes
¥ What does it mean to be declared Þnal?
Ð Final Þelds canÕt be assigned a new value Ð Final methods cannot be overridden Ð Final classes cannot be extended
¥ There is only single inheritance in Java
1-4 Next¥ Finish the inheritance quiz
¥ Do the Inheritance Demo linked from the
Schedule page
¥ Take a break
Polymorphism
¥ Polymorphism allows a reference to a superclass or interface to be used instead of a reference to its subclass
// Rectangle and Circle could implement or extend Shape Shape rect = new Rectangle(); Shape circle = new Circle(); void printArea(Shape shape) { System.out.println(shape.getArea()); }
1-1, 1-3, 2-1, 2-2
Polymorphism
double totalArea(ArrayList
1-4, 2-4
Example
¥ In the bird and parrot example, consider a bird method: static void printCall(Bird bird) { System.out.println(bird.call); } ¥ Generic: printBirdCall expects a Bird, but any type of bird is OK. ¥ Cannot write Parrot p = new Bird(); -thereÕs not enough info ¥ However, without casting, b can only use bird methods; parrot-speci c information can't be accessed Bird b = new Parrot(); printBirdCall(b); Parrot p = new Parrot(); printBirdCall(p);Casting and instanceof
¥ If we know that b is a Parrot, we can cast it and use Parrot methods: ((Parrot)b).speak() ¥ At runtime, if b is just a Bird, the JVM will throw aClassCastException.
¥ To test this, use instanceof:
if (b instanceof Parrot) { ((Parrot)b).speak()) }Late Binding: The Power of Polymorphism
HourlyEmployee h = new HourlyEmployee("Wilma Worker", new Date("October", 16, 2005), 12.50, 170); SalariedEmployee s = new SalariedEmployee("Mark Manager", new Date("June", 4, 2006), 40000);Employee e = null; if (getWeekDay().equals(ÒSaturdayÓ) e = h; else e = s; System.out.println(e);
Is e's actual type (and thus which toString() to use) known at compile-time or run-time?