0% found this document useful (0 votes)
24 views

Features of OOPS

Uploaded by

Nitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Features of OOPS

Uploaded by

Nitya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

FEATURES OF

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

Encapsulation - Inheritance: Inheritance Hierarchies- Super and Subclasses - access control -


super keyword – final keyword-final classes and methods.

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.

 To achieve this, you must:

 declare class variables/attributes as private

 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,

with the first letter in upper case


public class Person
{
private String name;
public String getName() // Getter
{
return name;
}
public void setName(String newName) // Setter
{
this.name = newName;
}
}
public class Main1 {
public static void main(String[] args) {
Person myObj = new Person();
myObj.name = "John"; // error
System.out.println(myObj.name); // error
}
}
public class Person {
private String name;
public String getName() // Getter
{
return name;
}
public void setName(String newName) // Setter
{
this.name = newName;
}
}
public class Main1 {
public static void main(String[] args) {
Person myObj = new Person();
myObj.setName("John"); // Set the value of the name variable to "John"
System.out.println(myObj.getName());
}
JAVA INHERITANCE
 In Java, it is possible to inherit attributes and methods from one class to another.

 IS-A relationship which is also known as a parent-child relationship.

 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

 To inherit from a class, use the extends keyword.

 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

2. Multiple Inheritance (Through Interface)

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

 Multilevel Inheritance a derived class will be inheriting a parent

class and as well as the derived class act as the parent class to
other class.

 ClassB inherits the property of ClassA and again ClassB act as a

parent for ClassC.

 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

 It is mixer of two or more of the above types of

Inheritance

 Not supported by Java

 A hybrid inheritance can be achieved using interfaces.

yes you heard it right.

 By using interfaces you can have multiple as well

as hybrid inheritance in Java.


MULTIPLE INHERITANCE
 Not support by java

 one class extending more than one class

 The problem with “multiple inheritance” is that the derived class

will have to manage the dependency on two base classes.

 Multiple Inheritance is very rarely used in software projects. Using

Multiple inheritance often leads to problems in the hierarchy. This


results in unwanted complexity when further extending the class.
class X
{
void display()
{
System.out.println("class X dispaly method ");
}
}
class Y
{
void display()
{
System.out.println("class Y display method ");
}
}
public class Z extends X,Y
{
public static void main(String args[])
{
Z obj=new Z(); // Compile time error
obj.display();
}
}
INTERFACE
INTERFACE
 You cannot instantiate an interface.

 An interface does not contain any constructors.

 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

must be declared both static and final.

 An interface is not extended by a class; it is implemented by a class.

 An interface can extend multiple interfaces

 Methods in an interface are implicitly public.


ACCESS MODIFIERS IN
INHERITANCE
ACCESS MODIFIERS IN
INHERITANCE
 Public - Access anywhere

 Protected – sub class

 Private – own class


SAMPLE PROGRAM FOR PROTECTED ACCESS MODIFIER

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

public class PrivateDemo


{
private int a = 101;
private String s = "TutorialRide";
public void show()
{
System.out.println("Private int a = "+a+"\nString s = "+s);
}
public static void main(String args[])
{
PrivateDemo pd = new PrivateDemo();
pd.show();
System.out.println(pd.a+" "+pd.s);
}
}
SAMPLE PROGRAM FOR PRIVATE ACCESS MODIFIER - 2
SUPER KEYWORD
SUPER KEYWORD
 The super keyword in java is a reference variable that is used to refer parent class objects.

 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

also adds super() as the first statement.


class Animal{

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{

public static void main(String args[]){

Dog d=new Dog();


FINAL KEYWORD
FINAL KEYWORD
 Final variable ---- can’t change value

 Final method() ---- can’t be overridden to our extended class.

 Final Class ---- can’t extend to sub class


JAVA FINAL VARIABLE
class Bike9{

final int speedlimit=90; //final variable


If you make any variable as final,
void run(){ you cannot change the value of

speedlimit=400; // compile time error final variable. It will be constant.

public static void main(String args[]){

Bike9 obj=new Bike9();

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);

 A blank final variable can be


}

initialized in a constructor only.


public static void main(String args[]){
new Bike10();
}
}
2. STATIC BLANK FINAL
VARIABLE
class A{
static final int data; //static blank final variable
static
{
A static final variable that is not
data=50;
initialized at the time of declaration is
}
known as static blank final variable.
public static void main(String args[]){
It can be initialized only in static block.
System.out.println(A.data);
}
}
JAVA FINAL METHOD
class Bike{
final void run(){System.out.println("running");
} }
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph"); // compile time error
}
public static void main(String args[]){
Honda honda= new Honda(); If you make any method

honda.run(); as final, you cannot


} override it
}
FINAL PARAMETER
class Bike11{
int cube(final int n){
n=n+2;//can't be changed as n is final If you declare any
n*n*n;
parameter as final, you
}
cannot change the
public static void main(String args[]){
value of it.
Bike11 b=new Bike11();
b.cube(5);
}
}
FINAL METHOD CAN BE
INHERITED BUT NOT
OVERRIDDEN
class Bike{
final void run(){System.out.println("running...");}
}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
}
}
JAVA FINAL CLASS
final class Bike{}
class Honda1 extends Bike // compile time error
{ If you make any class
void run() as final, you cannot
{ extend it.
System.out.println("running safely with 100kmph");
}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}

You might also like