Features of OOPS
Features of OOPS
OOP
MODULE NO. 1
OBJECT-ORIENTED PROGRAMMING –
FUNDAMENTALS
Data types, variables, Array, Operators, String function, Control statements
Features of OOP – Objects and Classes in Java – Defining Classes – Methods - Access
Specifiers – Static Members – Constructors, this Keyword
2
ENCAPSULATION
• Encapsulation is defined as the wrapping up of data under a single unit.
• Technically in encapsulation, the variables or data of a class is hidden from any other
class and can be accessed only through any member function of its own class in which it
is declared.
provide public get and set methods to access and update the value of a private
variable
GET AND SET METHODS
Private variables can only be accessed within the same class (an outside class has no access to
it).
However, it is possible to access them if we provide public get and set methods.
The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable,
subclass (child) - the class that inherits from another class. Also called as derived class or
extended class
superclass (parent) - the class being inherited from. AKA base class
Reusability
In the example, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass)
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method We set the brand
System.out.println("Tuut, tuut!"); attribute in Vehicle to a
protected access
}} modifier.
class Car extends Vehicle { If it was set to private,
the Car class would not
private String modelName = "Mustang"; // Car attribute be able to access it.
public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class) and the value of the modelName (from the Car
class)
System.out.println(myCar.brand + " " + myCar.modelName);
TYPES OF INHERITANCE
1. Single Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
SINGLE INHERITANCE
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
MULTILEVEL INHERITANCE
class and as well as the derived class act as the parent class to
other class.
In Short ClassA parent for ClassB and ClassB parent for ClassC.
Class X {
public void methodX( )
{ System.out.println("Class X method"); }
}
Class Y extends X {
public void methodY( )
{ System.out.println("class Y method"); }
}
Class Z extends Y
{
public void methodZ()
{ System.out.println("class Z method"); }
public static void main(String args[])
{
Z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodZ(); //calling local method
}
HIERARCHICAL INHERITANCE
Hierarchical inheritance one parent class will be inherited by many sub classes
D:\DD\JavaExampleHieInh.java
HYBRID INHERITANCE
Inheritance
An interface is implicitly abstract. You do not need to use the abstract keyword while declaring
an interface.
Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
An interface cannot contain instance fields. The only fields that can appear in an interface
class Employee
{
protected int id = 101;
protected String name = "Jack";
}
public class ProtectedDemo extends Employee
{
private String dept = "Networking";
public void display()
{
System.out.println("Employee Id : "+id);
System.out.println("Employee name : "+name);
System.out.println("Employee Department : "+dept);
}
public static void main(String args[])
{
ProtectedDemo pd = new ProtectedDemo();
pd.display();
}
}
SAMPLE PROGRAM FOR PRIVATE ACCESS MODIFIER - 1
Use of super with variables: This scenario occurs when a derived class and base class has
same data members. In that case there is a possibility of ambiguity for the JVM.
Use of super with methods: This is used when we want to call parent class method. So
whenever a parent and child class have same named methods then to resolve ambiguity we
use super keyword.
Use of super with constructors: super keyword can also be used to access the parent class
constructor.
D:\DD\sup.java
SUPER IS USED TO REFER IMMEDIATE
PARENT CLASS INSTANCE VARIABLE
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color); //prints color of Dog class
System.out.println(super.color); //prints color of Animal class
}
We can use super keyword to access
}
the data member or field of parent
class TestSuper1{
class. It is used if parent class and child
public static void main(String args[]){
class have same fields.
Dog d=new Dog();
d.printColor();
SUPER CAN BE USED TO INVOKE PARENT
CLASS METHOD
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
The super keyword can also be used to
void work(){ invoke parent class method. It should be
super.eat(); used if subclass contains the same
method as parent class. In other words,
bark();
it is used if method is overridden.
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
d.work();
SUPER IS USED TO INVOKE PARENT
CLASS CONSTRUCTOR
class Animal{
Animal(){System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
The super keyword can
super(); also be used to invoke
System.out.println("dog is created"); the parent class
constructor.
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
Default constructor is provided by compiler automatically if there is no constructor. But, it
Animal(){System.out.println("animal is created");}
}
super() is added in each
class constructor
class Dog extends Animal{
automatically by compiler
if there is no super() or
Dog(){
this().
System.out.println("dog is created");
class TestSuper4{
obj.run();
}
1. BLANK FINAL VARIABLE AND ITS
INITIALIZATION
class Bike10{
final int speedlimit; //blank final variable
A final variable that is not
initialized at the time of Bike10(){
declaration is known as blank speedlimit=70;
final variable. System.out.println(speedlimit);