Object-oriented programming is at the core of Java. In fact , all the Java programs are object-oriented. this isn’t an option the way that it is in C++, for example. OOP is so integral to Java that you must understand its basic principles before you can write the simple Java programs . Therefore , this course will begin with a discussion of the theoretical aspects of OOP.
THE THREE OOP PRINCIPLES
All object-oriented programming languages provide mechanisms that help you to implement the object-oriented model. They are encapsulation , inheritance and polymorphism . Let’s take a look at these concepts now.
- Encapsulation is the one of the four fundamental OOP concepts . Encapsulation is the mechanism that bind together the code and data t manipulates , and keeps both safe and secure from outside interference and misuse.
- Encapsulation is the technique of making the fields in a class private and providing them access to the fields via public methods.
- One way to think about encapsulation is a protective wrapper that prevents the code and the data from being arbitrarily accessed by the other code defined outside the wrapper.
- In Java the basis of encapsulation is the class .
I think it is little bit complex to understand the terms like code , data and class . Well I will explain you actually what are they in Java .
- When you create a class ( a class is nothing but a logical representation of the program ), you will specify the code and the data that constitute a class, collectively these elements are known as the members of the class.
- specially , the data defined by the class are referred to as member variables or instance variables( in C-language we call it as global variables ) . The code that operates on the data is referred to as member methods or just methods ( where as in C-language we call it as function )
Now each method or variable in a class may be marked as public or private . The public interface of a class represents everything that external is users of the class need to know , or may know . The private method and data can only be accessed by code that is a member of the class. Therefore any other code that is not a member of a class cannot access a private method or variable ( In stance variable ).
- Inheritance is the process by which one object acquires the properties of another object . Inheritance interacts with encapsulation as well. If a given class encapsulates some attributes , then any subclass will have the same attributes plus any that it adds as a part of it’s specialisation . A new subclass inherits all of the properties or attributes of all of it’s superclass.
- Inheritance means to take something that is already made it is nothing but in programming terms, it is the mechanism through which we can derived classes from other classes . The derived class is called a child-class or a subclass or we can say the extended class , the class from which we are deriving the subclass is called the base class or parent class .
- To derive a class in Java we use the keyword
Types of inheritance :
- simple inheritance
- multilevel inheritance
pictorial representation of the simple and multilevel inheritance .
Simple Inheritance :
When a subclass is derived from it’s parent class then this mechanism is known as simple inheritance . In simple inheritance there is only one subclass and one parent class, it is also called single inheritance or one level inheritance .
simple inheritance program :
Multilevel inheritance : multilevel inheritance can go up to any number of level
polymorphism ( from the Greek meaning ” many forms ” ) is a feature that allows one interface to be used for a general class of actions . More generally . the concept of polymorphism is often expressed by the phrase ” one interface , multiple methods ” . This means that it is possible o design a generic interface to a group of relative activities . This helps reduce complexity by allowing the same interface to be used to specify a general class of action .