Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.

Similar presentations


Presentation on theme: "Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism."— Presentation transcript:

1 Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism

2 Class vs Instance Objects Class members –class fields –class methods Instance members –instance fields –instance methods

3 Class Members Class members are associated with the class in which they are defined –exist only once, a single copy –declared using the static keyword –class members are accessed using the class name ClassName.classMemberName

4 Class fields and Methods Class fields –a class field has the same value for all instances of the object –a class field declared with a final modifier is a constant static final double PI=3.14159; Class methods –a class method cannot use any instance members (fields or methods)

5 Instance members Instance members are associated with instances of the class –exist for each separate instance –copied as the object is instantiated –instance fields in difference instance objects can have different values

6 Example – class vs instance public class BankAccount{ // instance fields String name; double balance; // class field static double interestRate; // instance method public double getBalance(){ return balance; } // class method public static double getRate(){ return interestRate; }... }

7 Example – class vs instance Code to setup and use BankAccounts //instantiate a BankAccount BankAccount myAcc = new BankAccount(...); // access its instance field balance System.out.println(myAcc.getBalance()); // access its class field interestRate System.out.println(BankAccount.interestRate); default constructor

8 Method Overloading Defining methods within the same class with the same name but with different parameter lists Java compiler determines which method to call based on the parameter list Often used for constructors BankAccount(String name){ this.name=name; this.balance=0; } BankAccount(String name, double balance){ this.name=name; this.balance=balance; };

9 Inheritance Inheritance allows classes to be considered in a hierarchy with the data and methods of the parent (super) class being passed onto (inherited) by the child (sub) classes and not needed in their specifications implemented using the extends keyword e.g. class subClass extends superClass { … } an instance of subClass can also be treated as an instance of superClass

10 Object class Every class defined has a superclass If a superclass is not explicitly defined it is the superclass java.lang.Object Object is a special class –the only class that does not have a superclass –all Java classes inherit the methods of Object –instances of any class can be treated as an instance of Object –all arrays are also considered instances of Object

11 Inheritance example public class LoanAccount extends BankAccount{ double loanAmount; int loanPeriod;// no of yrs... }

12 Super Constructors are not inherited by subclasses A subclass should call the constructor of its superclass to perform the initialisation on the instance fields that the superclass is responsible for This is done using the super reference super(param1, param2,…) must always be the first line in the subclass constructor code

13 super Example public class BankAccount{ String name; double balance=0; public BankAccount (String name){ this.name=name; }... } public class LoanAccount extends BankAccount{ double loanAmount; int loanPeriod;// no of yrs public LoanAccount(name, amt, period){ super(name); this.loanAmount=amt; this.loanPeriod=period; }... }

14 Points about Inheritance Multiple Inheritance –a Java class may only extend from one superclass - multiple inheritance is not supported (see interfaces for work around) A class that is declared with the final modifier cannot be extended or subclassed

15 Points about Inheritance Working within a hierarchy –any object of a subclass can be assigned to an object of a superclass –any object of a superclass can be assigned to an object of a subclass with an appropriate cast BankAccount myAcc = new BankAccount(…); LoanAccount myLoan = new LoanAccount(…); myAcc = myLoan; // OK myLoan = myAcc // not OK myLoan = (LoanAccount) myAcc; // OK if(myAcc instanceOf LoanAccount) LoanAccount myLoan = (LoanAccount) myAcc; // better

16 Shadowing Fields A shadowed field is where a subclass replaces data from a superclass with a different version of the data Accessing shadowed fields public class ShortTermLoan extends LoanAccount{ float loanPeriod;// no of yrs loanPeriod //refers to ShortTermLoan field this.loanPeriod //refers to ShortTermLoan field super.loanPeriod //refers to LoanAccount field

17 Overriding Methods Overriding is where a subclass decides to supply its own version of an instance method already supplied by its superclass –general or default method provided in superclass –more specialised method provided in subclass Example - toString() method

18 toString() The root Object class has a toString() method which allows an object of the class to be output as a string public String toString(); The toString() method is overridden at all levels in the hierarchy Useful for outputing details of any object System.out.println(“Account details: “+myAcc); string concatenation automatically invokes a toString() on myAcc to convert it to a String

19 Overriding example public class BankAccount { public String toString(){ String s=“Bank Account for ”; s+=name+”\nbalance= “+balance; return s; } } public class LoanAccount { public String toString(){ String s=“\nLoan Account for ”; s+=name+”\nbalance= “+balance; s+=“Loan of ”+loanAmt; s+=“ for ”+loanPeriod+ “years”; return s; } }

20 super use the super keyword to invoke the superclass version of an overridden method // Bank Account toString method public String toString(){ String s=“Account for ”; s+=name+”\nbalance= “+balance; return s; } // Loan Account toString method public String toString(){ String s= super.toString(); s+=“Loan of ”+loanAmt; s+=“ for ”+loanPeriod+ “years”; return s; }

21 Overriding Where there are several instances of the same method in a hierarchy – the one in the closest subclass is always used. Even if an object of the subclass is assigned to an object of the superclass and method is invoked from the subclass. System.out.println(myAcc); uses BankAccount toString() System.out.println(myLoan); uses LoanAccount toString() myAcc=myLoan; System.out.println(myAcc); still uses LoanAccount toString()

22 Dynamic Binding Overriding uses dynamic binding or dynamic method lookup to find the correct method at run time Example: Consider an account object acc, acc.toString() which toString() method should be run? The interpreter checks the type of the object acc and then finds the toString() method that is appropriate for that type Dynamic binding is also known as virtual method invocation (what C++ does for virtual functions) Note: Overriding is not the same as Overloading Overloading does not use dynamic binding

23 Shadowing vs Dynamic Binding class A{ int i=1;// an instance field int f(){ return i;};// an instance method } class B extends A{ // define a subclass of A int i=2;// shadow field i in A int f(){ return –i; };// override method f in A }... B b = new B(); // create obj of type B System.out.println(b.i); // prints 2 System.out.println(b.f()); // prints –2 A a = (A) b; // cast b as a obj of class A System.out.println(a.i); // prints 1 System.out.println(a.f(g)); // still prints -2

24 Polymorphism Overriding + dynamic binding = polymorphism Polymorphism is the ability of different objects to respond to the same message (method) in their own way

25 Encapsulation Encapsulation is the packaging of the object’s fields (and internal methods) within a protective shell/capsule of its methods –an object has a “public” interface that other objects use to communicate with it –an object maintains “private” information that can be changed without affecting other objects that depend on it or use it –“blackbox” effect Encapsulation achieved using Information Hiding

26 Information Hiding Information Hiding – reveal only what is needed to the user of the object –hides implementation details from user –facilitates maintenance –protects against willful or accidental damage –keeps API simple and “uncluttered” –facilitates ease of understanding and use

27 Accessibility Modifiers Information Hiding implemented through accessibility modifiers –public = accessible to any other object –private = accessible within the object itself –protected = accessible within the object and any of its subclasses (and to all classes within the package)

28 Encapsulation To implement encapsulation –All fields should be private –Include ‘get’ and ‘set’ methods (accessor methods) for all fields that need to be accessed by users –Hide internal methods (i.e. methods that are not relevant to users but are only used by the class itself)


Download ppt "Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism."

Similar presentations


Ads by Google