Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
15-Jun-15 Abstract Classes and Interfaces. 2 Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
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 and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Object-Oriented Programming: Polymorphism Chapter 10.
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.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
UNIT-2. Inheritance –Definition Single Inheritance Benefits of inheritance Member access rules super classes Polymorphism Method overriding Using final.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Inheritance.
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Web Design & Development Lecture 9
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Inheritance-Basics.
Final and Abstract Classes
Inheritance and Polymorphism
Interface.
Object Oriented Programming
Modern Programming Tools And Techniques-I Inheritance
Abstract classes and interfaces
Interface.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Abstract classes and interfaces
Final and Abstract Classes
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Lecture 21 - Abstract Classes and Interface

Example Figure –Rectangle –Triangle Figure –Dimensions –Area

Figure Class public class Figure { protected double dim1; protected double dim2; public Figure (double firstD, double secondD) { dim1 = firstD; dim2 = secondD; } public double area() { system.out.println(“Area here is undefined”); return 0; }

Class Rectangle public class Rectangle extends Figure { Rectangle (double firstD, double secondD) { super (firstD, secondD); } public double area () { return dim1*dim2; }

Class Triangle public class Triangle extends Figure { Triangle (double firstD, double secondD) { super (firstD, secondD); } public double area () { return (dim1*dim2)/2; }

Class FindAreas public class FindAreas { public static void main(String args[]) { Figure f = new Figure(10,10); Rectangle r = new Rectangle (2,5); Triangle t = new Triangle (3,4); Figure figref; figref =f; f.area(); // print area of figure figref = r; r. area(); // print area of rectangle figref = t; t.area();// print area of triangle }

Abstract Classes Superclasses just provide generalization Subclasses must complete implementation to be more meaningful. One way: Print a warning message. Abstract classes: More elegant and ensure that subclass implements it.

Figure class public abstract class Figure { double dim1; double dim2; Figure (firstD, secondD) { dim1 = firstD; dim2 = secondD; } public abstract double area(); }

Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. Abstract methods make the class abstract. Abstract classes cannot be instantiated. Concrete subclasses complete the implementation.

Class FindAreas public class FindAreas { public static void main(String args[]) { Figure f = new Figure(10,10); Rectangle r = new Rectangle (2,5); Triangle t = new Triangle (3,4); Figure figref; figref =f; f.area(); figref = r; r. area(); // print area of rectangle figref = t; t.area();// print area of triangle }

Using final public class A { final void aMethod() { } } public class B extends A { public void aMethod() { } // Error can’t override } Using final prevents the method to be overridden.

Final Prevents Inheritance public final class A { // … } public class B extends A // Error can’t subclass A { }

Interfaces public interface MyInterface { void method1 ( ); void method2 ( ); void method3 ( ); } Access is generally public If the interface is public, all its members are implicitly public Notice the keyword interface and complete lack of method bodies.

Interfaces An interface defines a type, just like classes do. They tell the computer what classes that implement the interface will do. They do not tell the computer how those classes will perform those tasks. Unrelated classes in terms of class hierarchy can have same interface. Using this you can fully abstract a class’s interface from its implementation. Any number of classes can implement an interface Also, a class can have more than one interface

class MyClass implements MyInterface, MyInterface2… { // must implement all the methods declared by Interface }

Abstract Class vs. Interface An abstract class can have fields and other methods that have bodies. Interface cannot have either of those.

Which to Choose When a partial implementation is feasible, abstract classes make sense as they can provide some functionality with the methods. Pure abstract classes (with all abstract methods) in Java are functionally equivalent to an interface, but restricted to single inheritance. Java will allow you to implement more than one interface. You can use access modifiers (protected, package, etc) in an abstract class though. Interfaces are always public.