Java Chapter 4 - OOP Concepts[1]
Java Chapter 4 - OOP Concepts[1]
OOP
Concepts
Introduction
• The three fundamental object oriented programming
principles are:-
• Encapsulation (data hiding) and Data abstraction
• Inheritance
• Polymorphism
• Main purpose of these principles is to manage
software system complexity by improving software
quality factors.
Encapsulation
Encapsulation refers to the combining
of fields and methods together in a class
such that the methods operate on the
data, as opposed to users of the class
accessing the fields directly.
It is a the technique which involves
making the fields in a class private and
providing access to the fields via public
methods.
M
M
2
1
Fields/Data
4
M
3
Continued…
If a field is declared private, it cannot be accessed by anyone
outside the class, thereby hiding the fields within the class.
For this reason, encapsulation is also referred to as data
hiding.
Encapsulation can be described as a protective barrier that
prevents the code and data being randomly accessed by other
code defined outside the class.
The main benefit of encapsulation is the ability to modify our
implemented code without breaking the code of others who
use our code.
Example
public class EncapTest{ public void setAge( int newAge)
private String {
name; age = newAge;
private String }
idNum; public void setName(String
private int age; newName){
public int getAge() name = newName;
{ }
return age; public void setIdNum( String
} newId){
public String idNum = newId;
getName(){ }
return name; }
}
public String
Continued…
• The public methods are the access points to this class’s fields from
the outside java world.
• Normally these methods are referred as getters and setters.
• Therefore any class that wants to access the variables should
access them through these getters and setters.
• The variables of the EncapTest class can be accessed as below:-
public class RunEncap{
public static void main(String args[]){ Out Put:
EncapTest encap = new EncapTest(); Name : James Age : 20
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName()+ " Age : "+ encap.getAge());
}
}
Data Abstraction
Classes normally hide the
details of their
implementation from their
clients.
This is called information hiding.
The client cares about what
functionality a class offers,
not about how that
functionality is implemented.
This concept is referred to as
data abstraction.
Inheritance
Object-oriented programming (OOP): allows you to derive
new classes from existing classes. This is called inheritance.
protected Members
Methods
Attributes
Public Members
Methods
Continued…
An instance method can be overridden only if it is accessible.
Thus a private method cannot be overridden, because it is
not accessible outside its own class.
If a method defined in a subclass is private in its superclass,
the two methods are completely unrelated.
Applications of inheritance
Specialization
The new class or object has data or behavior aspects that are
not part of the inherited class.
Overriding
Permit a class or object to replace the implementation of an
aspect—typically a behavior—that it has inherited
Code re-use
Re-use of code which already existed in another class.
Continued…
• In Java inheritance is represented by the key word extends.
Syntax
modifier class SubclassName extends SuperclassName
{
//body
}
Example
public class Mammal extends Animal
{
//body
}
Continued…
• The inheritance relationship is transitive: if class x extends class y, then
a class z, which extends class x, will also inherit from class y.
public Student() {
name = “”;
}
public Student(String s) {
name = s;
}
public String getName(){
return name;
}
}
Continued…
• The instanceof operator compares an object to a specified type.
• You can use it to test if an object is an instance of a class, an
instance of a subclass, or an instance of a class that implements a
particular interface.
public class Dog extends Mammal{
public static void main(String args[]){
Animal a = new Animal();
Mammal m = new Mammal();
Dog d = new Dog();
System.out.println(m instanceof Animal);
System.out.println(d instanceof Mammal);
System.out.println(d instanceof Animal);
}
}
Example
class Animal {
public int numberOfLegs;
public void talk() {
System.out.println("Hello");
}
}
class Dog extends Animal {
public int numberOfFleas;
public Dog() {
numberOfLegs = 4;
numberOfFleas = 10;
}
public void bark() {
System.out.println("Woof woof");
}
public void scratch() {
if (numberOfFleas > 0) numberOfFleas--;
}
}
Continued…
Dog d=new Dog();
d.bark();
d.scratch();
System.out.println(d.numberOfFleas);
• Because the Dog class inherits from the Animal class, we can
also use the d reference to access the methods and properties
of the Animal class.
• Use the code below:-
d.talk();
System.out.println(d.numberOfLegs);
Super keyword
• When extending a class you can reuse the immediate
superclass constructor and overridden superclass methods
by using the reserved word super.
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
Invoke Faculty
public Faculty() { constructor
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
Invoke Employee’s no-arg
class Employee extends Person {
public Employee() { constructor
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
Invoke Employee(String)
class Employee extends Person {
public Employee() {
constructor
this("(2) Invoke Employee’s overloaded constructor");
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() { Execute println
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
Execute println
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() { Execute println
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Trace Execution
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Faculty's no-arg constructor is invoked");
}
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Person's no-arg constructor is invoked");
}
}
Inheritance Example
Continued…
Continued…
Continued…
Continued
• You can also use super to refer to a hidden field.
Example:
java.lang.System, java.lang.Math and java.lang.String.
Continued…
• A final method cannot be overridden by subclasses.
This is used to prevent unexpected behavior from a
subclass altering a method that may be crucial to the
function or consistency of the class.
interface Animal {
public void eat();
public void travel();
}
Continued…
public class Mammal implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels"); }
public int noOfLegs(){
return 0; }
public static void main(String args[]){
Mammal m = new Mammal();
Output:
m.eat(); Mammal eats
m.travel(); Mammal travels
}
}
Extending Interfaces
An interface can extend another interface, similarly to the
way that a class can extend another class.
The extends keyword is used to extend an interface, and the
child interface inherits the methods of the parent interface.
public interface Sports{
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
public interface Football extends Sports{
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
Continued…
public interface Hockey extends Sports{
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
The Hockey interface has four methods, but it inherits two
from Sports; thus, a class that implements Hockey needs to
implement all six methods.
Similarly, a class that implements Football needs to define the
three methods from Football and the two methods from Sports.
Note that an interface can extend more than one parent
interface
public interface Hockey extends Sports, Event
Continued…
• In Java, a subclass can extend only one superclass.
• In Java, a subinterface can extend several superinterface
• In Java, a class can implement several interfaces — this is Java’s
form of multiple inheritance.
• An abstract class can have code for some of its methods; other
methods are declared abstract and left with no code.
• An interface only lists methods but does not have any code.
• Inheritance plays a dual role:
• A subclass reuses the code from the superclass.
• A subclass (or a class that implements an interface) inherits
the data type of the superclass (or the interface) as its own
secondary type.
Abstrac vs Interface
• Unlike interfaces, abstract classes can contain fields that are not
static and final, and they can contain implemented methods.