Labels

.NET Job Questions About Java Absract class Abstract class Abstract Class and Interface Aggregation ajax aop apache ofbiz Apache ofbiz tutrial Association authentication autocad basics batch Binary Tree bootstrap loader in java build Builder design pattern C++ Job Questions caching CallableStatement in java certifications Chain of responsibility Design pattern charts check parentheses in a string Classes classloader in java classloading concept code quality collage level java program Composition concurrency Concurrency Tutorial Converting InputStream to String Core Java core java concept core java interview questions Core Java Interview Questions Core Java Questions core java tutorial CyclicBarrier in Java data structures database Database Job Questions datetime in c# DB Db2 SQL Replication deserialization in java Design Patterns designpatterns Downloads dtd Eclipse ejb example/sample code exception handling in core java file handling injava File I/O vs Memory-Mapped Filter first program in spring flex Garbage Collection Generics concept in java grails groovy and grails Guice Heap hibernate Hibernate Interview Questions how-to IBM DB2 IBM DB2 Tutorial ide immutable Interceptor Interface interview Interview Questions for Advanced JAVA investment bank j2ee java JAVA Code Examples Java 7 java changes java class loading JAVA Classes and Objects Java Classloader concept Java classloading concept java cloning concept java collection Java collection interview questions Java Collections java concurrency Java CountDownLatch java definiton Java design pattern Java EE 5 Java EE 6 Java Exceptions Java file Java Garbage Collection Java generics Java Glossary java hot concept java immutable concept Java Interface Java interview Question java interview question 2012 java interview question answer Java Interview Questions Java Interview Questions and Answers java interview topic java investment bank Java Job Questions java multithreading java multithreading concept java new features Java Packages java proxy object java questions Java Serialization Java serialization concept java serialization interview question java session concept java string Java Swings Questions java synchronization java threading Java Threads Questions java tutorial java util; java collections; java questions java volatile java volatile interview question Java Wrapper Classes java.java1.5 java.lang.ClassCastException JavaNotes javascript JAX-WS jdbc JDBC JDBC Database connection jdk 1.5 features JDK 1.5 new features Concurrent HashMap JMS interview question JMS tutorial job JSESSIONID concept JSESSIONID interview Question JSF jsp JSP Interview Question JSP taglib JSTL with JSP Junit Junit Concept Junit interview question.Best Practices to write JUnit test cases in Java JVM Linux - Unix tutorial Marker Interfaces MD5 encryption and decryption messaging MNC software java interview question musix NCR java interview question Networking Job Questions news Object Serialization Objects ojdbc14.jar OOP Oracle Oracle SQL Query for two timestamp difference orm own JavaScript function call in Apache ofbiz Packages Palm Apps patterns pdf persistence Portal Portlet Spring Integration Prime number test in java programs Rails Reboot remote computers REST Ruby Sample application schema SCJP security Senior java developer interviews servlet3 servlets session tracking singleton design pattern Spring Spring 2.5 Framework spring ebook Spring framework concept spring MVC spring pdf Spring Security Spring Security interview questions SQL SQL performance SQL Query to create xml file Sql Query tuning ssis and ssrs StAX and XML string concept string immutable string in java strings struts Struts2 Struts2 integration synchronization works in java Technical Interview testing tips Tomcat top Tutorial Volatile in deep Volatile working concept web Web Developer Job Questions web services weblogic Weblogic Application Server websphere what is JSESSIONID xml XML parsing in java XML with Java xslt


Thursday 18 July 2013

Two Minute Drills of SCJP – Kathy Sierra Part-2

Encapsulation, IS-A, HAS-A
  • Encapsulation helps hide implementation behind an interface (or API).
  • Encapsulated code has two features:
  • Instance variables are kept protected (usually with the private modifier).
  • Getter and setter methods provide access to instance variables.
  • IS-A refers to inheritance or implementation.
  • IS-A is expressed with the keyword extends.
  • IS-A, "inherits from," and "is a subtype of " are all equivalent expressions.
  • HAS-A means an instance of one class "has a" reference to an instance of
  • Another class or another instance of the same class.

 

Inheritance
  • Inheritance allows a class to be a subclass of a superclass, and thereby
  • Inherit public and protected variables and methods of the superclass.
  • Inheritance is a key concept that underlies IS-A, polymorphism, overriding, overloading, and casting.
  • All classes (except class Object), are subclasses of type Object, and therefore they inherit Object's methods.

Polymorphism
  • Polymorphism means "many forms."
  • A reference variable is always of a single, unchangeable type, but it can refer to a subtype object.
  • A single object can be referred to by reference variables of many different types —as long as they are the same type or a supertype of the object.
  • The reference variable's type (not the object's type), determines which methods can be called!
  • Polymorphic method invocations apply only to overridden instance methods.

Overriding and Overloading
  • Methods can be overridden or overloaded; constructors can be overloaded but not overridden.
  • Abstract methods must be overridden by the first concrete (non-abstract) subclass.
  • With respect to the method it overrides, the overriding method Must have the same argument list.
  • Must have the same return type, except that as of Java 5, the return type
  • can be a subclass—this is known as a covariant return.
  • Must not have a more restrictive access modifier.
  • May have a less restrictive access modifier.
  • Must not throw new or broader checked exceptions.
  • May throw fewer or narrower checked exceptions, or any unchecked exception.
  • final methods cannot be overridden.
  • Only inherited methods may be overridden, and remember that private methods are not inherited.
  • A subclass uses super.overriddenMethodName() to call the superclass version of an overridden method.
  • Overloading means reusing a method name, but with different arguments.
  • Overloaded methods Must have different argument lists
  • May have different return types, if argument lists are also different
  • May have different access modifiers
  • May throw different exceptions
  • Methods from a superclass can be overloaded in a subclass.
  • Polymorphism applies to overriding, not to overloading.
  • Object type (not the reference variable's type), determines which overridden method is used at runtime.
  • Reference type determines which overloaded method will be used at compile time.

 

Reference Variable Casting
  • There are two types of reference variable casting: downcasting and upcasting.
  • Downcasting: If you have a reference variable that refers to a subtype object, you can assign it to a reference variable of the subtype. You must make an explicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.
  • Upcasting: You can assign a reference variable to a supertype reference variable explicitly or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable.

Implementing an Interface
  • When you implement an interface, you are fulfilling its contract.
  • You implement an interface by properly and concretely overriding all of the methods defined by the interface.
  • A single class can implement many interfaces.

Return Types
  • Overloaded methods can change return types; overridden methods cannot, except in the case of covariant returns.
  • Object reference return types can accept null as a return value.
  • An array is a legal return type, both to declare and return as a value.
  • For methods with primitive return types, any value that can be implicitly converted to the return type can be returned.
  • Nothing can be returned from a void, but you can return nothing. You're allowed to simply say return, in any method with a void return type, to bust out of a method early. But you can't return nothing from a method with a non-void return type.
  • Methods with an object reference return type, can return a subtype.
  • Methods with an interface return type, can return any implementer.

Constructors and Instantiation
  • A constructor is always invoked when a new object is created.
  • Each superclass in an object's inheritance tree will have a constructor called.
  • Every class, even an abstract class, has at least one constructor.
  • Constructors must have the same name as the class.
  • Constructors don't have a return type. If you see code with a return type, it's a method with the same name as the class, it's not a constructor.
  • Typical constructor execution occurs as follows:
    • The constructor calls its superclass constructor, which calls its superclass constructor, and so on all the way up to the Object constructor.
    • The Object constructor executes and then returns to the calling constructor, which runs to completion and then returns to its calling constructor, and so on back down to the completion of the constructor of the actual instance being created.
    • Constructors can use any access modifier (even private!).
    • The compiler will create a default constructor if you don't create any constructors in your class.
    • The default constructor is a no-arg constructor with a no-arg call to super().
    • The first statement of every constructor must be a call to either this() (an overloaded constructor) or super().
    • The compiler will add a call to super() unless you have already put in a call to this() or super().
    • Instance members are accessible only after the super constructor runs.
    • Abstract classes have constructors that are called when a concrete subclass is instantiated.
    • Interfaces do not have constructors.
    • If your superclass does not have a no-arg constructor, you must create a constructor and insert a call to super() with arguments matching those of the superclass constructor.
    • Constructors are never inherited, thus they cannot be overridden.
    • A constructor can be directly invoked only by another constructor (using a call to super() or this()).
    • Issues with calls to this() May appear only as the first statement in a constructor.
    • The argument list determines which overloaded constructor is called.
    • Constructors can call constructors can call constructors, and so on, but sooner or later one of them better call super() or the stack will explode.
    • Calls to this() and super() cannot be in the same constructor. You can have one or the other, but never both.


Statics
  • Use static methods to implement behaviors that are not affected by the state of any instances.
  • Use static variables to hold data that is class specific as opposed to instance specific—there will be only one copy of a static variable.
  • All static members belong to the class, not to any instance.
  • A static method can't access an instance variable directly.
  • Use the dot operator to access static members, but remember that using a reference variable with the dot operator is really a syntax trick, and the compiler will substitute the class name for the reference variable, for instance:

d.doStuff();

becomes:

Dog.doStuff();
  • static methods can't be overridden, but they can be redefined.

Coupling and Cohesion
  • Coupling refers to the degree to which one class knows about or uses members of another class.
  • Loose coupling is the desirable state of having classes that are well encapsulated, minimize references to each other, and limit the breadth of API usage.
  • Tight coupling is the undesirable state of having classes that break the rules of loose coupling.
  • Cohesion refers to the degree in which a class has a single, well-defined role or responsibility.
  • High cohesion is the desirable state of a class whose members support a single, well-focused role or responsibility.
  • Low cohesion is the undesirable state of a class whose members support multiple, unfocused roles or responsibilities.

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...