Introduction to OOPS with Java
Introduction to OOPS with Java
[CSE202]
Every object has a different copy of all the fields of the class
◦ Except static fields, which we will discuss in the next lecture
Every object has a different copy of all the fields of the class
◦ Except static fields, which we will discuss in the next lecture
Every object can invoke methods of the class, which operate on the object’s copy of the fields
◦ Except static methods, which may be invoked just with the class too, which we will discuss in the next lecture
Common properties between two objects, can be abstracted into an object of a super type
Common properties between two objects, can be abstracted into an object of a super type
The concept of Inheritance formalizes this observation
◦ A class is said to be derived out of one or more classes, if it inherits their properties
◦ One or more classes, from which this class is derived, are known as the super classes of the class
A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on
A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on
Java does not support multiple inheritance (having more than one super class for a class)
◦ There is an indirect way to apply multiple inheritance though – via Interfaces, which we’ll discuss shortly
A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on
Java does not support multiple inheritance (having more than one super class for a class)
◦ There is an indirect way to apply multiple inheritance though – via Interfaces, which we’ll discuss shortly
Because of this, a variable of a super class, can hold reference to an object of a derived class
◦ Check the Homework ;)
private Y N N N
default (no modifier) Y Y N N
protected Y Y Y N
public Y Y Y Y
Source: https://www.javatpoint.com/access-modifiers
SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD
Methods of a class
A method operates on the fields of the class
All methods, including a constructor, have one of those four access levels
◦ In addition, any method other than the constructor can also be abstract
abstract methods represent operations which are too abstract for a class
◦ However, these operations are concrete for any derived class
abstract methods represent operations which are too abstract for a class
◦ However, these operations are concrete for any derived class
While a class with even one abstract method must be abstract, the converse is not true
◦ You can declare a class as abstract, without any abstract method also
While a class with even one abstract method must be abstract, the converse is not true
◦ You can declare a class as abstract, without any abstract method also
A class can be made abstract, by putting the keyword abstract before the class keyword
[public] abstract class <Abstract Class Name> {
// Members details
}
However, Java does allow multiple inheritance with some restrictions via Interfaces
However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration
However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration
However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration
However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration