Classes, Objects, Inheritance, Abstract Classes and Interfaces 1 Constructors must have the same name as the class itself The get and set methods are used to read and in a subclass, the method defined in the superclass is hidden
Previous PDF | Next PDF |
[PDF] Abstract Superclass
An abstract method defines the method name and arguments, but there's no method code Methods that subclasses should implement are declared abstract Instances of the The java drawing class JComponent is an example of this sort of common superclass with Get our RT class name -- just showing off // some of
[PDF] Abstract Classes and Interfaces Abstract Classes
6 août 2013 · 1 Abstract Classes • A Java class that cannot be instantiated, but instead Beer is a very general name for lots of different types of Concrete subclass is required to implement the abstract methods beersOnTap get(i)
[PDF] Java Classes, Objects, Inheritance, Abstract and Interfaces Recap 2
Classes, Objects, Inheritance, Abstract Classes and Interfaces 1 Constructors must have the same name as the class itself The get and set methods are used to read and in a subclass, the method defined in the superclass is hidden
[PDF] Chapter 4 Inheritance in Java
another class is called derived class, child class or subclass access class subclass_name extends superclass_name { abstract class {
Solutions to Exercises
The Java platform promotes portability by providing an abstraction over name or parameter's name with this or the class name followed by the member access access to the subclass's overriding method's access could be made private,
[PDF] Chapter 11 Abstract Classes and Interfaces
GeometricObject1 java +get Radius(): doubl e class ▻ If a subclass of an abstract superclass does not implement all the The interface name and the
[PDF] Chapter 7 Inheritance and Abstract Classes - GMU CS Department
hierarchy of creating separate classes, using fields to get some aggregation going, but We can say that the Student class is a subclass of the Person class, or that We get a complaint from the Java compiler that name and age are private
[PDF] CS200: Advanced OO in Java interfaces, inheritance, abstract
Object can shield variables from external access abstract public class abstract- base-class-name { Some subclass is required to override the abstract
[PDF] 1 Chapter 10 Topics Inheritance Concepts A Sample Vehicle
Abstract Classes and Methods See Example 10 3 CheckingAccountClient java (next slide) Home CheckingAccount class cannot directly access balance name yes protected fields yes yes, by calling method from subclass methods yes
Final and Abstract Classes Restricting Inheritance Abstract Classes
This is achieved in Java by using the keyword final as follows: final class Marks The abstract methods of an abstract class must be defined in its subclass £ We cannot declare example marks (test1 and test 2 marks) and access methods } class Results multiple methods which have the same name, but different
[PDF] java abstract class return subclass
[PDF] java abstract class return subclass type
[PDF] java abstract class with example
[PDF] java access resources from jar
[PDF] java add edit delete sample program
[PDF] java advanced features and programming techniques
[PDF] java api tutorial for beginners pdf
[PDF] java application development tutorial
[PDF] java architect interview questions
[PDF] java array exercises with solutions pdf
[PDF] java array pointer
[PDF] java array programs exercise
[PDF] java arrays
[PDF] java assignments on collections
CSE260, Computer Science B: Honors
Stony Brook University
http://www.cs.stonybrook.edu/~cse260Recapitulate CSE160: Java Classes, Objects,
Inheritance, Abstract Classes and Interfaces
1 (c) Paul Fodor (CS Stony Brook) & PearsonObjectives
To refresh information from CSE160 about
classes, objects, inheritance, abstract classes and interfaces 2 (c) Paul Fodor (CS Stony Brook) & PearsonOO Programming Concepts
An object represents an entity in the real world that can be distinctly identified.An object has a unique state and behaviors
the state of an object consists of a set of data fields (properties) with their current values the behavior of an object is defined by a set of methods 3Class Name: Circle
Data Fields:
radius is _______Methods:
getArea()Circle Object 1
Data Fields:
radius is 1Circle Object 2
Data Fields:
radius is 25Circle Object 3
Data Fields:
radius is 125A class template
Three objects of
the Circle class (c) Paul Fodor (CS Stony Brook) & PearsonClasses
Classes are templates that define objects of
the same type.A Java class uses:
variables to define data fields and methods to define behaviorsA class provides a special type of methods
called constructorswhich are invoked to construct objects from the class 4 (c) Paul Fodor (CS Stony Brook) & Pearson5 class Circle { /** The radius of this circle */ private double radius = 1.0; /** Construct a circle object */ public Circle() { /** Construct a circle object */ public Circle(double newRadius) { radius = newRadius; /** Return the area of this circle */ public double getArea() { return radius * radius * 3.14159;Data field
Method
Constructors
Classes
(c) Paul Fodor (CS Stony Brook) & Pearson6 public class TestCircle { public static void main(String[] args) {Circle circle1 = new Circle();
Circle circle2 = new Circle(25);
Circle circle3 = new Circle(125);
System.out.println( circle1.getArea() );
System.out.println( circle2.getArea() );
System.out.println( circle3.getArea() );
//System.out.println( circle1.radius ); //System.out.println( circle2.radius ); //System.out.println( circle3.radius );Classes
(c) Paul Fodor (CS Stony Brook) & Pearson7UML Class Diagram
Circle
radius: doubleCircle()
Circle(newRadius: double)
getArea(): double circle1: Circle radius = 1.0Class name
Data fields
Constructors and
methods circle2: Circle radius = 25 circle3: Circle radius = 125UML Class Diagram
UML notation
for objects (c) Paul Fodor (CS Stony Brook) & PearsonConstructors
Constructors must have the same name as the class itself. Constructors do not have a return typenot even void. Constructors are invoked using the newoperator when an object is created they initialize objects to reference variables:ClassNameo = new ClassName();
Example:
Circle myCircle= new Circle(5.0);
A class may be declared without constructors: a no-argdefault constructorwith an empty body is implicitly declared in the class 8 (c) Paul Fodor (CS Stony Brook) & PearsonAccessing Objects
objectRefVar.dataExample:myCircle.radius
objectRefVar.methodName(arguments)Example:myCircle.getArea()
9 (c) Paul Fodor (CS Stony Brook) & Pearson10Default values
public class Test { public static void main(String[] args) { intx; // x has no default valueString y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}Compilation error: the variables are not initializedBUT it assigns default values to data fields!
Java assigns no default value to a local variable inside a method. (c) Paul Fodor (CS Stony Brook) & Pearson11Reference Data Fields
The data fields can also be of reference types
Example:
public class Student {String name; // name has default value null
intage; // age has default value 0 booleanisScienceMajor; // isScienceMajorhas default value false char gender; // c has default value '\u0000' If a data field of a reference type does not reference any object, the data field holds a special literal value: null. public class Test { public static void main(String[] args) {Student student= new Student();
System.out.println("name? " + student.name); // nullSystem.out.println("age? " + student.age); // 0
System.out.println("isScienceMajor? " + student.isScienceMajor); // false System.out.println("gender? " + student.gender); // (c) Paul Fodor (CS Stony Brook) & Pearson12Differences between Variables of
Primitive Data Types and Object Types
1 Primitive type int i = 1 i
Object type Circle c c reference
Created using new Circle()
c: Circle radius = 1 (c) Paul Fodor (CS Stony Brook) & Pearson13Copying Variables of Primitive Data
Types and Object Types
iPrimitive type assignment i = j
Before:
1 j 2 iAfter:
2 j 2 c1Object type assignment c1 = c2
Before:
c2 c1After:
c2 c1: Circle radius = 5 c2: Circle radius = 9 c1: Circle radius = 5 c2: Circle radius = 9 -The object previously referenced by c1 is no longer referenced it is called garbage -Garbage is automatically collected by JVM = garbage collection (c) Paul Fodor (CS Stony Brook) & Pearson static vs. non-staticStatic methods & variables are scoped to a
class one static variable for all objects to share!Non-static (object) methods & variables
are scoped to a single object each object owns its non-static methods & variables 14 (c) Paul Fodor (CS Stony Brook) & Pearson15Static Variables,
Constants and Methods
Circle
-radius: double -numberOfObjects: int +getNumberOfObjects(): int +getArea(): double1 radius
circle1 radius = 1 numberOfObjects = 2 instantiate instantiateMemory
25 radius
numberOfObjectsUML Notation:
+: public variables or methods underline: static variables or methods circle2 radius = 5 numberOfObjects = 2After two Circle
objects were created, numberOfObjects is 2. (c) Paul Fodor (CS Stony Brook) & Pearson public class StaticExample{ public intnonStaticCounter= 0; public static intstaticCounter= 0; public StaticExample() { nonStaticCounter++; staticCounter++; public static void main(String[] args) {StaticExampleex;
ex = new StaticExample(); ex = new StaticExample(); ex = new StaticExample();System.out.println(ex.nonStaticCounter);
System.out.println(staticCounter);
16StaticExample.java
(c) Paul Fodor (CS Stony Brook) & Pearson public class StaticExample{ public intnonStaticCounter= 0; public static intstaticCounter= 0; public StaticExample() { nonStaticCounter++; staticCounter++; public static void main(String[] args) {StaticExampleex;
ex = new StaticExample(); ex = new StaticExample(); ex = new StaticExample();System.out.println(ex.nonStaticCounter);
System.out.println(staticCounter);
17StaticExample.java
Output: 1
3 (c) Paul Fodor (CS Stony Brook) & Pearson staticusageCan a staticmethod:
-staticmethod in the same class? directly call a staticmethod in the same class? directly reference a non-staticvariable in the same class? directly reference a staticvariable in the same class?Can a non-staticmethod:
-staticmethod in the same class? directly call a staticmethod in the same class? directly reference a non-staticvariable in the same class? directly reference a staticvariable in the same class? 18 (c) Paul Fodor (CS Stony Brook) & Pearson staticusageCan a staticmethod:
-staticmethod in the same class? No directly call a staticmethod in the same class? Yes directly reference a non-staticvariable in the same class? No directly reference a staticvariable in the same class? YesCan a non-staticmethod:
-staticmethod in the same class? Yes directly call a staticmethod in the same class? Yes directly reference a non-staticvariable in the same class? Yes directly reference a staticvariable in the same class? Yes 19 (c) Paul Fodor (CS Stony Brook) & Pearson1 public class Nothing {
2private intnada;// Errors?
3private static intnothing;
45public void doNada(){ System.out.println(nada);}
6public static void doNothing(){ System.out.println("NOTHING"); }
78public static void myStaticMethod(){
9doNada();
10doNothing();
11nada = 2;
12nothing = 2;
13Nothing n = new Nothing();
14n.doNada();
15n.nada= 2;
16n.nothing= 6;
17}18public void myNonStaticMethod() {
19doNada();
20doNothing();
21nada = 2;
22nothing = 2;
23Nothing n = new Nothing();
24n.doNada();
25n.nada= 2;
26}}20
(c) Paul Fodor (CS Stony Brook) & Pearson1 public class Nothing {
2private intnada;
3private static intnothing;
45public void doNada(){ System.out.println(nada);}
6public static void doNothing(){ System.out.println("NOTHING"); }
78public static void myStaticMethod(){
9doNada();
10doNothing();
11nada = 2;
12nothing = 2;
13Nothing n = new Nothing();
14n.doNada();
15n.nada= 2;
16n.nothing= 6;
17}18public void myNonStaticMethod() {
19doNada();
20doNothing();
21nada = 2;
22nothing = 2;
23Nothing n = new Nothing();
24n.doNada();
25n.nada= 2;
26}}21
(c) Paul Fodor (CS Stony Brook) & PearsonVisibility Modifiers and
Accessor/MutatorMethods
By default, the class, variable, or method can be
accessed by any class in the same package. 22public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class -To protect data!
The get and set methods are used to read and
modify private properties. (c) Paul Fodor (CS Stony Brook) & Pearson23Array of Objects
Circle[] circleArray= new Circle[10];
An array of objects is an array of reference variables (like the multi-dimensional arrays seen before) referenceCircle object 0 circleArray[0]
circleArray circleArray[1] circleArray[9]Circle object 9
Circle object 1
(c) Paul Fodor (CS Stony Brook) & PearsonImmutable Objects and Classes
Immutable object: the contents of an object cannot be changed once the object is created -its class is called an immutable class Example immutable class: no set method in the Circle class public class Circle{ private double radius; public Circle() { } public Circle(double radius) { this.radius= radius; public double getRadius() { return radius; radius is private and cannot be changed without a set method A class with all private data fields and without mutatorsis not necessarily immutable!24 (c) Paul Fodor (CS Stony Brook) & PearsonWhat Class is Immutable?
1.It must mark all data fields private!
2.Provide no mutator methods!
3.Provide no accessor methods that would
return a reference to a mutable data field object! 25(c) Paul Fodor (CS Stony Brook) & Pearson26
Example mutable
public class Student { private intid; privateBirthDatebirthDate; public Student(intssn, intyear, intmonth, intday) { id = ssn; birthDate= new BirthDate(year, month, day); public intgetId() { return id; public BirthDategetBirthDate() { return birthDate; public class BirthDate{ private intyear; private intmonth; private intday; public BirthDate(intnewYear, intnewMonth, intnewDay) { year = newYear; month = newMonth; day = newDay; public void setYear(intnewYear) { year = newYear; public intgetYear() { return year; public class Test { public static void main(String[] args) { Student student= new Student(111223333, 1998, 1, 1);BirthDatedate = student.getBirthDate();
date.setYear(2050); // Now the student birth year is changed: System.out.println(student.getBirthDate().getYear());// 2050 (c) Paul Fodor (CS Stony Brook) & PearsonThe thisKeyword
The thiskeyword is the name of a
reference that refers to an object itselfCommon uses of the thiskeyword:
1.2.To enable a constructor to invoke another
constructor of the same class as the first statement in the constructor. 27(c) Paul Fodor (CS Stony Brook) & Pearson28