Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Abstract Classes - Lecture Slides - Computer Science II | CS 162, Papers of Computer Science

Material Type: Paper; Class: INTRODUCTION TO COMPUTER SCIENCE II; Subject: Computer Science; University: Oregon State University; Term: Fall 2007;

Typology: Papers

Pre 2010

Uploaded on 08/31/2009

koofers-user-780
koofers-user-780 🇺🇸

10 documents

1 / 2

Toggle sidebar

Related documents


Partial preview of the text

Download Abstract Classes - Lecture Slides - Computer Science II | CS 162 and more Papers Computer Science in PDF only on Docsity! 1 1 CS162: Introduction to Computer Science II Abstract Classes 2 Abstract classes 3 Abstract Classes • An abstract class represents a generic concept eg. shape • An abstract class cannot be instantiated • Use the modifier abstract on the class header to declare a class as abstract, e.g.: public abstract class Shape{ // } 4 Abstract Classes • An abstract class often contains abstract methods with no definitions – The abstract modifier must be applied to each abstract method – Forces subclass to redefine the abstract method – An abstract class typically contains some non- abstract methods (with definitions) • A class declared as abstract is not required to contain abstract methods 5 Abstract Class Example public abstract class Shape { private String shapeName = ”undefined”; public abstract double area(); // no definition code public void setName(String name) { shapeName = name; } public String getName() { return shapeName; } } Note: You can’t instantiate a shape class because it is an abstract class! 6 Abstract Classes • A child of an abstract class must override the abstract methods of the parent – if you inherit from an abstract class but you don’t define the abstract functions, then the subclass must be abstract. • An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved