When is a class not a class? When it is either abstract or an Interface.

Slides:



Advertisements
Similar presentations
24-Aug-14 Abstract Classes and Interfaces. Java is “safer” than Python Python is very dynamic—classes and methods can be added, modified, and deleted.
Advertisements

Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
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.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
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.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Multiple Choice Solutions True/False a c b e d   T F.
Abstract classes and Interfaces. Abstract classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
C# G 1 CSC 298 Object Oriented Programming Part 2.
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.
CIT 590 Intro to Programming Lecture 13. Agenda A few topics that you have seen but might not have fully grasped Static Public, private, protected etc.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
A Introduction to Computing II Lecture 3: Interfaces and Inheritance Fall Session 2000.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
BY:- TOPS Technologies
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Objects as a programming concept
Interfaces.
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Chapter 10 Thinking in Objects
Interfaces.
Class Inheritance (Cont.)
Interface.
Java Inheritance.
Conditional Statements
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Chapter 9 Carrano Chapter 10 Small Java
Workshop for Programming And Systems Management Teachers
Abstract classes and interfaces
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Topics OOP Review Inheritance Review Abstract Classes
Java Inheritance.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

When is a class not a class? When it is either abstract or an Interface

As the name indicates, an abstract class is abstract. If we think of a class like a blueprint, an abstract class is like a blueprint with a hole in it to be filled in by the Child classes. Something it can be difficult to decide whether a class is abstract but the question to ask is whether a method in the class is not well defined and whether you want instances of that class in the program.

Animal – While animals do exist, we never think of there just being an animal. Someone would never tell you that they adopted an animal Vehicle – How does a vehicle move? You would need a more specific type of vehicle to define that method such as a car, plane, or ship Electronic Device – What happens when you turn the device on? All electronic devices can be turned on but it all depends on what the specific device is.

You can make a class abstract by declaring it in the following way: public abstract class MyClass You can do the same to a method by declaring it in the following way: public abstract void myMethod( ); Notice that there is no code provided for the abstract method. Once you make a method abstract then the entire class is made abstract.

If a class extends an abstract class then it will have to define each abstract method in the parent class, unless it itself is abstract.

public class Parent{ protected int x; public Parent(int x){ this.x = x; } public abstract void doIt(int n){ }

public class Child extends Parent{ private int a; public Child(int a, int x){ super(x); this.a = a; } public void doIt(int n){ a *= n; x += n; }

Notice that the Parent class was not defined as abstract but the doIt method was so the entire Parent class by default becomes abstract. The Child class defined the doIt method that was the abstract method in the Parent class.

JAVA does not allow multiple inheritance where a class extends more than one other class. JAVA will though allow you to cheat and say that you implement multiple Interfaces. An Interface is essentially an abstract class where every single method is abstract and it contains no variables. This prevents conflict between variables and methods that exist in both parent classes.

No variables can be declared in an Interface No methods can have code in them in an Interface It is said that a class implements and interface, not that it extends it (the keyword implements is used)

public interface Parent { //notice the semicolons at //the ends of these signatures void method1( ); void method2( ); int method3(double d); }

public class Child implements Parent { public int statevar1; public void method1( ) { //some code…} public void method2( ) { //some code…} public int method3(double c ) { //some code…} //some other methods… }

When a class implements an Interface then it must declare each and every method in the Interface even if they do nothing (they have to be at least defined as doing nothing) A class can only extend one other class but can implement an infinite number of Interfaces. For example: public class Game extends JPanel implements MouseListener, ActionListener, KeyListener

You can declare an Interface or a Abstract Class or a Data Structure containing Interfaces or an Abstract Class just like any regular class. In this example, assume that Animal is an Abstract Class and Dog and Cat are classes that extend it: Animal rex = new Dog(); ArrayList zoo = new ArrayList (); zoo.add(new Cat()); The same could would work if Animal is an Interface and Dog and Cat implement it.