🗊Презентация 4. Java OOP. 4. Inheritance and Polymorphism

Нажмите для полного просмотра!
4. Java OOP. 4. Inheritance and Polymorphism, слайд №14. Java OOP. 4. Inheritance and Polymorphism, слайд №24. Java OOP. 4. Inheritance and Polymorphism, слайд №34. Java OOP. 4. Inheritance and Polymorphism, слайд №44. Java OOP. 4. Inheritance and Polymorphism, слайд №54. Java OOP. 4. Inheritance and Polymorphism, слайд №64. Java OOP. 4. Inheritance and Polymorphism, слайд №74. Java OOP. 4. Inheritance and Polymorphism, слайд №84. Java OOP. 4. Inheritance and Polymorphism, слайд №94. Java OOP. 4. Inheritance and Polymorphism, слайд №104. Java OOP. 4. Inheritance and Polymorphism, слайд №114. Java OOP. 4. Inheritance and Polymorphism, слайд №124. Java OOP. 4. Inheritance and Polymorphism, слайд №134. Java OOP. 4. Inheritance and Polymorphism, слайд №144. Java OOP. 4. Inheritance and Polymorphism, слайд №154. Java OOP. 4. Inheritance and Polymorphism, слайд №164. Java OOP. 4. Inheritance and Polymorphism, слайд №174. Java OOP. 4. Inheritance and Polymorphism, слайд №184. Java OOP. 4. Inheritance and Polymorphism, слайд №194. Java OOP. 4. Inheritance and Polymorphism, слайд №204. Java OOP. 4. Inheritance and Polymorphism, слайд №214. Java OOP. 4. Inheritance and Polymorphism, слайд №224. Java OOP. 4. Inheritance and Polymorphism, слайд №234. Java OOP. 4. Inheritance and Polymorphism, слайд №244. Java OOP. 4. Inheritance and Polymorphism, слайд №254. Java OOP. 4. Inheritance and Polymorphism, слайд №264. Java OOP. 4. Inheritance and Polymorphism, слайд №274. Java OOP. 4. Inheritance and Polymorphism, слайд №284. Java OOP. 4. Inheritance and Polymorphism, слайд №294. Java OOP. 4. Inheritance and Polymorphism, слайд №304. Java OOP. 4. Inheritance and Polymorphism, слайд №314. Java OOP. 4. Inheritance and Polymorphism, слайд №324. Java OOP. 4. Inheritance and Polymorphism, слайд №334. Java OOP. 4. Inheritance and Polymorphism, слайд №344. Java OOP. 4. Inheritance and Polymorphism, слайд №354. Java OOP. 4. Inheritance and Polymorphism, слайд №364. Java OOP. 4. Inheritance and Polymorphism, слайд №374. Java OOP. 4. Inheritance and Polymorphism, слайд №384. Java OOP. 4. Inheritance and Polymorphism, слайд №394. Java OOP. 4. Inheritance and Polymorphism, слайд №404. Java OOP. 4. Inheritance and Polymorphism, слайд №414. Java OOP. 4. Inheritance and Polymorphism, слайд №424. Java OOP. 4. Inheritance and Polymorphism, слайд №434. Java OOP. 4. Inheritance and Polymorphism, слайд №444. Java OOP. 4. Inheritance and Polymorphism, слайд №454. Java OOP. 4. Inheritance and Polymorphism, слайд №464. Java OOP. 4. Inheritance and Polymorphism, слайд №474. Java OOP. 4. Inheritance and Polymorphism, слайд №484. Java OOP. 4. Inheritance and Polymorphism, слайд №494. Java OOP. 4. Inheritance and Polymorphism, слайд №504. Java OOP. 4. Inheritance and Polymorphism, слайд №514. Java OOP. 4. Inheritance and Polymorphism, слайд №524. Java OOP. 4. Inheritance and Polymorphism, слайд №534. Java OOP. 4. Inheritance and Polymorphism, слайд №544. Java OOP. 4. Inheritance and Polymorphism, слайд №554. Java OOP. 4. Inheritance and Polymorphism, слайд №564. Java OOP. 4. Inheritance and Polymorphism, слайд №57

Содержание

Вы можете ознакомиться и скачать презентацию на тему 4. Java OOP. 4. Inheritance and Polymorphism. Доклад-сообщение содержит 57 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





