Written by: Dr. JJ Shepherd

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Written by: Dr. JJ Shepherd
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.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
The Object-Oriented Thought Process Chapter 03
Lecture 8 D&D Chapter 9 Inheritance Date.
Inheritance and Polymorphism
Packages, Interfaces & Exception Handling
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Inheritance, Polymorphism, and Interfaces. Oh My
null, true, and false are also reserved.
Interface.
Object-oriented Design in Processing
Creating Objects in a Few Simple Steps
Inheritance, Polymorphism, and Interfaces. Oh My
Java Programming Course
Object-oriented Design in Processing
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
JAVA CLASSES.
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
CIS 199 Final Review.
Object-oriented Design in Processing
Dr. R Z Khan Handout-3 Classes
Classes and Objects CGS3416 Spring 2019.
CMSC 202 Exceptions.
Object-oriented Design in Processing
CSG2H3 Object Oriented Programming
Presentation transcript:

Written by: Dr. JJ Shepherd CSCE 146 Hey Remember Java? Written by: Dr. JJ Shepherd

Classes Blueprints to create objects Remember the steps Define the class Instance variables Constructors Accessors and Mutators Additional Methods

Classes Defining the class Concept public class <<name>> { <<body of the class>> }

Classes Instance variables are the attributes of the class Concept public class <<name>> { private <<type>> <<name>>; … }

Classes Constructors constructs an instance of a class in memory and sets the instance variables to a value Default constructors have no parameters and set everything to a default value public <<name of the class>>() { <<set all instance variables>> }

Classes Parameterized Constructors have parameters that set one or all of the instance variables to a value Concept public <<name of the class>>(<<parameters>>) { <<set instance variables to parameters>> }

Classes Mutators are used to modify instance variables in a class Adds a layer of protection by validating values Concept public void <<set variable name>>(<<parameter>>) { this.<<variable>> = <<parameter>>; }

Classes Accessors are used to get values of instance variables outside of the class Concept public <<type>> <<get variable name>>() { return this.<<variable>>; }

Classes Methods are behaviors of classes Used internally and externally Accessors and mutators are methods Concept <<scope>> <<return value>> <<name>> (<<parameters>>) { <<body of the method>> }

Classes Static methods are static in memory Used as helper methods that exist outside of one particular instance <<scope>> static <<return value>> <<name>> (<<parameters>>) { <<body of the static method>> }

Inheritance Used to take the attributes and methods from a parent (super) class Concept public class <<name>> extends <<parent name>> { <<body of the class>> }

Inheritance In inherited constructors need to all the parent’s constructor by calling “super” Concept To call inherited methods use “super.” super(<<parameters>>); super.<<method name>>;

Interfaces Used as a blueprint to create classes. Classes are a blueprint to create instance of objects Only has method definitions Concept public interface <<name>> { <<method definition>>; … }

Interfaces Interfaces are the building concept of polymorphism To use an interface used “implements” Concept public class <<name>> implements <<interface name>> { <<body of the class>> }

Exceptions Gracefully allows programs to crash and an exceptional event happens To create exceptions extend the/an exception class Then call the parent’s constructors

Exceptions Concept public <<name>> extends <<Exception>> { public <<name>>(<<parameters>>) super(<<parameters>>) }

Exceptions Methods that can throw exceptions need to check for those cases Concept <<scope>> <<return>> <<name>> (<<parameters>>) throws <<Exceptions>> { … throw new <<exception name>>(); }

Exceptions If a method throws an exception it must be called in a try catch block that handles the exception Concept try { <<method that throws exception>>; } catch(<<exception name>> e) System.out.println(e.getMessage());

Example