CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Further abstraction techniques Abstract classes and interfaces 5.0.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Chapter 10: Introduction to Inheritance
Further abstraction techniques Abstract classes and interfaces 1.0.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Further abstraction techniques Abstract classes and interfaces 3.0.
Abstract Classes and Interfaces
CC1007NI: Further Programming Week Dhruba Sen Module Leader (Islington College)
CC1007NI: Further Programming Week 2 Dhruba Sen Module Leader (Islington College)
Lecture 3 Casting Abstract Classes and Methods Interfaces.
OOP (Java): Abstract/ OOP Objectives – –use a foxes-and-rabbits simulation to introduce abstract classes, interfaces, and multiple.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Abstract Classes. Review PA – 3 Design Considerations /Web/CS239/programs/pa3/draft.php ?harrisnlCS239
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Further abstraction techniques
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 COS 260 DAY 21 Tony Gauvin. 2 Agenda Questions? 8 th Mini Quiz corrected –Good results 9 Th Mini Quiz Today –40 min covering Chap 9 Assignment 5 Due.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
Further Abstraction Techniques Chapter 10. Abstract Classes There are times when you do not want someone to be able to make an object of your class. For.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Further abstraction techniques Abstract classes and interfaces 6.0.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
11 Further abstraction techniques
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Interfaces.
Java Programming Language
Abstract Classes AKEEL AHMED.
Week 6 Object-Oriented Programming (2): Polymorphism
Lecture 14- Abstract Classes
METHOD OVERRIDING in JAVA
Abstract Classes An abstract class is a kind of ghost class. It can pass along methods and variables but it can’t ever be instantiated itself. We can.
Abstract Classes Page
Java – Inheritance.
COS 260 DAY 19 Tony Gauvin.
Java Inheritance.
COS 260 DAY 23 Tony Gauvin.
Fundaments of Game Design
Object-Oriented Programming
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
COS 260 DAY 23 Tony Gauvin.
Further abstraction techniques
Presentation transcript:

CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)

What is Abstration? An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.

What is Abstration? If a class is abstract and cannot be instantiated, the class does not have much use unless it is subclassed (Inheritance). This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Why abstraction? If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

‘abstract’ Keyword Use ‘abstract’ keyword to declare a class or method as abstract. E.g. public abstract class Animal public abstract void eat()

Abstract methods Abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows: E.g. public abstract double computePay();

Abstract methods Declaring a method as abstract has two results: The class must also be declared abstract. If a class contains an abstract method, the class must be abstract as well. Any child class must either override the abstract method or declare itself abstract.

Abstract methods A child class that inherits an abstract method must override it. If they do not, they must be abstract, and any of their children must override it. Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.

Simulations Programs regularly used to simulate real-world activities They are often only partial simulations They often involve simplifications. –Greater detail has the potential to provide greater accuracy –Greater detail typically requires more resources

Predator-prey simulations There is often a delicate balance between species. A lot of prey means a lot of food. A lot of food encourages higher predator numbers. More predators eat more prey. Less prey means less food. Less food means...

The foxes-and-rabbits project

Main classes of interest Fox Simple model of a type of predator. Rabbit Simple model of a type of prey. Simulator Manages the overall simulation task. Holds a collection of foxes and rabbits.

Example of the visualization

Room for improvement Fox and Rabbit have strong similarities but do not have a common superclass. The update step involves similar-looking code. The Simulator is tightly coupled to specific classes. It ‘knows’ a lot about the behavior of foxes and rabbits.

The Animal superclass Place common fields in Animal : age, alive, location Method renaming to support information hiding: run and hunt become act. Simulator can now be significantly decoupled.

The act method of Animal Static type checking requires an act method in Animal. There is no obvious shared implementation. Define act as abstract: abstract public void act(List newAnimals);

Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. The presence of at least one abstract method makes the class abstract. Abstract classes cannot be instantiated. Concrete (i.e. non-abstract) subclasses complete the implementation.

The Animal class public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(List newAnimals); other methods omitted }

Review Abstract methods allow static type checking without requiring implementation. Abstract classes function as incomplete superclasses. No instances. Abstract classes support polymorphism.

THANK YOU.