4. Java OOP 
4. Inheritance and Polymorphism
Описание слайда:
4. Java OOP 4. Inheritance and Polymorphism

Слайд 2





Inheritance Basics (1 of 3)
Classes can be derived from other classes, thereby inheriting fields and methods from those classes:
		class Sub extends Sup {
			…
		}
Описание слайда:
Inheritance Basics (1 of 3) Classes can be derived from other classes, thereby inheriting fields and methods from those classes: class Sub extends Sup { … }

Слайд 3





Inheritance Basics (2 of 3)
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). 
The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Every class has one and only one direct superclass (single inheritance).
Class Object is exception, it is a root class
Описание слайда:
Inheritance Basics (2 of 3) A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class). Every class has one and only one direct superclass (single inheritance). Class Object is exception, it is a root class

Слайд 4





Inheritance Basics (3 of 3)
A subclass inherits all the members (fields, methods, and nested classes) from its superclass
Constructors are not members, so they are not inherited by subclasses
The constructor of the superclass can be invoked from the subclass
Описание слайда:
Inheritance Basics (3 of 3) A subclass inherits all the members (fields, methods, and nested classes) from its superclass Constructors are not members, so they are not inherited by subclasses The constructor of the superclass can be invoked from the subclass

Слайд 5





Members Inheritance
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. 
If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. 
You can use the inherited members as is, replace them, hide them, or supplement them with new members
Описание слайда:
Members Inheritance A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members

Слайд 6





Fields Inheritance
The inherited fields can be used directly
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
You can declare new fields in the subclass that are not in the superclass.
Описание слайда:
Fields Inheritance The inherited fields can be used directly You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended). You can declare new fields in the subclass that are not in the superclass.

Слайд 7





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(s);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(s); }

Слайд 8





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(s);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(s); }

Слайд 9





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(p);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(p); }

Слайд 10





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(p);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(p); }

Слайд 11





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(v1); }

Слайд 12





