Escolar Documentos
Profissional Documentos
Cultura Documentos
OOP Model
• Organization is centered around objects
rather than actions
• Central design of programs is based on
how data is defined and how it can be
manipulated rather than the sequential
logic of the program
• Java embraces this model as the core of
its design
Classes
• Everything in Java is a class (basically)
• A class defines a new data type and all of the properties
that an object of that type contains
– Fields (data members, instance variables, …)
– Methods (procedures to possibly manipulate the fields or provide
some other functionality)
• Each instance of a class has a copy of all non-static
fields and methods defined for that class
– Actually the method code is shared among all instances but
conceptually this is not important
• Only one copy of static fields and methods exists for
each class
Car
class
Car objects
• A class consists of data and methods.
Revisiting static
• static class members
– can be accessed from outside of its class through the use of the
class name
– can be accessed from inside any member class method with or
without the class name
• static methods
– Do not have access to non-static methods or fields (you will get
a compiler error if you try this)
– Makes sense since static members are not associated with any
particular object and exist even before any object of the class is
instantiated
Initializing data
• Three ways to initialize class member variables
– Right in the class body during variable declaration
class MyClass {
String myVariable = “foo”;
int a = 10;
}
– Constructors
Preetinder Singh Brar GJ-IMT 7
Internet Programming And Java MCA-502(N2) MCA-Semester III
Constructors
• A constructor is a special method used to initialize class
members with custom specified data during instantiation
• Declared with no return type and a name which is the
same as the class name
class MyClass {
MyClass(..parameters..)
{
//initialize data here
}
}
Method Overloading
Destructors
• No such thing!
• Java has a garbage collector that
automatically cleans up objects that are no
longer referenced in your program
• Each class also has a finalize() method
that is called when the object is garbage
collected
• You probably don’t want to do anything
with it
Packages
• Java has an additional level of support for organizing
your classes called packages
• You can put a class in a package by declaring the
package at the type of the file
package myPackage;
class MyClass {
MyClass(..parameters..)
{
//initialize data here
}
}
Modifiers
• Java has access attributes similar to C++
– No modifier
• Can be accessed by any class in the same package
– public
• Accessible by anyone
– private
• Can be accessed by any methods within the class. Note that this
includes a separate instantiation of the same class
– protected
• Can be accessed by any class in the same package and any
subclass in any package
Inheritance
• Sometimes when designing classes we run into the
following relationship
– Class1 is a more specialized form of Class2
• In this situation we do not want to duplicate all of the
functionality and attributes in Class1
• Instead we create Class2 as a subclass of Class1
• Class2 inherits all of the fields and methods provided in
Class1 and can also define additional functionality for its
specialized features
Inheritance
• We declare a class as a subclass of another through the
use of the extends keyword
class SubClass extends BaseClass{
…
}
Student.java
student_id, toString()
year, name year_group()
inherits (extends)
GradStudent.java
dept, toString()
thesis
Objects as Pictures
s1 gs1
student_id 100 student_id 200
year 1 year 4
name “Jane Doe” name “John Smith”
dept “Pharmacy”
Subclass Constructors
• Constructors of a subclass should always call one of the
constructors of the base class first
• This can be done with the super keyword
class SubClass extends BaseClass{
SubClass() {
super(..arguments..);
}
}
gs1
student_id 200
stud
year 4
name “John Smith”
dept “Pharmacy”
continued
• This means that methods in the GradStud
ent class cannot directly see or change
student_id, name, or year
– how can they be seen/changed?
• A better object diagram:
student_id 200
year 4
name “John Smith”
dept “Pharmacy”
gs1
thesis “Retail Thesis”
continued
2.5. Protected Variables
Method Overriding
• You can override any method of the base class by
declaring the method in your subclass with the same
signature (same return type, method name, and
parameter list)
• If you have overridden a base class method and wish to
access the base class version of it, you can again use
the super keyword
class SubClass extends BaseClass{
…
public String baseMethod() {
return super.baseMethod() + “\n”;
}
}
Polymorphism
• The power of OOP programming is realized through
polymorphism
• Say we wish to work with a list of user accounts in the
RPI CS department
• We have defined a class called UserAccount with two
subclasses
– StudentUserAccount
– ProfessorUserAccount
• Polymorphism allows us work with a list of UserAccounts
without knowing or caring what type of accounts they are
Polymorphism
• In Java we can assign a variable of a certain class type
an instance of that particular class or an instance of any
subclass of that class
UserAccount myAccount = new StudentUserAccount();
Student stud;
GradStudent g;
PostGradStudent p;
:
stud = new Student();
stud.toString(); // which toString()?
Abstract classes
• You can declare a class abstract with the abstract
keyword
public abstract class Animal {
public abstract void sound();
public Animal(String type) {
this.type = type;
}
public String toString() {
return “This is a “ + type;
}
private String type;
}
Abstract classes
abstract
Employee
extends
Boss Hourly
Worker
Commission
Piece
Worker
Worker
Notes
• Employee is an abstract class
– it contains no implementation for its ear
nings() function
– each subclass must implement it
Pre jdk1.1
• In jdk1.0, the clean and simple class rules were
ballyhooed as a major improvement over C++
• Addition of inner classes complicates things
significantly
• However, they do make certain code much less
awkard to write, particularly when writing GUIs
• Still, you do not have to use them, but they can
be quite cool and I do recommend it in
moderation!
• Inner classes
– local
• anonymous or named
– non-local
• named only
• Static nested classes
– non-local named only
Interfaces
• Takes the abstraction one step further
• Only defines a set of methods that represent the class
interface but does not provide an implementation
• Can also define constants
public interface Shape {
double PI = 3.14;
int Area();
String name();
}
Interfaces
• Java provides support for pseudo-multiple-inheritance
through the use of interfaces
– Classes can extend one and only one class
– However, classes can implement any number of interfaces
Interfaces
public Point() { x = 0; y = 0; }
:
// various methods
:
continued
Multiple Implements
continued
• Java does not have multiple inheritance
using extends, so interfaces are used in
stead.
• An interface is a guide
– it says what methods a subclass should im
plement in order to conform to the supercla
ss
Internet Programming And Java MCA-502(N2) MCA-Semester III
MacMenu WindowsMenu
Question: How can we work only with the abstract classes if we must
instantiate them using one of the concrete subclasses?