Download presentation
Presentation is loading. Please wait.
1
Object Types Primitive types Primitive Vs Object Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint Type = Set of operations ALoan AnotherLoan
2
Structure Types Atomic types Structure Vs Atomic Types Types Classes ABMISpreadsheet AnotherBMISpreadsheet Interfaces BMISpreadsheet Loan Stringdoubleint ALoan AnotherLoan Instances of structure type decomposed into one or more smaller values
3
Physical Vs. Logical Structure ALoan instance double principalALoan instance double Principal Yearly Interest Monthly Interest double Physical Logical
4
Programmer-defined Vs Predefined Types Programmer-defined interface/class (Loan/ALoan) is programmer-defined type. Programmer-defined types in Java must be object types Some object types are predefined (String) All primitive types are predefined.
5
Initialization of Object Variables Loan carLoan = new ALoan() Loan houseLoan = new AnotherLoan() Loan houseLoan = new Loan()
6
Programmed Instantiation new C(...) creates an instance of class C … is actual parameters. C is method and class name. cannot instantiate interface –new Loan() is illegal
7
Constructor Vs Regular Method public class ALoan implements Loan{ int principal; public int getPrincipal() { return principal; } public void setPrincipal(int newVal) { principal = newVal; } public int getYearlyInterest() { return principal*INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE; } public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { setYearlyInterest(newVal*12); } public ALoan(int initPrincipal) { setPrincipal(initPrincipal); } Combined type and method name Missing type name public ALoan ALoan(int initPrincipal) { setPrincipal(initPrincipal); } Not a constructor
8
Constructor Method that constructs a new object based on its parameters. –new ALoan(10000) Actually, just initializes object Object constructed by Java Cannot be declared in interface
9
Multi-Parameter Constructor public ALoanPair (Loan initCarLoan, Loan initHouseLoan) { setCarLoan(initCarLoan); setHouseLoan(initHouseLoan); } new ALoanPair(new ALoan(10000), new ALoan(10000)) Usually as many constructor parameters as are required to initialize independent instance variables.
10
Default Constructor public class ALoan implements Loan{ int principal; public int getPrincipal() { return principal; } public void setPrincipal(int newVal) { principal = newVal; } public int getYearlyInterest() { return principal*INTEREST_RATE/100; } public void setYearlyInterest(int newVal) { principal = newVal*100/INTEREST_RATE; } public int getMonthlyInterest() { return getYearlyInterest()/12; } public void setMonthlyInterest(int newVal) { setYearlyInterest(newVal*12); } public ALoan() { } Inserted In Object Code not in Source Code Default new ALoan() Invoking Default, Parameterless Constructor
11
Class Methods Methods can be invoked on class itself. Called class or static methods Declared in class on which they are invoked. Keyword static in header. Accesses no instance variable. Header cannot appear in interface.
12
External Call of Class Method ALoan.add(carLoan, houseLoan) Class as target Math.round(5.7)
13
Instance Vs Class Members Class Members –Class methods –Class variables Instance Members –Instance methods –Instance variables
14
Scope of Class and Instance Members Class Members –visible to other class members –visible to all instance members class & instance methods can access class variables class and instance methods can call class methods Instance Members –visible to other instance members –not visible to class members which of (zero to many) copies of an instance variable should a class member refer to?
15
public static int getNumLoans() { System.out.println(principal); return numInstances; } Legal & Illegal Accesses static int numInstances = 0; public ALoan(int initPrincipal) { setPrincipal(initPrincipal); numInstances = numInstances + 1; System.out.println(getNumLoans()); } int principal; public setPrincipal(int newPrincipal) { principal = newPrincipal; }
16
Class Vs Instance Methods public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Class MethodAccesses no instance variable public Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Instance Method ALoan.add(carLoan, houseLoan) Math.round(5.7) (new ALoan).add(carLoan, houseLoan) (new Math).round(5.7)
17
Class Vs Instance Method Instance method has all the privileges of a class method. Any class method can be made an instance method. Bad style to have instance method that does not access any instance variable. –They belong to the class. –Violate least privilege principle. –Require needless instantiation.
18
Polymorphic Methods public static Loan add(Loan loan1, Loan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } AnotherLoan Instance ALoan Instance Actual Parameters of different types Methods that take actual parameters of different types
19
Non Polymorphic Methods public static Loan add(ALoan loan1, ALoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } public static Loan add(AnotherLoan loan1, AnotherLoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } public static Loan add(ALoan loan1, AnotherLoan loan2) { return new ALoan(loan1.getPrincipal() + loan2.getPrincipal())); } Code Duplication
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.