What will be the output?
class A{
   int v1 = 8;
   protected double p = -5.0;
   private String s = “1234”;
}
class B extends A{
   public void doSomething(){
		System.out.println(v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected double p = -5.0; private String s = “1234”; } class B extends A{ public void doSomething(){ System.out.println(v1); }

Слайд 13





Methods Inheritance
The inherited methods can be used directly as they are.
You can declare new methods in the subclass that are not in the superclass.
Описание слайда:
Methods Inheritance The inherited methods can be used directly as they are. You can declare new methods in the subclass that are not in the superclass.

Слайд 14





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 15





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 16





Methods Overriding and Hiding
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
Описание слайда:
Methods Overriding and Hiding You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it. You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.

Слайд 17





Constructors Call
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
Описание слайда:
Constructors Call You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Слайд 18





Private Members in a Superclass
A subclass does not inherit the private members of its parent class. 
However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
Описание слайда:
Private Members in a Superclass A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

Слайд 19





Exercise 4.4.1: DepoBase class
Modify 433DepoMonthCapitalize, 432DepoBarrier, and 431SimpleDepo projects with help of ancestor DepoBase class (should contain all common elements – fields and methods)
Описание слайда:
Exercise 4.4.1: DepoBase class Modify 433DepoMonthCapitalize, 432DepoBarrier, and 431SimpleDepo projects with help of ancestor DepoBase class (should contain all common elements – fields and methods)

Слайд 20





DepoBase Class (1 of 2)
public class DepoBase {
	protected Date startDate;
	protected int dayLong;
	protected double sum;
	protected double interestRate;
	public DepoBase() {}
	public DepoBase(Date startDate, int dayLong, double sum, 
			        double interestRate){
		this.startDate = startDate;
		this.dayLong = dayLong;
		this.sum = sum;
		this.interestRate = interestRate;	}
Описание слайда:
DepoBase Class (1 of 2) public class DepoBase { protected Date startDate; protected int dayLong; protected double sum; protected double interestRate; public DepoBase() {} public DepoBase(Date startDate, int dayLong, double sum, double interestRate){ this.startDate = startDate; this.dayLong = dayLong; this.sum = sum; this.interestRate = interestRate; }

Слайд 21





DepoBase Class (2 of 2)
// accessors
public double calculateInterest(LocalDate start, LocalDate 	maturity){
	int startYear = start.getYear();
	int maturityYear = maturity.getYear();
   .  .  .  .  .  .  .  .  .  .
	double dayCf = start.until(maturity, ChronoUnit.DAYS)
		 + 1;
    double interest = sum * (interestRate / 100.0) * 
		(dayCf / daysInYear);
    return interest;
}
Описание слайда:
DepoBase Class (2 of 2) // accessors public double calculateInterest(LocalDate start, LocalDate maturity){ int startYear = start.getYear(); int maturityYear = maturity.getYear(); . . . . . . . . . . double dayCf = start.until(maturity, ChronoUnit.DAYS) + 1; double interest = sum * (interestRate / 100.0) * (dayCf / daysInYear); return interest; }

Слайд 22





DepoSimple Class
public class DepoSimple extends DepoBase{
	public DepoSimple(){   }
	public DepoSimple(Date startDate, int dayLong, double 			sum, double interestRate){
		super(startDate, dayLong, sum, interestRate);
	}
public double getInterest(){
	double interest = 0.0;
  .  .  .  .  .  .  .  .  .  .  .  .
	return interest;
}
Описание слайда:
DepoSimple Class public class DepoSimple extends DepoBase{ public DepoSimple(){ } public DepoSimple(Date startDate, int dayLong, double sum, double interestRate){ super(startDate, dayLong, sum, interestRate); } public double getInterest(){ double interest = 0.0; . . . . . . . . . . . . return interest; }

Слайд 23





Exercise 4.4.1: DepoBase class
See 441DepoBase projects for the full text
Описание слайда:
Exercise 4.4.1: DepoBase class See 441DepoBase projects for the full text

Слайд 24





Casting Objects (1 of 3)
Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance:
		Object obj = new ClassName(); 
If, on the other hand, we write
		 ClassName cn = obj; 
we would get a compile-time error because 
obj is not known to the compiler to be a 
ClassName
Описание слайда:
Casting Objects (1 of 3) Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance: Object obj = new ClassName(); If, on the other hand, we write ClassName cn = obj; we would get a compile-time error because obj is not known to the compiler to be a ClassName

Слайд 25





Casting Objects (2 of 3)
We can tell the compiler to assign a ClassName to obj by explicit casting: 	ClassName cn = (ClassName)obj; 
This cast inserts a runtime check that obj is assigned a ClassName so that the compiler can safely assume that obj is a ClassName
If obj is not a ClassName at runtime, a ClassCastException will be thrown.
Описание слайда:
Casting Objects (2 of 3) We can tell the compiler to assign a ClassName to obj by explicit casting: ClassName cn = (ClassName)obj; This cast inserts a runtime check that obj is assigned a ClassName so that the compiler can safely assume that obj is a ClassName If obj is not a ClassName at runtime, a ClassCastException will be thrown.

Слайд 26





Casting Objects (3 of 3) 
You can make a logical test as to the type of a particular object using the instanceof operator:
	if (obj instanceof ClassName) {
		ClassName myBike = (ClassName)obj; 
	}
 
The test x instanceof C does not generate an exception if x is null. It simply returns false.
Описание слайда:
Casting Objects (3 of 3) You can make a logical test as to the type of a particular object using the instanceof operator: if (obj instanceof ClassName) { ClassName myBike = (ClassName)obj; } The test x instanceof C does not generate an exception if x is null. It simply returns false.

Слайд 27





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 28





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 29





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 30





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 31





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 32





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 33





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 34





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void doSomething(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void doSomething(){ System.out.println(2 * v1); }

Слайд 35





Overriding Instance Methods I
An instance method in a subclass with the same signature and return type as an instance method in the superclass overrides the superclass's method
The overriding method has the same name, number and type of parameters, and return type as the method it overrides.
An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.
Описание слайда:
Overriding Instance Methods I An instance method in a subclass with the same signature and return type as an instance method in the superclass overrides the superclass's method The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.

Слайд 36





Overriding Instance Methods II
When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass.
The access specifier for an overriding method can allow more, but not less, access than the overridden method (protected to public, but not to private)
Описание слайда:
Overriding Instance Methods II When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. The access specifier for an overriding method can allow more, but not less, access than the overridden method (protected to public, but not to private)

Слайд 37





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void printV1(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void printV1(){ System.out.println(2 * v1); }

Слайд 38





What will be the output?
class A{
   int v1 = 8;
   protected void printV1(){
 	  System.out.println(v1);
  }
}
class B extends A{
   public void printV1(){
	 System.out.println(2 * v1);
}
Описание слайда:
What will be the output? class A{ int v1 = 8; protected void printV1(){ System.out.println(v1); } } class B extends A{ public void printV1(){ System.out.println(2 * v1); }

Слайд 39





Hiding Static Methods (1 of 6)
public class Animal { 
	public static void testClassMethod() { 
		System.out.println("The class method in Animal."); 
	} 
	public void testInstanceMethod() { 
		System.out.println("The instance  method in Animal."); 
	} 
}
Описание слайда:
Hiding Static Methods (1 of 6) public class Animal { public static void testClassMethod() { System.out.println("The class method in Animal."); } public void testInstanceMethod() { System.out.println("The instance method in Animal."); } }

Слайд 40





Hiding Static Methods (2 of 6)
public class Cat extends Animal { 
	public static void testClassMethod() { 
		System.out.println("The class method in Cat."); 
	} 
	public void testInstanceMethod() { 
		System.out.println("The instance method in Cat."); 
	} 
}
Описание слайда:
Hiding Static Methods (2 of 6) public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method in Cat."); } public void testInstanceMethod() { System.out.println("The instance method in Cat."); } }

Слайд 41





Hiding Static Methods (3 of 6)
public static void main(String[] args) { 
	Animal myAnimal = new Animal();
   Animal myAnimalCat = new Cat();
   Cat myCat = new Cat();
	myAnimal.testInstanceMethod(); 
   myAnimalCat.testInstanceMethod();
   myCat.testInstanceMethod();
}
Описание слайда:
Hiding Static Methods (3 of 6) public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myAnimalCat = new Cat(); Cat myCat = new Cat(); myAnimal.testInstanceMethod(); myAnimalCat.testInstanceMethod(); myCat.testInstanceMethod(); }

Слайд 42





Hiding Static Methods (4 of 6)
Output:
The instance  method in Animal
The instance method in Cat
The instance method in Cat
Описание слайда:
Hiding Static Methods (4 of 6) Output: The instance method in Animal The instance method in Cat The instance method in Cat

Слайд 43





Hiding Static Methods (5 of 6)
public static void main(String[] args) { 
	Animal myAnimal = new Animal();
   Animal myAnimalCat = new Cat();
   Cat myCat = new Cat();
	myAnimal.testClassMethod(); 
   myAnimalCat.testClassMethod();
   myCat. testClassMethod();
}
Описание слайда:
Hiding Static Methods (5 of 6) public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myAnimalCat = new Cat(); Cat myCat = new Cat(); myAnimal.testClassMethod(); myAnimalCat.testClassMethod(); myCat. testClassMethod(); }

Слайд 44





Hiding Static Methods (6 of 6)
Output:
The class method in Animal.
The class method in Animal.
The class method in Cat.
Описание слайда:
Hiding Static Methods (6 of 6) Output: The class method in Animal. The class method in Animal. The class method in Cat.

Слайд 45





Polymorphism (1 of 2)
Connecting a method call to a method body is called binding
When binding is performed before the program is run (e.g. by the compiler), it’s called early binding. 
Late binding means that the binding occurs at run time, based on the type of object
There must be some mechanism to determine the type of the object at run time and to call the appropriate method
Описание слайда:
Polymorphism (1 of 2) Connecting a method call to a method body is called binding When binding is performed before the program is run (e.g. by the compiler), it’s called early binding. Late binding means that the binding occurs at run time, based on the type of object There must be some mechanism to determine the type of the object at run time and to call the appropriate method

Слайд 46





Polymorphism (2 of 2)
All method binding in Java uses late binding unless the method is static or final (private methods are implicitly final)
You can write your code to talk to the base class and know that all the derived-class cases will work correctly using the same code
Typical example: create an array of Base class and fill it with subclasses objects. Then you can call the same method for each object from array elements
Описание слайда:
Polymorphism (2 of 2) All method binding in Java uses late binding unless the method is static or final (private methods are implicitly final) You can write your code to talk to the base class and know that all the derived-class cases will work correctly using the same code Typical example: create an array of Base class and fill it with subclasses objects. Then you can call the same method for each object from array elements

Слайд 47





Exercise 4.4.2
Create a deposit array of different types and calculate sum of their interest values
Описание слайда:
Exercise 4.4.2 Create a deposit array of different types and calculate sum of their interest values

Слайд 48





Exercise: Interest Values Sum
Date start = new GregorianCalendar(2013, Calendar.SEPTEMBER, 8).getTime();
DepoBase[] depo = new DepoBase[6];
depo[0] = new DepoSimple(start, 20, 1000.0, 15.0);
depo[1] = new DepoSimple(start, 20, 2500.0, 18.0);
depo[2] = new DepoBarrier(start, 40, 15000.0, 11.5);
depo[3] = new DepoBarrier(start, 80, 5000.0, 14.0);
depo[4] = new DepoMonthCapitalize(start, 180, 2000.0, 16.5);
depo[5] = new DepoMonthCapitalize(start, 91, 40000.0, 12.1);
Описание слайда:
Exercise: Interest Values Sum Date start = new GregorianCalendar(2013, Calendar.SEPTEMBER, 8).getTime(); DepoBase[] depo = new DepoBase[6]; depo[0] = new DepoSimple(start, 20, 1000.0, 15.0); depo[1] = new DepoSimple(start, 20, 2500.0, 18.0); depo[2] = new DepoBarrier(start, 40, 15000.0, 11.5); depo[3] = new DepoBarrier(start, 80, 5000.0, 14.0); depo[4] = new DepoMonthCapitalize(start, 180, 2000.0, 16.5); depo[5] = new DepoMonthCapitalize(start, 91, 40000.0, 12.1);

Слайд 49





Exercise: Interest Values Sum
double sum = 0.0;
	for(DepoBase d: depo) sum += d.getInterest();
	sum = Math.round(sum * 100) / 100.0;
	if (sum == 1763.41) System.out.println("Test is true");
	else System.out.println("Test failed");
}
Описание слайда:
Exercise: Interest Values Sum double sum = 0.0; for(DepoBase d: depo) sum += d.getInterest(); sum = Math.round(sum * 100) / 100.0; if (sum == 1763.41) System.out.println("Test is true"); else System.out.println("Test failed"); }

Слайд 50





Exercise : Interest Values Sum
See 442InterestSum or 442aInterestSum  project for the full text
Описание слайда:
Exercise : Interest Values Sum See 442InterestSum or 442aInterestSum project for the full text

Слайд 51





Hiding Fields
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different
Hided field in the superclass can be accessed through super keyword
Hiding fields is not recommended as it makes code difficult to read
Описание слайда:
Hiding Fields Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different Hided field in the superclass can be accessed through super keyword Hiding fields is not recommended as it makes code difficult to read

Слайд 52





Subclass Constructors (1 of 2)
The syntax for calling a superclass constructor is
		super();   or: super(parameter list); 
Invocation of a superclass constructor must be the first line in the subclass constructor.
Описание слайда:
Subclass Constructors (1 of 2) The syntax for calling a superclass constructor is super(); or: super(parameter list); Invocation of a superclass constructor must be the first line in the subclass constructor.

Слайд 53





Subclass Constructors (2 of 2)
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass
If the super class does not have a no-argument constructor, you will get a compile-time error
Описание слайда:
Subclass Constructors (2 of 2) If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass If the super class does not have a no-argument constructor, you will get a compile-time error

Слайд 54





Accessing Superclass Members
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super
Описание слайда:
Accessing Superclass Members If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super

Слайд 55





Writing Final Methods
You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses
You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object
Methods called from constructors should generally be declared final
If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results
Описание слайда:
Writing Final Methods You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object Methods called from constructors should generally be declared final If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results

Слайд 56





Final Classes
You can declare an entire class final
A class that is declared final cannot be subclassed
This is particularly useful, for example, when creating an immutable class like the String class.
Описание слайда:
Final Classes You can declare an entire class final A class that is declared final cannot be subclassed This is particularly useful, for example, when creating an immutable class like the String class.

Слайд 57





Manuals
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Описание слайда:
Manuals http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html



Похожие презентации
Mypresentation.ru
Загрузить презентацию