Java Technology Classes and Inheritance Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Session Plan
The basic constructs in Java
To introduce Classes, Objects and Methods
To introduce Inheritance
Implementing Inheritance in Java
To introduce Polymorphism
Structuring program by types of polymorphism
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
2
Classes & Objects in Java
Class
A class defines a real world or abstract entity.
A class defines both the behavior and attributes of a group of objects with similar characteristics
Object
An object is an instance or variable of a class. It is said to belong to the class.
An object can be distinguished from other objects by its attributes.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
3
Example : Objects and Classes object
class class Student char name int rollNo setName()
Jodie
Daria
Jane
Brittany
R001
R002
R003
R004
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
4
setRollNo() calcMarks()
Constituents of a Class public class myClass Data Members { (State) private int a; myClass() { Constructor a=0; } public static void main(String args[]) { System.out.println(“Hello”); } } Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
5
Startup Method
Creating Objects in Java
The New Keyword
E.g. :
Default Constructor
myClass myClassObject= new myClass(); Or myClass myNextClassObject= new myClass(5); User Defined Constructor Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
6
Access Specifiers
Access Control modifiers
Four Access Control Modifiers in Java
public
private
protected
package [default access specifier]
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
7
Access Specifiers
Access specifiers could be used on classes in Java
All classes belong to packages in Java
“public” types are the only ones accessible outside the package
private and protected specifier are invalid for classes
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
8
Access Specifiers Private
Default Specifier
Protected
Public
Same Class
Yes
Yes
Yes
Yes
Same Package Sub Class
No
Yes
Yes
Yes
Same Package NonSub Class
No
Yes
Yes
Yes
Different Package Sub Class
No
No
Yes
Yes
Different Package Non-Sub Class
No
No
No
Yes
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
9
Modifiers in Java
Access Specifiers – Access Control modifiers
static
final
abstract
synchronized
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
10
Static
“static” -> the members can be used before the object is created. Can be accessed without reference to any object
For variable:
Global Variables (for all objects)
For methods:
Can call only static methods
Can refer to only static variables
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
11
Final
“final” modifier has a meaning based on its usage
For variable:
Primitives: read-only
Objects: reference is read-only
use all upper case letters
For methods: no overriding
For classes: no inheritance
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
12
Abstract
“abstract” modifier is used to defer an operation
Cannot be used for variables
For methods: no implementation
For classes: no instantiation
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
13
Abstract – Rules to follow
The following cannot be marked with “abstract” modifier
Constructors
Static methods
Private methods
Methods marked with “final” modifier
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
14
Methods in Java
Methods are written within a class
Override a method
Overload a method
Parameter passing
By Value
By reference
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
15
Nested Classes & Inner Classes
A class written within another class is known as nested class
The scope of the nested class is bounded by the scope of its enclosing class.
A nested class has access to the members, including private members, of the class in which it is nested.
The enclosing class does not have access to the members of the nested class Overload a method
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
16
Inheritance in Java
Inheritance in Java is achieved using
Parent
‘extends’ keyword. Multiple Inheritance is not allowed. E.g. : Class parent {………}
Child
Class child extends parent
Inheritance in Java (Single Inheritance)
{………} Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
17
Inheritance in Java GrandParen t
Account
Parent
Child Multilevel Inheritance Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Savings A/C
Current A/C
Hierarchical Inheritance
18
Inheritance in Java Paternal
Matern al
Student
Test Child
Final Result
Multiple Inheritance (Not Allowed) Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Sports
Hybrid Inheritance (Not Allowed) 19
Inheritance in Java GrandParen t
Paternal
Maternal
Parent Child
Child
Not Allowed in Java
Allowed in Java Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
20
Inheritance in Java Following are the code for the diagram in the slide shown before : Class Parent extends GrandParent {
Class Child extends Paternal, Maternal {
}
}
Class Child extends Parent { }
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
The code written above is not acceptable by Java 21
The super keyword
Super is a java keyword that allows a method to access/refer the hidden variables and overriden methods of the super class.
Example:
Super.aNumber,
Super.getNumber()
The method super() is used to invoke the constructor of super class.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
22
Abstract class:
The classes without complete implementation.
The keyword used is abstract
Abstract classes cannot be instantiated.
Abstract classes contain zero or more abstract methods, which are later implemented by concrete classes.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
23
Abstract class Example <
> Vehicle Abstract drive()
Car Drive(){}
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Bus Drive(){}
24
Polymorphism
Polymorphism means, behaving differently at different time depending on the situation
Types
Method Overloading (Compile time/static Polymorphism)
Method Overriding (Run time/dynamic polymorphism)
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
25
Method Overloading
Java allows to have more than one method in the class to have same name, but different signature.
Signature includes method name and parameter types.
Example:
int add (int a, int b);
float add (float a, float b);
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
26
Constructor Overloading public class myMath { private int x; myMath(){ x=0; } myMath(int i) { x=i; } int add ( int a, int b){ return (a+b); } }
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Data Members (State)
Constructor
One more Constructor (Overloading)
Method (Behavior)
27
Method Overriding
A method defined in the base class can be overriden in the derived class.
This will change the behaviour in the derived class.
The signature should be same in both classes.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
28
Rules of overriding
A subclass CANNOT override methods that are declared final
A subclass MUST override methods that are declared abstract
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
29
Overloading vs Overriding
Overloading means the method name is same but the signature is different. Applicable only within a class.
Overriding means the name and signature are same but behaviour may be different. Applicable in class hierarchies.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
30
Summary
Constituents of a Class, Creating Objects in Java
Modifiers in Java (Access Control Modifiers+ Others)
Inheritance and its types in Java
Abstract Class
Polymorphism in Java
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
31